Class: Chronicle::Models::Base
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- Chronicle::Models::Base
- Defined in:
- lib/chronicle/models/base.rb
Overview
The base class for all generated models
Class Attribute Summary collapse
-
.type_id ⇒ Object
readonly
Returns the value of attribute type_id.
Class Method Summary collapse
-
.many_cardinality_attributes ⇒ Object
FIXME: this isn’t working.
- .new(attributes = {}) ⇒ Object
- .one_cardinality_attributes ⇒ Object
Instance Method Summary collapse
- #meta ⇒ Object
- #properties ⇒ Object
- #to_h ⇒ Object
-
#to_h_flattened ⇒ Object
TODO: exclude dedupe_on from serialization.
- #type_id ⇒ Object
Class Attribute Details
.type_id ⇒ Object (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_attributes ⇒ Object
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.[: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. end |
.one_cardinality_attributes ⇒ Object
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
#meta ⇒ Object
52 53 54 55 56 |
# File 'lib/chronicle/models/base.rb', line 52 def output = {} output[:dedupe_on] = dedupe_on unless dedupe_on.empty? output end |
#properties ⇒ Object
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_h ⇒ Object
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_flattened ⇒ Object
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_id ⇒ Object
58 59 60 |
# File 'lib/chronicle/models/base.rb', line 58 def type_id self.class.type_id end |