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!

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)



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

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



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

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



20
21
22
# File 'lib/lhs/collection.rb', line 20

def _pagination
  _record.pagination(_data)
end

#collection?Boolean

Returns:

  • (Boolean)


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

def collection?
  true
end

#hrefObject



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

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

#item?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/lhs/collection.rb', line 41

def item?
  false
end

#raw_itemsObject



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

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