Class: LHS::Data

Inherits:
Object
  • Object
show all
Includes:
Becomes, Equality, Json, ToHash, Inspect
Defined in:
lib/lhs/data.rb,
lib/lhs/concerns/data/json.rb,
lib/lhs/concerns/data/becomes.rb,
lib/lhs/concerns/data/to_hash.rb,
lib/lhs/concerns/data/equality.rb

Overview

Data provides functionalities to accesses information

Defined Under Namespace

Modules: Becomes, Equality, Json, ToHash

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspect

#inspect

Methods included from Json

#as_json

Methods included from Equality

#==

Methods included from Becomes

#becomes

Constructor Details

#initialize(input, parent = nil, record = nil, request = nil, endpoint = nil) ⇒ Data

Returns a new instance of Data.



26
27
28
29
30
31
32
33
# File 'lib/lhs/data.rb', line 26

def initialize(input, parent = nil, record = nil, request = nil, endpoint = nil)
  self._raw = raw_from_input(input)
  self._parent = parent
  self._record = record
  self._proxy = proxy_from_input(input)
  self._request = request
  self._endpoint = endpoint
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



89
90
91
# File 'lib/lhs/data.rb', line 89

def method_missing(name, *args, &block)
  _proxy.send(name, *args, &block)
end

Instance Attribute Details

#_endpointObject

prevent clashing with attributes of underlying data



23
24
25
# File 'lib/lhs/data.rb', line 23

def _endpoint
  @_endpoint
end

#_parentObject

prevent clashing with attributes of underlying data



23
24
25
# File 'lib/lhs/data.rb', line 23

def _parent
  @_parent
end

#_proxyObject

prevent clashing with attributes of underlying data



23
24
25
# File 'lib/lhs/data.rb', line 23

def _proxy
  @_proxy
end

#_rawObject

Returns the value of attribute _raw.



24
25
26
# File 'lib/lhs/data.rb', line 24

def _raw
  @_raw
end

#_recordObject

prevent clashing with attributes of underlying data



23
24
25
# File 'lib/lhs/data.rb', line 23

def _record
  @_record
end

#_requestObject

prevent clashing with attributes of underlying data



23
24
25
# File 'lib/lhs/data.rb', line 23

def _request
  @_request
end

Instance Method Details

#_rootObject



51
52
53
54
55
# File 'lib/lhs/data.rb', line 51

def _root
  root = self
  root = root._parent while root&._parent
  root
end

#classObject



65
66
67
# File 'lib/lhs/data.rb', line 65

def class
  _record || _root._record
end

#collection?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/lhs/data.rb', line 79

def collection?
  _proxy.is_a? LHS::Collection
end

#item?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/lhs/data.rb', line 83

def item?
  _proxy.is_a? LHS::Item
end

#merge_raw!(data) ⇒ Object

merging data e.g. when loading remote data via link



37
38
39
40
# File 'lib/lhs/data.rb', line 37

def merge_raw!(data)
  return false if data.blank? || !data._raw.is_a?(Hash)
  _raw.merge! data._raw
end

#parentObject



57
58
59
60
61
62
63
# File 'lib/lhs/data.rb', line 57

def parent
  if _parent&._record
    _parent._record.new(_parent, false)
  else
    _parent
  end
end

#root_item?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/lhs/data.rb', line 75

def root_item?
  root_item == self
end

#unwrap(usecase) ⇒ Object

Unwraps data for certain use cases like items_created_key for CREATE, UPDATED etc. like item_key for GET etc.



45
46
47
48
49
# File 'lib/lhs/data.rb', line 45

def unwrap(usecase)
  nested_path = record.send(usecase) if record
  return LHS::Data.new(dig(*nested_path) || _raw, nil, self.class) if nested_path
  self
end