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, 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

#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

#includedObject (readonly)

Returns the value of attribute included.



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

def included
  @included
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, 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


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 related_to_many(name)
  register_relationship(name)

  send(:define_method, name.to_sym) do
    included = relationships.dig(name.to_s, 'data')&.map do |related|
      client.get_record(name.to_s, related['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


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/artemis_api/model.rb', line 6

def related_to_one(name)
  register_relationship(name)

  send(:define_method, name.to_sym) do
    related_id = relationships.dig(name.to_s, 'data', 'id')
    included = client.get_record(name.to_s, related_id)

    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

.relationshipsObject



52
53
54
# File 'lib/artemis_api/model.rb', line 52

def relationships
  @relationships ||= []
end

Instance Method Details

#inspectObject



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

Returns:

  • (Boolean)


67
68
69
# File 'lib/artemis_api/model.rb', line 67

def respond_to_missing?(name)
  attributes.key?(name.to_s)
end