Class: EntitySchema::Fields::Object

Inherits:
Abstract
  • Object
show all
Defined in:
lib/entity_schema/fields/object.rb

Overview

Associated object

Direct Known Subclasses

Collection, ObjectBelongsTo

Instance Attribute Summary

Attributes inherited from Abstract

#name, #specification, #src_key

Instance Method Summary collapse

Methods inherited from Abstract

#given?, #public_get, #public_set, #set

Constructor Details

#initialize(specification) ⇒ Object

Returns a new instance of Object.



9
10
11
12
13
# File 'lib/entity_schema/fields/object.rb', line 9

def initialize(specification)
  @mapper     = specification[:mapper]
  @serializer = specification[:serializer]
  super(specification)
end

Instance Method Details

#get(obj) ⇒ Object



15
16
17
18
# File 'lib/entity_schema/fields/object.rb', line 15

def get(obj)
  value = read(obj)
  value.is_a?(Hash) ? write(obj, map(value)) : value
end

#serialize(obj, output) ⇒ Object



20
21
22
23
24
25
# File 'lib/entity_schema/fields/object.rb', line 20

def serialize(obj, output)
  return unless given?(obj)
  value = read(obj)
  return output[src_key] = value if value.is_a?(Hash)
  output[src_key] = unwrap(value)
end