Sha256: fb1f8a9ce80d5dad095b11d1ce915d7e0ea139344e877046ec06bc67d147cf63

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

require 'singleton'
require 'forwardable'

module Exchange
  
  class Configurable
    include Singleton
    extend SingleForwardable
    
    attr_accessor :subclass
    
    def_delegators :instance, :subclass, :subclass=, :set
     
    def subclass_with_constantize
      self.subclass = parent_module.const_get camelize(self.subclass_without_constantize) unless self.subclass_without_constantize.is_a?(Class)
      subclass_without_constantize
    end
    alias_method :subclass_without_constantize, :subclass
    alias_method :subclass, :subclass_with_constantize
    
    def set hash
      hash.each_pair do |k,v|
        self.send(:"#{k}=", v)
      end
      
      self
    end
    
    def reset
      set Exchange::Configuration::DEFAULTS[key]
    end
    
    [:key, :parent_module].each do |subclass_method|
      define_method subclass_method do
        raise StandardError.new("Subclass Responsibility")
      end
    end
    
    private

      # Camelize a string or a symbol
      # @param [String, Symbol] s The string to camelize
      # @return [String] a camelized string
      # @example Camelize an underscored symbol
      #   camelize(:some_thing) #=> "SomeThing"
      #
      def camelize s
        s = s.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
      end
    
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
exchange-0.11.0 lib/exchange/configurable.rb
exchange-0.10.2 lib/exchange/configurable.rb
exchange-0.10.1 lib/exchange/configurable.rb
exchange-0.10.0 lib/exchange/configurable.rb
exchange-0.9.0 lib/exchange/configurable.rb