Class: ArtemisApi::Model
- Inherits:
-
Object
- Object
- ArtemisApi::Model
- Defined in:
- lib/artemis_api/model.rb
Direct Known Subclasses
Batch, Completion, Discard, Facility, Harvest, HarvestUnit, Item, Organization, SeedingUnit, 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.
-
#relationships ⇒ Object
readonly
Returns the value of attribute relationships.
Class Method Summary collapse
- .instance_for(type, data, client) ⇒ Object
- .json_type(type = nil) ⇒ Object
- .related_to_many(name) ⇒ Object
- .related_to_one(name) ⇒ Object
Instance Method Summary collapse
-
#initialize(client, data) ⇒ Model
constructor
A new instance of Model.
- #method_missing(name) ⇒ Object
Constructor Details
#initialize(client, data) ⇒ Model
Returns a new instance of Model.
35 36 37 38 39 40 |
# File 'lib/artemis_api/model.rb', line 35 def initialize(client, data) @client = client @id = data['id'].to_i @attributes = data['attributes'] @relationships = data['relationships'] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
31 32 33 |
# File 'lib/artemis_api/model.rb', line 31 def method_missing(name) attributes[name.to_s] 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 |
#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, client) ⇒ Object
27 28 29 |
# File 'lib/artemis_api/model.rb', line 27 def self.instance_for(type, data, client) @@registered_classes[type]&.new(client, data) end |
.json_type(type = nil) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/artemis_api/model.rb', line 18 def self.json_type(type = nil) if type @json_type = type @@registered_classes ||= {} @@registered_classes[type] = self end @json_type end |
.related_to_many(name) ⇒ Object
12 13 14 15 16 |
# File 'lib/artemis_api/model.rb', line 12 def self.(name) self.send(:define_method, name.to_sym) do @client.find_all(self.relationships[name.to_s]['data']['type']) end end |
.related_to_one(name) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/artemis_api/model.rb', line 5 def self.(name) self.send(:define_method, name.to_sym) do relationship = relationships[name.to_s]['data'] @client.find_one(relationship['type'], relationship['id']) end end |