Class: JsonApiClient::Associations::BaseAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_client/associations/base_association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr_name, klass, options = {}) ⇒ BaseAssociation

Returns a new instance of BaseAssociation.


5
6
7
8
9
# File 'lib/json_api_client/associations/base_association.rb', line 5

def initialize(attr_name, klass, options = {})
  @attr_name = attr_name
  @klass = klass
  @options = options
end

Instance Attribute Details

#attr_nameObject (readonly)

Returns the value of attribute attr_name


4
5
6
# File 'lib/json_api_client/associations/base_association.rb', line 4

def attr_name
  @attr_name
end

#klassObject (readonly)

Returns the value of attribute klass


4
5
6
# File 'lib/json_api_client/associations/base_association.rb', line 4

def klass
  @klass
end

#optionsObject (readonly)

Returns the value of attribute options


4
5
6
# File 'lib/json_api_client/associations/base_association.rb', line 4

def options
  @options
end

Instance Method Details

#association_classObject


11
12
13
14
15
# File 'lib/json_api_client/associations/base_association.rb', line 11

def association_class
  @association_class ||= Utils.compute_type(klass, options.fetch(:class_name) do
    attr_name.to_s.classify
  end)
end

#data(url) ⇒ Object


17
18
19
# File 'lib/json_api_client/associations/base_association.rb', line 17

def data(url)
  from_result_set(association_class.requestor.linked(url))
end

#from_result_set(result_set) ⇒ Object


21
22
23
# File 'lib/json_api_client/associations/base_association.rb', line 21

def from_result_set(result_set)
  result_set.to_a
end

#load_records(data) ⇒ Object


25
26
27
28
29
30
# File 'lib/json_api_client/associations/base_association.rb', line 25

def load_records(data)
  data.map do |d|
    record_class = Utils.compute_type(klass, d["type"].classify)
    record_class.load id: d["id"]
  end
end