Class: ConfigMapper
- Inherits:
-
Object
- Object
- ConfigMapper
- Defined in:
- lib/config_mapper.rb,
lib/config_mapper/version.rb
Overview
Supports marshalling of plain-old data (e.g. loaded from YAML files) onto strongly-typed objects.
Defined Under Namespace
Classes: ErrorProxy, ObjectAsHash
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Class Method Summary collapse
-
.set(data, target) ⇒ Hash
Attempt to set attributes on a target object.
Instance Method Summary collapse
-
#initialize(target, errors = {}) ⇒ ConfigMapper
constructor
A new instance of ConfigMapper.
-
#set_attributes(data) ⇒ Object
Set multiple attributes from a Hash.
Constructor Details
#initialize(target, errors = {}) ⇒ ConfigMapper
Returns a new instance of ConfigMapper.
21 22 23 24 |
# File 'lib/config_mapper.rb', line 21 def initialize(target, errors = {}) @target = ObjectAsHash[target] @errors = errors end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
27 28 29 |
# File 'lib/config_mapper.rb', line 27 def errors @errors end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
26 27 28 |
# File 'lib/config_mapper.rb', line 26 def target @target end |
Class Method Details
.set(data, target) ⇒ Hash
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.
15 16 17 18 19 |
# File 'lib/config_mapper.rb', line 15 def self.set(data, target) mapper = new(target) mapper.set_attributes(data) mapper.errors end |
Instance Method Details
#set_attributes(data) ⇒ Object
Set multiple attributes from a Hash.
31 32 33 34 35 |
# File 'lib/config_mapper.rb', line 31 def set_attributes(data) data.each do |key, value| set_attribute(key, value) end end |