Class: Cot::Collection

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/cot/collection.rb

Instance Method Summary collapse

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, options = {})
  options = { sub_key: options } unless options.is_a?(Hash)
  @options = 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

Returns:

  • (Boolean)


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

def changed?
  @objects.map(&:changed?).include? true
end

#errorsObject



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

Returns:

  • (Boolean)


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

def exists?
  @objects.map(&:exists?).all?
end

#serializable_hashObject



16
17
18
# File 'lib/cot/collection.rb', line 16

def serializable_hash
  @objects.map(&:serializable_hash)
end

#to_jsonObject



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