Class: Chronicle::Serialization::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/chronicle/serialization/record.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = meta
  @schema = schema
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/chronicle/serialization/record.rb', line 4

def id
  @id
end

#metaObject (readonly)

Returns the value of attribute meta.



4
5
6
# File 'lib/chronicle/serialization/record.rb', line 4

def meta
  @meta
end

#propertiesObject (readonly)

Returns the value of attribute properties.



4
5
6
# File 'lib/chronicle/serialization/record.rb', line 4

def properties
  @properties
end

#schemaObject (readonly)

Returns the value of attribute schema.



4
5
6
# File 'lib/chronicle/serialization/record.rb', line 4

def schema
  @schema
end

#typeObject (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

Raises:

  • (ArgumentError)


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.meta,
    schema: :chronicle
  )
end

Instance Method Details

#association_propertiesObject



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_propertiesObject



57
58
59
# File 'lib/chronicle/serialization/record.rb', line 57

def attribute_properties
  properties.except(*association_properties.keys)
end

#to_hObject



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