class Cot::Collection
Public Class Methods
new(*params)
click to toggle source
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
Calls superclass method
# 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
Public Instance Methods
changed?()
click to toggle source
# File lib/cot/collection.rb, line 44 def changed? map(&:changed?).include? true end
errors()
click to toggle source
# File lib/cot/collection.rb, line 35 def errors Hash[reject(&:valid?).map { |x| [x.id, x.errors] }] end
exists?()
click to toggle source
# File lib/cot/collection.rb, line 31 def exists? map(&:exists?).all? end
serializable_hash()
click to toggle source
# File lib/cot/collection.rb, line 23 def serializable_hash map(&:serializable_hash) end
to_json()
click to toggle source
# File lib/cot/collection.rb, line 27 def to_json serializable_hash.to_json end
update_members(payload)
click to toggle source
# 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
Private Instance Methods
initialize_objects(objects)
click to toggle source
# File lib/cot/collection.rb, line 73 def initialize_objects(objects) @objects = objects.map do |payload| payload = payload.fetch(@options[:sub_key], {}) if @options[:sub_key] self.class.klass.new @options[:default_attributes].merge(payload) end # Set the delegator methods to point to the new objects array __setobj__(@objects) end
parse_options(options)
click to toggle source
# File lib/cot/collection.rb, line 65 def parse_options(options) options = { sub_key: options } unless options.instance_of?(Hash) @options = options.with_indifferent_access @options[:default_attributes] = {} unless @options[:default_attributes] self.class.set_default_values @options.merge! self.class.options end
parse_params(params)
click to toggle source
# File lib/cot/collection.rb, line 50 def parse_params(params) until params.empty? item = params.shift if item.instance_of? Class self.class.klass = item elsif item.instance_of? Array @objects = item else options = item end end options ||= {} parse_options(options) end