Class: LHS::Collection

Inherits:
Proxy
  • Object
show all
Includes:
Create, HandleNested, InternalCollection
Defined in:
lib/lhs/collection.rb,
lib/lhs/concerns/collection/handle_nested.rb,
lib/lhs/concerns/collection/internal_collection.rb

Overview

A collection is a special type of data that contains multiple items

Defined Under Namespace

Modules: HandleNested, InternalCollection

Constant Summary collapse

METHOD_NAMES_EXLCUDED_FROM_WRAPPING =
%w(to_a to_ary map).freeze

Constants included from Proxy::Accessors

Proxy::Accessors::BLACKLISTED_KEYWORDS

Instance Attribute Summary

Attributes inherited from Proxy

#_data, #_loaded

Instance Method Summary collapse

Methods inherited from Proxy

#initialize, #load!, #record, #reload!

Methods included from Proxy::Create

#create, #create!

Methods included from Proxy::Accessors

#clear_cache!

Constructor Details

This class inherits a constructor from LHS::Proxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lhs/collection.rb', line 57

def method_missing(name, *args, &block)
  if _collection.respond_to?(name)
    value = _collection.send(name, *args, &block)
    record = LHS::Record.for_url(value[:href]) if value.is_a?(Hash) && value[:href]
    record ||= _record
    value = enclose_item_in_data(value) if value.is_a?(Hash)
    return value if METHOD_NAMES_EXLCUDED_FROM_WRAPPING.include?(name.to_s)
    wrap_return(value, record, name, args)
  elsif _data._raw.is_a?(Hash)
    get(name, *args)
  end
end

Instance Method Details

#_collectionObject


31
32
33
34
35
36
37
# File 'lib/lhs/collection.rb', line 31

def _collection
  @_collection ||= begin
    raw = _data._raw if _data._raw.is_a?(Array)
    raw ||= _data.access(input: _data._raw, record: _record)
    Collection.new(raw, _data, _record)
  end
end

#_paginationObject


22
23
24
# File 'lib/lhs/collection.rb', line 22

def _pagination
  _record.pagination(_data)
end

#collection?Boolean

Returns:

  • (Boolean)

39
40
41
# File 'lib/lhs/collection.rb', line 39

def collection?
  true
end

#hrefObject


26
27
28
29
# File 'lib/lhs/collection.rb', line 26

def href
  return _data._raw[:href] if _data._raw.is_a? Hash
  nil
end

#item?Boolean

Returns:

  • (Boolean)

43
44
45
# File 'lib/lhs/collection.rb', line 43

def item?
  false
end

#raw_itemsObject


47
48
49
50
51
52
53
# File 'lib/lhs/collection.rb', line 47

def raw_items
  if _raw.is_a?(Array)
    _raw
  else
    access(input: _raw, record: _record)
  end
end