Class: DHS::Collection::InternalCollection::Collection
- Inherits:
-
Object
- Object
- DHS::Collection::InternalCollection::Collection
- Includes:
- Enumerable
- Defined in:
- lib/dhs/concerns/collection/internal_collection.rb
Overview
The internal collection class that includes enumerable and insures to return DHS::Items in case of iterating items
Instance Attribute Summary collapse
-
#raw ⇒ Object
Returns the value of attribute raw.
Instance Method Summary collapse
- #compact ⇒ Object
- #compact! ⇒ Object
- #each(&_block) ⇒ Object
-
#initialize(raw, parent, record) ⇒ Collection
constructor
A new instance of Collection.
- #to_ary ⇒ Object
Constructor Details
#initialize(raw, parent, record) ⇒ Collection
Returns a new instance of Collection.
20 21 22 23 24 |
# File 'lib/dhs/concerns/collection/internal_collection.rb', line 20 def initialize(raw, parent, record) self.raw = raw @parent = parent @record = record end |
Instance Attribute Details
#raw ⇒ Object
Returns the value of attribute raw.
15 16 17 |
# File 'lib/dhs/concerns/collection/internal_collection.rb', line 15 def raw @raw end |
Instance Method Details
#compact ⇒ Object
40 41 42 43 44 |
# File 'lib/dhs/concerns/collection/internal_collection.rb', line 40 def compact dup.tap do |collection| collection.compact! if collection.raw.present? end.as_json # do not return an internal collection! end |
#compact! ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/dhs/concerns/collection/internal_collection.rb', line 46 def compact! self.raw = raw.map do |item| if item.is_a?(DHS::Data) && item._request && !item._request.response.success? nil else cast_item(item) end end.compact end |
#each(&_block) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/dhs/concerns/collection/internal_collection.rb', line 30 def each(&_block) raw.each do |item| if item.is_a? Hash yield cast_item(item) else yield item end end end |
#to_ary ⇒ Object
26 27 28 |
# File 'lib/dhs/concerns/collection/internal_collection.rb', line 26 def to_ary map { |item| item } end |