Class: Dato::Local::JsonApiEntity

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/local/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.



10
11
12
13
# File 'lib/dato/local/json_api_entity.rb', line 10

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)



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dato/local/json_api_entity.rb', line 72

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.



8
9
10
# File 'lib/dato/local/json_api_entity.rb', line 8

def data_source
  @data_source
end

#payloadObject (readonly)

Returns the value of attribute payload.



8
9
10
# File 'lib/dato/local/json_api_entity.rb', line 8

def payload
  @payload
end

Instance Method Details

#==(other) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/dato/local/json_api_entity.rb', line 27

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

#[](key) ⇒ Object



40
41
42
# File 'lib/dato/local/json_api_entity.rb', line 40

def [](key)
  attributes[key.to_sym]
end

#idObject



15
16
17
# File 'lib/dato/local/json_api_entity.rb', line 15

def id
  @payload[:id]
end

#metaObject



23
24
25
# File 'lib/dato/local/json_api_entity.rb', line 23

def meta
  @meta ||= JsonApiMeta.new(@payload[:meta])
end

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

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/dato/local/json_api_entity.rb', line 44

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

#to_sObject Also known as: inspect



35
36
37
# File 'lib/dato/local/json_api_entity.rb', line 35

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

#typeObject



19
20
21
# File 'lib/dato/local/json_api_entity.rb', line 19

def type
  @payload[:type]
end