Class: Arbor::Model::Serialiser
- Inherits:
-
Object
- Object
- Arbor::Model::Serialiser
- Defined in:
- lib/arbor/model/serialiser.rb
Class Method Summary collapse
- .load(data) ⇒ Object
- .parse_attributes(attributes) ⇒ Object
- .parse_resource(r) ⇒ Object
- .transform_keys(hash) ⇒ Object
Class Method Details
.load(data) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/arbor/model/serialiser.rb', line 7 def load(data) attributes = parse_attributes(data) resource = data["entityType"].underscore.to_sym Factory.create(resource).new(attributes) end |
.parse_attributes(attributes) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/arbor/model/serialiser.rb', line 14 def parse_attributes(attributes) attr_array = attributes.map do |key, value| value = case value when Hash parse_resource(value) when Array value.map { |nr| parse_resource(nr) } else value end [key, value] end transform_keys(Hash[attr_array]) end |
.parse_resource(r) ⇒ Object
30 31 32 33 |
# File 'lib/arbor/model/serialiser.rb', line 30 def parse_resource(r) nested_type = r["entityType"] nested_type ? Arbor.serialisers[nested_type].load(r) : r end |
.transform_keys(hash) ⇒ Object
35 36 37 |
# File 'lib/arbor/model/serialiser.rb', line 35 def transform_keys(hash) Hash[hash.map { |k, v| [k.underscore, v] }] end |