class DHS::Data

Data provides functionalities to accesses information

Attributes

_endpoint[RW]

prevent clashing with attributes of underlying data

_parent[RW]

prevent clashing with attributes of underlying data

_proxy[RW]

prevent clashing with attributes of underlying data

_raw[R]
_record[RW]

prevent clashing with attributes of underlying data

_request[RW]

prevent clashing with attributes of underlying data

Public Class Methods

new(input, parent = nil, record = nil, request = nil, endpoint = nil) click to toggle source
# File lib/dhs/data.rb, line 29
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
  preserve_input_requests!(input)
end

Public Instance Methods

_raw=(raw) click to toggle source

enforce internal data structure to have deep symbolized keys

# File lib/dhs/data.rb, line 74
def _raw=(raw)
  raw.to_hash.deep_symbolize_keys! if raw&.respond_to?(:to_hash)
  @_raw = raw
end
_root() click to toggle source
# File lib/dhs/data.rb, line 55
def _root
  root = self
  root = root._parent while root&._parent
  root
end
class() click to toggle source
# File lib/dhs/data.rb, line 69
def class
  _record || _root._record
end
collection?() click to toggle source
# File lib/dhs/data.rb, line 83
def collection?
  _proxy.is_a? DHS::Collection
end
item?() click to toggle source
# File lib/dhs/data.rb, line 87
def item?
  _proxy.is_a? DHS::Item
end
merge_raw!(data) click to toggle source

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

# File lib/dhs/data.rb, line 41
def merge_raw!(data)
  return false if data.blank? || !data._raw.is_a?(Hash)
  _raw.merge! data._raw
end
parent() click to toggle source
# File lib/dhs/data.rb, line 61
def parent
  if _parent&._record
    _parent._record.new(_parent, false)
  else
    _parent
  end
end
root_item?() click to toggle source
# File lib/dhs/data.rb, line 79
def root_item?
  root_item == self
end
unwrap(usecase) click to toggle source

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

# File lib/dhs/data.rb, line 49
def unwrap(usecase)
  nested_path = record.send(usecase) if record
  return DHS::Data.new(dig(*nested_path) || _raw, nil, self.class) if nested_path
  self
end

Protected Instance Methods

method_missing(name, *args, **keyword_args, &block) click to toggle source
# File lib/dhs/data.rb, line 93
def method_missing(name, *args, **keyword_args, &block)
  _proxy.send(name, *args, **keyword_args, &block)
end
respond_to_missing?(name, include_all = false) click to toggle source
# File lib/dhs/data.rb, line 97
def respond_to_missing?(name, include_all = false)
  (root_item? && instance_methods.include?(name)) ||
    _proxy.respond_to?(name, include_all)
end

Private Instance Methods

collection_proxy?(input) click to toggle source
# File lib/dhs/data.rb, line 115
def collection_proxy?(input)
  (input.is_a?(Hash) && DHS::Collection.access(input: input, record: _record)) ||
    input.is_a?(Array) ||
    _raw.is_a?(Array)
end
json?(input) click to toggle source
# File lib/dhs/data.rb, line 160
def json?(input)
  input.is_a?(String) && !input.empty? && !!input.match(/^("|\[|'|\{)/)
end
preserve_input_requests!(input) click to toggle source
# File lib/dhs/data.rb, line 104
def preserve_input_requests!(input)
  if input.is_a?(DHS::Data)
    _request = input._request if _request.nil?
  elsif input.is_a?(Array) && input.first.is_a?(DHS::Data)
    input.each_with_index do |item, index|
      next if item.nil?
      self[index]._request = item._request
    end
  end
end
proxy_from_input(input) click to toggle source
# File lib/dhs/data.rb, line 138
def proxy_from_input(input)
  if input.is_a? DHS::Proxy
    input
  elsif collection_proxy?(raw_from_input(input))
    DHS::Collection.new(self)
  else
    DHS::Item.new(self)
  end
end
raw_from_anything_else(input) click to toggle source
# File lib/dhs/data.rb, line 175
def raw_from_anything_else(input)
  input = input.to_hash if input.class != Hash && input.respond_to?(:to_hash)
  input.deep_symbolize_keys! if input.is_a?(Hash)
  input
end
raw_from_input(input) click to toggle source
# File lib/dhs/data.rb, line 148
def raw_from_input(input)
  if json?(input)
    raw_from_json_string(input)
  elsif defined?(input._raw)
    input._raw
  elsif defined?(input._data)
    input._data._raw
  else
    raw_from_anything_else(input)
  end
end
raw_from_json_string(input) click to toggle source
# File lib/dhs/data.rb, line 164
def raw_from_json_string(input)
  json = JSON.parse(input)
  if json.is_a?(Hash)
    json.deep_symbolize_keys
  elsif json.is_a?(Array)
    json.map { |item| item.is_a?(Hash) ? item.deep_symbolize_keys : item }
  else
    json
  end
end
root?() click to toggle source
# File lib/dhs/data.rb, line 134
def root?
  _root == self
end
root_item() click to toggle source
# File lib/dhs/data.rb, line 121
def root_item
  return if _proxy.class != DHS::Item
  root = root_item = self
  loop do
    root = root._parent
    root_item = root if root && root._proxy.is_a?(DHS::Item)
    unless root&._parent
      break
    end
  end
  root_item
end