Module: Kamerling::Mapper
- Defined in:
- lib/kamerling/mapper.rb
Class Method Summary collapse
- .from_h(klass, hash, repos: Repos) ⇒ Object
- .object_pair_from(key, value, repos) ⇒ Object
- .to_h(object) ⇒ Object
Class Method Details
.from_h(klass, hash, repos: Repos) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/kamerling/mapper.rb', line 7 def from_h(klass, hash, repos: Repos) attributes = hash.map do |key, value| case key when :host, :port, :prot then [:addr, Addr.new(hash)] when /_uuid$/ then object_pair_from(key, value, repos) else [key, value] end end.to_h klass.new(attributes) end |
.object_pair_from(key, value, repos) ⇒ Object
18 19 20 21 |
# File 'lib/kamerling/mapper.rb', line 18 def object_pair_from(key, value, repos) type = key[/(.*)_uuid$/, 1].to_sym [type, repos[Kamerling.const_get(type.capitalize)][value]] end |
.to_h(object) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/kamerling/mapper.rb', line 23 def to_h(object) object.to_h.reduce({}) do |hash, (key, value)| hash.merge case value when Addr then value.to_h when Hash then { :"#{key}_uuid" => value[:uuid] } else { key => value } end end end |