Class: Chronicle::Models::Base

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/chronicle/models/base.rb

Overview

The base class for all generated models

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.type_idObject (readonly)

Returns the value of attribute type_id.



12
13
14
# File 'lib/chronicle/models/base.rb', line 12

def type_id
  @type_id
end

Class Method Details

.many_cardinality_attributesObject

FIXME: this isn’t working



75
76
77
78
79
# File 'lib/chronicle/models/base.rb', line 75

def self.many_cardinality_attributes
  schema.type.select do |type|
    type.meta[:many]
  end.map(&:name)
end

.new(attributes = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chronicle/models/base.rb', line 37

def self.new(attributes = {})
  if block_given?
    attribute_struct = Struct.new(*schema.map(&:name)).new
    attribute_struct.dedupe_on = []

    yield(attribute_struct)
    attributes = attribute_struct.to_h
  end

  super(attributes)
rescue Dry::Struct::Error, NoMethodError => e
  # TODO: be more clear about the attribute that's invalid
  raise AttributeError, e.message
end

.one_cardinality_attributesObject



81
82
83
# File 'lib/chronicle/models/base.rb', line 81

def self.one_cardinality_attributes
  schema.type.map(&:name) - many_cardinality_attributes
end

Instance Method Details

#metaObject



52
53
54
55
56
# File 'lib/chronicle/models/base.rb', line 52

def meta
  output = {}
  output[:dedupe_on] = dedupe_on unless dedupe_on.empty?
  output
end

#propertiesObject



15
16
17
18
# File 'lib/chronicle/models/base.rb', line 15

def properties
  # FIXME: think about this more. Does dedupe belong in serialization
  attributes.except(:dedupe_on)
end

#to_hObject



62
63
64
65
66
# File 'lib/chronicle/models/base.rb', line 62

def to_h
  super.merge({ type: type_id })
    .except(:dedupe_on)
    .compact # TODO: don't know about this one
end

#to_h_flattenedObject

TODO: exclude dedupe_on from serialization



69
70
71
72
# File 'lib/chronicle/models/base.rb', line 69

def to_h_flattened
  require 'chronicle/utils/hash_utils'
  Chronicle::Utils::HashUtils.flatten_hash(to_h)
end

#type_idObject



58
59
60
# File 'lib/chronicle/models/base.rb', line 58

def type_id
  self.class.type_id
end