Class: ArtemisApi::Model
- Inherits:
-
Object
- Object
- ArtemisApi::Model
- Defined in:
- lib/artemis_api/model.rb
Direct Known Subclasses
Batch, Completion, CropBatchState, CropVariety, CustomData, CustomFields, Discard, Facility, Harvest, HarvestUnit, Item, Organization, ResourceUnit, SeedingUnit, Stage, SubStage, Subscription, User, Zone
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#included ⇒ Object
readonly
Returns the value of attribute included.
-
#relationships ⇒ Object
readonly
Returns the value of attribute relationships.
Class Method Summary collapse
- .instance_for(type, data, included, client) ⇒ Object
- .json_type(type = nil) ⇒ Object
- .related_to_many(name) ⇒ Object
- .related_to_one(name) ⇒ Object
- .relationships ⇒ Object
Instance Method Summary collapse
-
#initialize(client, data, included = nil) ⇒ Model
constructor
A new instance of Model.
- #inspect ⇒ Object
- #method_missing(name) ⇒ Object
- #respond_to_missing?(name) ⇒ Boolean
Constructor Details
#initialize(client, data, included = nil) ⇒ Model
Returns a new instance of Model.
71 72 73 74 75 76 77 |
# File 'lib/artemis_api/model.rb', line 71 def initialize(client, data, included = nil) @client = client @id = data['id'].to_i @attributes = data['attributes'] @relationships = data['relationships'] @included = included end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
63 64 65 |
# File 'lib/artemis_api/model.rb', line 63 def method_missing(name) respond_to_missing?(name) ? attributes[name.to_s] : super end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/artemis_api/model.rb', line 3 def attributes @attributes end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/artemis_api/model.rb', line 3 def client @client end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/artemis_api/model.rb', line 3 def id @id end |
#included ⇒ Object (readonly)
Returns the value of attribute included.
3 4 5 |
# File 'lib/artemis_api/model.rb', line 3 def included @included end |
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
3 4 5 |
# File 'lib/artemis_api/model.rb', line 3 def relationships @relationships end |
Class Method Details
.instance_for(type, data, included, client) ⇒ Object
48 49 50 |
# File 'lib/artemis_api/model.rb', line 48 def instance_for(type, data, included, client) @@registered_classes[type]&.new(client, data, included) end |
.json_type(type = nil) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/artemis_api/model.rb', line 39 def json_type(type = nil) if type @json_type = type @@registered_classes ||= {} @@registered_classes[type] = self end @json_type end |
.related_to_many(name) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/artemis_api/model.rb', line 22 def (name) register_relationship(name) send(:define_method, name.to_sym) do included = relationships.dig(name.to_s, 'data')&.map do || client.get_record(name.to_s, ['id']) end return included if included&.present? @client.find_all( relationships.dig(name.to_s, 'data', 0, 'type') || name.to_s, filters: { name => id } ) end end |
.related_to_one(name) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/artemis_api/model.rb', line 6 def (name) register_relationship(name) send(:define_method, name.to_sym) do = relationships.dig(name.to_s, 'data', 'id') included = client.get_record(name.to_s, ) return included if included&.present? relationship = relationships.dig(name.to_s, 'data') return if relationship.nil? @client.find_one(relationship['type'], relationship['id']) unless relationship['id'].to_s.empty? || relationship['id'].nil? end end |
.relationships ⇒ Object
52 53 54 |
# File 'lib/artemis_api/model.rb', line 52 def relationships @relationships ||= [] end |
Instance Method Details
#inspect ⇒ Object
79 80 81 82 83 |
# File 'lib/artemis_api/model.rb', line 79 def inspect vars = %i[id attributes].map { |v| "#{v}=#{send(v).inspect}" }.join(', ') vars << ", relationships={#{self.class.relationships.join(', ')}}" "<#{self.class}: #{vars}>" end |
#respond_to_missing?(name) ⇒ Boolean
67 68 69 |
# File 'lib/artemis_api/model.rb', line 67 def respond_to_missing?(name) attributes.key?(name.to_s) end |