class Configurations::Configuration

Configuration is a blank object in order to allow configuration of various properties including keywords

Public Class Methods

new(options = {}, &block) click to toggle source

Initialize a new configuration @param [Hash] options The options to initialize a configuration with @option options [Hash] methods a hash of method names pointing to procs @option options [Proc] not_configured a proc to evaluate for

not_configured properties
# File lib/configurations/configuration.rb, line 21
def initialize(options = {}, &block)
  @data = Data.new(__configuration_hash__)
  @path = options.fetch(:path) { Path.new }
  @data_map = options.fetch(:data) { Maps::Data.new }

  @methods = options.fetch(:methods) { ::Hash.new }
  @method_blocks = options.fetch(:method_blocks) { Maps::Blocks.new }
  @not_configured_blocks = options.fetch(:not_configured_blocks) { Maps::Blocks.new }

  @reserved_method_validator = Validators::ReservedMethods.new
  @key_ambiguity_validator = Validators::Ambiguity.new

  __instance_eval__(&options[:defaults]) if options[:defaults]
  __instance_eval__(&block) if block

  __install_configuration_methods__
end

Public Instance Methods

__configurable?(property) click to toggle source

@param [Symbol] property The property to test for configurability @return [Boolean] whether the given property is configurable

# File lib/configurations/configuration.rb, line 106
def __configurable?(property)
  if defined?(@configurable_properties) && @configurable_properties
    @configurable_properties.configurable?(@path.add(property))
  else
    true
  end
end
__configured?(property) click to toggle source

@param [Symbol] property The property to test for @return [Boolean] whether the given property has been configured

# File lib/configurations/configuration.rb, line 117
def __configured?(property)
  @data.key?(property)
end
__empty?() click to toggle source

@return [Boolean] whether this configuration is empty

# File lib/configurations/configuration.rb, line 122
def __empty?
  @data.empty?
end
from_h(h) click to toggle source

A convenience accessor to instantiate a configuration from a hash @param [Hash] h the hash to read into the configuration @return [Configuration] the configuration with values assigned @raise [ConfigurationError] if the given hash ambiguous values

- string and symbol keys with the same string value pointing to
different values
# File lib/configurations/configuration.rb, line 74
def from_h(h)
  @key_ambiguity_validator.validate!(h)

  h.each do |property, value|
    p = property.to_sym
    if value.is_a?(::Hash) && __nested?(p)
      @data[p].from_h(value)
    elsif __configurable?(p)
      __assign!(p, value)
    end
  end

  self
end
inspect(debug = false) click to toggle source

Inspect a configuration. Implements inspect without exposing internally used instance variables. @param [TrueClass, FalseClass] debug whether to show internals, defaults

to false

@return [String] The inspect output for this instance

Calls superclass method
# File lib/configurations/configuration.rb, line 95
def inspect(debug = false)
  unless debug
    '#<%s:0x00%x @data=%s>' % [__class__, object_id << 1, @data.inspect]
  else
    super()
  end
end
method_missing(method, *args, &block) click to toggle source

Method missing gives access to Kernel methods

Calls superclass method
# File lib/configurations/configuration.rb, line 41
def method_missing(method, *args, &block)
  if __can_delegate_to_kernel?(method)
    ::Kernel.__send__(method, *args, &block)
  else
    super
  end
end
respond_to_missing?(method, include_private = false) click to toggle source

Respond to missing according to the method_missing implementation

Calls superclass method
# File lib/configurations/configuration.rb, line 51
def respond_to_missing?(method, include_private = false)
  __can_delegate_to_kernel?(method) || super
end
to_h() click to toggle source

A convenience accessor to get a hash representation of the current state of the configuration @return [Hash] the configuration in hash form

# File lib/configurations/configuration.rb, line 59
def to_h
  @data.reduce({}) do |h, (k, v)|
    h[k] = v.is_a?(__class__) ? v.to_h : v

    h
  end
end

Protected Instance Methods

__assign!(property, value) click to toggle source

Assigns a value after running the assertions @param [Symbol] property the property to type test @param [Any] value the given value

# File lib/configurations/configuration.rb, line 172
def __assign!(property, value)
  @data_map.add_entry(@path.add(property), value)
  @data[property] = value
end
__can_delegate_to_kernel?(method) click to toggle source

@param [Symbol] method the method to test for @return [Boolean] whether the configuration can delegate

the given method to Kernel
# File lib/configurations/configuration.rb, line 192
def __can_delegate_to_kernel?(method)
  ::Kernel.respond_to?(method, true)
end
__configuration_hash__() click to toggle source

@return [Hash] A configuration hash instantiating subhashes

if the key is configurable
# File lib/configurations/configuration.rb, line 162
def __configuration_hash__
  ::Hash.new do |h, k|
    h[k] = __class__.__new__(__options_hash_for__(k)) if __configurable?(k)
  end
end
__install_configuration_methods__() click to toggle source

Installs the given configuration methods for this configuration as singleton methods

# File lib/configurations/configuration.rb, line 131
def __install_configuration_methods__
  entries = @method_blocks.entries_at(@path)
  entries.each do |meth, entry|
    @reserved_method_validator.validate!(meth)
    __define_singleton_method__(meth, &entry.block)
  end
end
__is_writer?(method) click to toggle source

@param [Symbol] method the method to test for @return [Boolean] whether the given method is a writer

# File lib/configurations/configuration.rb, line 180
def __is_writer?(method)
  method.to_s.end_with?('=')
end
__nested?(property) click to toggle source
# File lib/configurations/configuration.rb, line 184
def __nested?(property)
  @data[property].is_a?(__class__)
end
__options_hash_for__(property) click to toggle source

Instantiates an options hash for a nested property @param [Symbol] property the nested property to instantiate the hash for @return [Hash] a hash to be used for configuration initialization

# File lib/configurations/configuration.rb, line 143
def __options_hash_for__(property)
  nested_path = @path.add(property)

  hash = {}
  hash[:path] = nested_path
  hash[:data] = @data_map
  hash[:properties] = defined?(@properties) && @properties

  hash[:not_configured_blocks] = @not_configured_blocks

  hash[:method_blocks] = @method_blocks
  hash[:methods] = @methods[property] if @methods.key?(property)

  hash
end
__property_from_writer__(method) click to toggle source

@param [Symbol] method the writer method to turn into a property @return [Symbol] the property derived from the writer method

# File lib/configurations/configuration.rb, line 199
def __property_from_writer__(method)
  method.to_s[0..-2].to_sym
end