Class: ConfigMapper

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#errorsObject (readonly)

Returns the value of attribute errors.



27
28
29
# File 'lib/config_mapper.rb', line 27

def errors
  @errors
end

#targetObject (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.

Returns:

  • (Hash)

    exceptions encountered



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