Class: Cot::Collection

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

Instance Attribute Summary

Attributes included from CollectionClassMethods

#klass, #options

Instance Method Summary collapse

Methods included from CollectionClassMethods

collected_class, set_default_values

Constructor Details

#initialize(*params) ⇒ Collection

We take an array of params here and then parse them due to backwards compat crap Collection can take 1-3 parameters and there are two cases when it gets two 3: klass, objects, options 2: klass, objects 2: objects, options 1: objects



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cot/collection.rb', line 11

def initialize(*params)
  parse_params(params)

  if self.class.klass.nil?
    fail 'Collected class not set, please either pass a class to initialize or call collected_class'
  end
  # If you pass in different types of things here we can't be friends
  initialize_objects(@objects) unless @objects.first.instance_of? self.class.klass

  super @objects
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/cot/collection.rb', line 44

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

#errorsObject



35
36
37
# File 'lib/cot/collection.rb', line 35

def errors
  Hash[reject(&:valid?).map { |x| [x.id, x.errors] }]
end

#exists?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/cot/collection.rb', line 31

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

#serializable_hashObject



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

def serializable_hash
  map(&:serializable_hash)
end

#to_jsonObject



27
28
29
# File 'lib/cot/collection.rb', line 27

def to_json
  serializable_hash.to_json
end

#update_members(payload) ⇒ Object



39
40
41
42
# File 'lib/cot/collection.rb', line 39

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