class DHS::Collection::InternalCollection::Collection
The internal collection class that includes enumerable and insures to return DHS::Items in case of iterating items
Attributes
raw[RW]
Public Class Methods
new(raw, parent, record)
click to toggle source
# File lib/dhs/concerns/collection/internal_collection.rb, line 20 def initialize(raw, parent, record) self.raw = raw @parent = parent @record = record end
Public Instance Methods
compact()
click to toggle source
# 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!()
click to toggle source
# 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() { |cast_item(item)| ... }
click to toggle source
# 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()
click to toggle source
# File lib/dhs/concerns/collection/internal_collection.rb, line 26 def to_ary map { |item| item } end
Private Instance Methods
cast_item(item)
click to toggle source
# File lib/dhs/concerns/collection/internal_collection.rb, line 58 def cast_item(item) data = DHS::Data.new(item, @parent, @record) (record_by_href(item) || @record)&.new(data) || data end
plain_value?(item)
click to toggle source
# File lib/dhs/concerns/collection/internal_collection.rb, line 69 def plain_value?(item) item.is_a?(String) || item.is_a?(Numeric) || item.is_a?(TrueClass) || item.is_a?(FalseClass) end
record_by_href(item)
click to toggle source
# File lib/dhs/concerns/collection/internal_collection.rb, line 63 def record_by_href(item) return if plain_value?(item) || item[:href].blank? DHS::Record.for_url(item[:href]) end