Class: Chronicle::Serialization::Record
- Inherits:
-
Object
- Object
- Chronicle::Serialization::Record
- Defined in:
- lib/chronicle/serialization/record.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #association_properties ⇒ Object
- #attribute_properties ⇒ Object
-
#initialize(properties: {}, type: nil, id: nil, meta: {}, schema: nil) ⇒ Record
constructor
A new instance of Record.
- #to_h ⇒ Object
- #value_to_h(v) ⇒ Object
Constructor Details
#initialize(properties: {}, type: nil, id: nil, meta: {}, schema: nil) ⇒ Record
Returns a new instance of Record.
10 11 12 13 14 15 16 |
# File 'lib/chronicle/serialization/record.rb', line 10 def initialize(properties: {}, type: nil, id: nil, meta: {}, schema: nil) @properties = properties @type = type @id = id @meta = @schema = schema end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/chronicle/serialization/record.rb', line 4 def id @id end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
4 5 6 |
# File 'lib/chronicle/serialization/record.rb', line 4 def @meta end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
4 5 6 |
# File 'lib/chronicle/serialization/record.rb', line 4 def properties @properties end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
4 5 6 |
# File 'lib/chronicle/serialization/record.rb', line 4 def schema @schema end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
4 5 6 |
# File 'lib/chronicle/serialization/record.rb', line 4 def type @type end |
Class Method Details
.build_from_model(model) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/chronicle/serialization/record.rb', line 18 def self.build_from_model(model) raise ArgumentError, 'model must be a Chronicle::Models::Base' unless model.is_a?(Chronicle::Models::Base) properties = model.properties.transform_values do |v| if v.is_a?(Array) v.map do |record| if record.is_a?(Chronicle::Models::Base) Record.build_from_model(record) else record end end elsif v.is_a?(Chronicle::Models::Base) Record.build_from_model(v) else v end end.compact new( properties:, type: model.type_id, id: model.id, meta: model., schema: :chronicle ) end |
Instance Method Details
#association_properties ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/chronicle/serialization/record.rb', line 46 def association_properties # select properties whose values are a record, or are an array that contain a record properties.select do |_k, v| if v.is_a?(Array) v.any? { |record| record.is_a?(Chronicle::Serialization::Record) } else v.is_a?(Chronicle::Serialization::Record) end end end |
#attribute_properties ⇒ Object
57 58 59 |
# File 'lib/chronicle/serialization/record.rb', line 57 def attribute_properties properties.except(*association_properties.keys) end |
#to_h ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/chronicle/serialization/record.rb', line 61 def to_h properties.transform_values do |v| if v.is_a?(Array) v.map do |item| value_to_h(item) end else value_to_h(v) end end.merge({ type:, id: }).compact end |
#value_to_h(v) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/chronicle/serialization/record.rb', line 73 def value_to_h(v) if v.is_a?(Array) v.map do |item| if item.is_a?(Chronicle::Serialization::Record) item.to_h else item end end elsif v.is_a?(Chronicle::Serialization::Record) v.to_h else v end end |