Class: JsonApiClient::IncludedData

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result_set, data) ⇒ IncludedData

Returns a new instance of IncludedData.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/json_api_client/included_data.rb', line 5

def initialize(result_set, data)
  record_class = result_set.record_class
  grouped_data = data.group_by{|datum| datum["type"]}
  @data = grouped_data.inject({}) do |h, (type, records)|
    klass = Utils.compute_type(record_class, record_class.key_formatter.unformat(type).singularize.classify)
    h[type] = records.map do |datum|
      params = klass.parser.parameters_from_resource(datum)
      resource = klass.load(params)
      resource.last_result_set = result_set
      resource
    end.index_by(&:id)
    h
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/json_api_client/included_data.rb', line 3

def data
  @data
end

Instance Method Details

#data_for(method_name, definition) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/json_api_client/included_data.rb', line 20

def data_for(method_name, definition)
  # If data is defined, pull the record from the included data
  return nil unless data = definition["data"]

  if data.is_a?(Array)
    # has_many link
    data.map do |link_def|
      record_for(link_def)
    end
  else
    # has_one link
    record_for(data)
  end
end

#has_link?(name) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/json_api_client/included_data.rb', line 35

def has_link?(name)
  data.has_key?(name.to_s)
end