Class: Cot::Collection
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Cot::Collection
- Defined in:
- lib/cot/collection.rb
Instance Method Summary collapse
- #changed? ⇒ Boolean
- #errors ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(klass, objects, options = {}) ⇒ Collection
constructor
A new instance of Collection.
- #serializable_hash ⇒ Object
- #to_json ⇒ Object
- #update_members(payload) ⇒ Object
Constructor Details
#initialize(klass, objects, options = {}) ⇒ Collection
Returns a new instance of Collection.
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/cot/collection.rb', line 3 def initialize(klass, objects, = {}) = { sub_key: } unless .is_a?(Hash) @options = .with_indifferent_access @options[:default_attributes] = {} unless @options[:default_attributes].is_a?(Hash) @klass = klass @objects = objects # If you pass in different types of things here we can't be friends initialize_objects(objects) unless objects.first.is_a? klass super @objects end |
Instance Method Details
#changed? ⇒ Boolean
37 38 39 |
# File 'lib/cot/collection.rb', line 37 def changed? @objects.map(&:changed?).include? true end |
#errors ⇒ Object
28 29 30 |
# File 'lib/cot/collection.rb', line 28 def errors Hash[@objects.reject(&:valid?).map { |x| [x.id, x.errors] }] end |
#exists? ⇒ Boolean
24 25 26 |
# File 'lib/cot/collection.rb', line 24 def exists? @objects.map(&:exists?).all? end |
#serializable_hash ⇒ Object
16 17 18 |
# File 'lib/cot/collection.rb', line 16 def serializable_hash @objects.map(&:serializable_hash) end |
#to_json ⇒ Object
20 21 22 |
# File 'lib/cot/collection.rb', line 20 def to_json serializable_hash.to_json end |
#update_members(payload) ⇒ Object
32 33 34 35 |
# File 'lib/cot/collection.rb', line 32 def update_members(payload) # NOTE: replacing objects is lazy, but I don't want to deal with updating and such right now initialize_objects(payload) end |