Class: ConfigMapper::AttributeSink
- Inherits:
-
Object
- Object
- ConfigMapper::AttributeSink
- Defined in:
- lib/config_mapper/attribute_sink.rb
Overview
Sets attributes on an object, collecting errors
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
-
#initialize(target, errors = {}) ⇒ AttributeSink
constructor
A new instance of AttributeSink.
-
#set_attribute(key, value) ⇒ Object
Set a single attribute.
-
#set_attributes(data) ⇒ Object
Set multiple attributes from a Hash.
Constructor Details
#initialize(target, errors = {}) ⇒ AttributeSink
Returns a new instance of AttributeSink.
10 11 12 13 |
# File 'lib/config_mapper/attribute_sink.rb', line 10 def initialize(target, errors = {}) @target = ObjectAsHash[target] @errors = errors end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
16 17 18 |
# File 'lib/config_mapper/attribute_sink.rb', line 16 def errors @errors end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
15 16 17 |
# File 'lib/config_mapper/attribute_sink.rb', line 15 def target @target end |
Instance Method Details
#set_attribute(key, value) ⇒ Object
Set a single attribute.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/config_mapper/attribute_sink.rb', line 28 def set_attribute(key, value) if value.is_a?(Hash) && !target[key].nil? nested_errors = ErrorProxy.new(errors, [key]) nested_mapper = self.class.new(target[key], nested_errors) nested_mapper.set_attributes(value) else target[key] = value end rescue NoMethodError, ArgumentError => e errors[[key]] = e end |
#set_attributes(data) ⇒ Object
Set multiple attributes from a Hash.
20 21 22 23 24 |
# File 'lib/config_mapper/attribute_sink.rb', line 20 def set_attributes(data) data.each do |key, value| set_attribute(key, value) end end |