lib/config_mapper.rb in config_mapper-1.0.0 vs lib/config_mapper.rb in config_mapper-1.1.0

- old
+ new

@@ -1,23 +1,39 @@ -require "config_mapper/attribute_sink" +require "config_mapper/hash_target" +require "config_mapper/object_target" # Supports marshalling of plain-old data (e.g. loaded from # YAML files) onto strongly-typed objects. # module ConfigMapper - # Attempt to set attributes on a target object. - # - # For simple, scalar values, set the attribute by calling the - # named writer-method on the target object. - # - # For Hash values, set attributes of the named sub-component. - # - # @return [Hash] exceptions encountered - # - def self.set(data, target) - mapper = AttributeSink.new(target) - mapper.set_attributes(data) - mapper.errors + class << self + + # Set attributes of a target object based on configuration data. + # + # For simple, scalar values, set the attribute by calling the + # named writer-method on the target object. + # + # For Hash values, set attributes of the named sub-component. + # + # @param data configuration data + # @param [Object, Hash] target the object to configure + # + # @return [Hash] exceptions encountered + # + def configure_with(data, target) + target(target).with(data) + end + + alias_method :set, :configure_with + + def target(target) + if target.is_a?(Hash) + HashTarget.new(target) + else + ObjectTarget.new(target) + end + end + end end