Class: LHS::Collection

Inherits:
Proxy
  • Object
show all
Includes:
Create, InternalCollection
Defined in:
lib/lhs/collection.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: InternalCollection

Constant Summary collapse

METHOD_NAMES_EXLCUDED_FROM_WRAPPING =
%w(to_a 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::Errors

#errors, #initialize

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)



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lhs/collection.rb', line 50

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]
    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)
  elsif _data._raw.is_a?(Hash)
    get(name, *args)
  end
end

Instance Method Details

#_collectionObject



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

def _collection
  raw = _data._raw if _data._raw.is_a?(Array)
  raw ||= _data._raw[items_key]
  Collection.new(raw, _data, _record)
end

#_paginationObject



17
18
19
# File 'lib/lhs/collection.rb', line 17

def _pagination
  _record.pagination(_data)
end

#collection?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/lhs/collection.rb', line 32

def collection?
  true
end

#hrefObject



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

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

#item?Boolean

Returns:

  • (Boolean)


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

def item?
  false
end

#raw_itemsObject



40
41
42
43
44
45
46
# File 'lib/lhs/collection.rb', line 40

def raw_items
  if _raw.is_a?(Array)
    _raw
  else
    _raw[items_key]
  end
end