Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hey Everybody - I Found a Use for a Singleton

Matt Polito
September 18, 2015

Hey Everybody - I Found a Use for a Singleton

Lightning talk given @ WindyCityRails 2015 about what I found a decent use of a Singleton would be in a Ruby library.

Matt Polito

September 18, 2015
Tweet

More Decks by Matt Polito

Other Decks in Programming

Transcript

  1. require 'singleton' module AppName module PaymentGatewayName class Configuration include Singleton

    attr_accessor :base_uri, :database_name, :database_type, :server_name, :username, :password end end end
  2. module AppName module PaymentGatewayName module API def self.configure yield(configuration) if

    block_given? end def self.configuration Configuration.instance end def self.connection url = configuration.base_url @connection ||= Faraday.new(url) end end end end
  3. module AppName module PaymentGatewayName module API ... def self.connection url

    = configuration.base_url @connection ||= Faraday.new(url) end end end end
  4. module AppName module PaymentGatewayName module API def self.configure yield(configuration) if

    block_given? end def self.configuration Configuration.instance end def self.connection url = configuration.base_url @connection ||= Faraday.new(url) end end end end
  5. AppName::PaymentGatewayName::API.configure do |config| config.base_uri = ENV.fetch("PAYMENTGATEWAY_BASE_URI") config.database_name = ENV.fetch("PAYMENTGATEWAY_DATABASE_NAME") config.database_type

    = ENV.fetch("PAYMENTGATEWAY_DATABASE_TYPE") config.password = ENV.fetch("PAYMENTGATEWAY_PASSWORD") config.server_name = ENV.fetch("PAYMENTGATEWAY_SERVER_NAME") config.username = ENV.fetch("PAYMENTGATEWAY_USERNAME") end