Class: ArtemisApi::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/artemis_api/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/artemis_api/model.rb', line 3

def attributes
  @attributes
end

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/artemis_api/model.rb', line 3

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/artemis_api/model.rb', line 3

def id
  @id
end

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


12
13
14
15
16
# File 'lib/artemis_api/model.rb', line 12

def self.related_to_many(name)
  self.send(:define_method, name.to_sym) do
    @client.find_all(self.relationships[name.to_s]['data']['type'])
  end
end


5
6
7
8
9
10
# File 'lib/artemis_api/model.rb', line 5

def self.related_to_one(name)
  self.send(:define_method, name.to_sym) do
    relationship = relationships[name.to_s]['data']
    @client.find_one(relationship['type'], relationship['id'])
  end
end