class DotConfig::Configuration

This class gives you access to the configuration with dot syntax.

@author {geoffrey.roguelon@gmail.com Geoffrey Roguelon} @example Using the dot syntax (readonly):

app_config = DotConfig::Configuration.new #=> #<DotConfig::Configuration:0x007fa5d38f4fa0>
app_config.config = { ruby: { rails => '4.0.0', sinatra: '1.3.3' } }
app_config.ruby.rails #=> '4.0.0'
app_config.python     #=> NoMethodError: undefined method `python' for #<DotConfig::Configuration:0x007fa5d38c23c0>

@example Using the dot syntax:

app_config = DotConfig::Configuration.new(writing: true) #=> #<DotConfig::Configuration:0x007fa5d38f4fa0>
app_config.config = { ruby: { rails => '4.0.0', sinatra: '1.3.3' } }
app_config.ruby.rails #=> '4.0.0'
app_config.ruby.rails = '3.2.9'
app_config.ruby.rails #=> '3.2.9'

Attributes

config[R]

@!attribute config [r]

@return [Hash] Stores the configuration like a +Hash+.

@!attribute writing [r]

@return [Hash] Determines if the configuration is mutable.
writing[R]

@!attribute config [r]

@return [Hash] Stores the configuration like a +Hash+.

@!attribute writing [r]

@return [Hash] Determines if the configuration is mutable.

Public Class Methods

new(options = {}) click to toggle source

Class constructor.

@return [self]

# File lib/dot_config/configuration.rb, line 29
def initialize(options = {})
  raise ArgumentError, 'Invalid argument' if !options.is_a?(Hash)

  @writing    = options[:writing] ? true : false
  self.config = options[:config] if options[:config].is_a?(Hash)
end

Public Instance Methods

config=(hash) click to toggle source

Assigns the configuration to the instance.

@param [Hash] hash The Hash configuration. @return [Hash] The Hash configuration.

# File lib/dot_config/configuration.rb, line 40
def config=(hash)
  raise ArgumentError, 'Invalid argument' if !hash.is_a?(Hash) || hash.empty?
  raise "Some keys are already used by Ruby" if collision_name?(hash.keys)

  @config = hash.reduce(Hash.new) do |config, (key, value)|
    config[key.to_sym] = if value.is_a?(Hash)
      self.class.new(config: value, writing: @writing)
    else
      value
    end
    config
  end
end
method_missing(name, *arguments) click to toggle source

Post treatment of call method which have failed.

@param [Symbol] name The name of the method called. @param [Array] arguments The arguments provided with the call. @return [Object] The value of the config or call the super method.

Calls superclass method
# File lib/dot_config/configuration.rb, line 59
def method_missing(name, *arguments)
  if name[-1, 1] == '=' && @config.key?(name[0..-2].to_sym)
    if @writing
      @config[name[0..-2].to_sym] = arguments.first
    else
      raise 'The configuration is not allowed in writing'
    end
  elsif @config.key?(name)
    @config[name]
  else
    super
  end
end
to_hash() click to toggle source

Returns the config like a Hash.

@return [Hash] The original hash with keys symbolized.

# File lib/dot_config/configuration.rb, line 76
def to_hash
  hash = Hash.new
  @config.each { |k, v| hash[k] = v.is_a?(self.class) ? v.to_hash : v }
  hash
end

Private Instance Methods

collision_name?(keys) click to toggle source

Checks if the keys duplicate the name of the instance methods.

@param [Array] keys The keys of the configuration. @return [Boolean] true if some keys match with the instance methods

name, otherwise +false+.
# File lib/dot_config/configuration.rb, line 89
def collision_name?(keys)
  # Retrieves all names of the instance methods.
  methods = self.class.instance_methods(true)
  keys.reduce(false) do |value, key|
    value || methods.include?(key.to_sym) || methods.include?(:"#{key}=")
  end
end