lib/configurations/configuration.rb in configurations-1.3.5 vs lib/configurations/configuration.rb in configurations-1.4.0

- old
+ new

@@ -1,5 +1,6 @@ +# -*- coding: utf-8 -*- module Configurations # Configuration is a blank object in order to allow configuration of various properties including keywords # class Configuration < BasicObject @@ -12,11 +13,11 @@ :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :to_enum, :enum_for else - # @!macro [attach] install_kernel_method + # @macro [attach] install_kernel_method # @method $1 # def self.install_kernel_method(method) kernel_method = ::Kernel.instance_method(method) @@ -98,10 +99,27 @@ h end end + # 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 + # @note can only be accessed during writeable state (in configure block). Unassignable values are ignored + # + def from_h(h) + raise ArgumentError, 'can not dynamically assign values from a hash' unless @_writeable + + h.each do |property, value| + if value.is_a?(::Hash) && _nested?(property) + @configuration[property].from_h(value) + elsif _configurable?(property) + _assign!(property, value) + end + end + end + # @param [Symbol] property The property to test for configurability # @return [Boolean] whether the given property is configurable # def _configurable?(property) _arbitrarily_configurable? or @configurable.has_key?(property) @@ -150,11 +168,11 @@ end end # @param [Symbol, Hash, Array] property configurable properties, either single or nested # @param [Symbol, Hash, Array] value configurable properties, either single or nested - # @param [Hash] assertions if any + # @param [Hash] assertion assertion if any # @return a hash with configurable values pointing to their types # def _configurable_hash(property, value, assertion) value = [value] unless value.is_a?(::Array) hash = ::Hash[value.zip([assertion].flatten*value.size)] @@ -201,9 +219,16 @@ # @param [Symbol] assertion_type The evaluation type type to test for # @return [Boolean] whether the given property is assertable # def _evaluable?(property, evaluation) @configurable and @configurable.has_key?(property) and @configurable[property].is_a?(::Hash) and @configurable[property].has_key?(evaluation) + end + + # @param [Symbol] property The property to test for + # @return [Boolean] whether this property is nested + # + def _nested?(property) + _arbitrarily_configurable? or @configuration.has_key?(property) and @configuration[property].is_a?(Configuration) end # @param [Symbol] method the method to test for # @return [Boolean] whether the given method is a writer #