Class: DatoDump::JsonApiEntity

Inherits:
Object
  • Object
show all
Defined in:
lib/dato_dump/json_api_entity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload, data_source) ⇒ JsonApiEntity

Returns a new instance of JsonApiEntity.



5
6
7
8
# File 'lib/dato_dump/json_api_entity.rb', line 5

def initialize(payload, data_source)
  @payload = payload
  @data_source = data_source
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object (private)



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dato_dump/json_api_entity.rb', line 63

def method_missing(method, *arguments, &block)
  return super unless arguments.empty?

  if attributes.key?(method)
    attributes[method]
  elsif relationships.key?(method)
    dereference_linkage(relationships[method][:data])
  else
    super
  end
end

Instance Attribute Details

#data_sourceObject (readonly)

Returns the value of attribute data_source.



3
4
5
# File 'lib/dato_dump/json_api_entity.rb', line 3

def data_source
  @data_source
end

#payloadObject (readonly)

Returns the value of attribute payload.



3
4
5
# File 'lib/dato_dump/json_api_entity.rb', line 3

def payload
  @payload
end

Instance Method Details

#==(other) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/dato_dump/json_api_entity.rb', line 18

def ==(other)
  if other.is_a? JsonApiEntity
    id == other.id && type == other.type
  else
    false
  end
end

#[](key) ⇒ Object



31
32
33
# File 'lib/dato_dump/json_api_entity.rb', line 31

def [](key)
  attributes[key]
end

#attributesObject



43
44
45
# File 'lib/dato_dump/json_api_entity.rb', line 43

def attributes
  @payload.fetch(:attributes, {})
end

#idObject



10
11
12
# File 'lib/dato_dump/json_api_entity.rb', line 10

def id
  @payload[:id]
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/dato_dump/json_api_entity.rb', line 35

def respond_to?(method, include_private = false)
  if attributes.key?(method) || relationships.key?(method)
    true
  else
    super
  end
end

#to_sObject Also known as: inspect



26
27
28
# File 'lib/dato_dump/json_api_entity.rb', line 26

def to_s
  "#<JsonApiEntity id=#{id} type=#{type} payload=#{payload}>"
end

#typeObject



14
15
16
# File 'lib/dato_dump/json_api_entity.rb', line 14

def type
  @payload[:type]
end