Class: JA2R::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/ja2r/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin_data, options = {}) ⇒ Element

Returns a new instance of Element.



3
4
5
6
7
# File 'lib/ja2r/element.rb', line 3

def initialize(origin_data, options = {})
  @origin_data = origin_data.with_indifferent_access
  @options = options
  @relationships = origin_data['relationships'] ? convert_relationships(origin_data['relationships']) : {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object (private)



42
43
44
45
46
47
# File 'lib/ja2r/element.rb', line 42

def method_missing(symbol, *args)
  return attributes[symbol] if attributes&.key? symbol
  return relationships[symbol] if relationships&.key? symbol

  safe_traverse? ? nil : super
end

Instance Attribute Details

#origin_dataObject (readonly) Also known as: serializable_hash

Returns the value of attribute origin_data.



9
10
11
# File 'lib/ja2r/element.rb', line 9

def origin_data
  @origin_data
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



9
10
11
# File 'lib/ja2r/element.rb', line 9

def relationships
  @relationships
end

Instance Method Details

#attribute(key) ⇒ Object



11
12
13
# File 'lib/ja2r/element.rb', line 11

def attribute(key)
  origin_data.dig 'attributes', key
end

#attributesObject



15
16
17
# File 'lib/ja2r/element.rb', line 15

def attributes
  origin_data['attributes']
end

#idObject



19
20
21
# File 'lib/ja2r/element.rb', line 19

def id
  origin_data['id']
end


23
24
25
# File 'lib/ja2r/element.rb', line 23

def link(key)
  origin_data.dig 'links', key
end

#meta(key) ⇒ Object



27
28
29
# File 'lib/ja2r/element.rb', line 27

def meta(key)
  origin_data.dig 'meta', key
end

#typeObject



36
37
38
# File 'lib/ja2r/element.rb', line 36

def type
  origin_data['type']
end