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



3
4
5
6
7
8
# 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']&.then { |rd| convert_relationships(rd) } || {}
  build_references
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



55
56
57
58
59
60
61
62
63
64
# File 'lib/ja2r/element.rb', line 55

def method_missing(symbol, *args)
  case @references[symbol]
  in attribute:
    attributes[attribute]
  in relationship:
    relationships[relationship]
  else
    safe_traverse? ? nil : super
  end
end

Instance Attribute Details

#origin_dataObject (readonly) Also known as: serializable_hash

Returns the value of attribute origin_data.



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

def origin_data
  @origin_data
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



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

def relationships
  @relationships
end

Instance Method Details

#attribute(key) ⇒ Object



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

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

#attributesObject



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

def attributes
  origin_data['attributes']
end

#idObject



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

def id
  origin_data['id']
end


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

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

#meta(key) ⇒ Object



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

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

#typeObject



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

def type
  origin_data['type']
end