lib/cistern/collection.rb in cistern-0.12.3 vs lib/cistern/collection.rb in cistern-1.0.0.pre

- old
+ new

@@ -1,24 +1,28 @@ -class Cistern::Collection - extend Cistern::Attributes::ClassMethods - include Cistern::Attributes::InstanceMethods +module Cistern::Collection BLACKLISTED_ARRAY_METHODS = [ :compact!, :flatten!, :reject!, :reverse!, :rotate!, :map!, :shuffle!, :slice!, :sort!, :sort_by!, :delete_if, :keep_if, :pop, :shift, :delete_at, :compact ].to_set # :nodoc: - attr_accessor :records, :loaded, :connection + def self.service_collection(service, klass) + plural_name = Cistern::String.underscore(Cistern::String.demodulize(klass.name)) - alias service connection + service.const_get(:Collections).module_eval <<-EOS, __FILE__, __LINE__ + def #{plural_name}(attributes={}) + #{klass.name}.new(attributes.merge(service: self)) + end + EOS + end - def self.model(new_model=nil) - if new_model == nil - @model - else - @model = new_model + attr_accessor :records, :loaded, :service + + module ClassMethods + def model(new_model=nil) + @model ||= new_model end end alias build initialize @@ -73,10 +77,10 @@ raise(ArgumentError.new("Initialization parameters must be an attributes hash, got #{attributes.class} #{attributes.inspect}")) end model.new( { :collection => self, - :connection => connection, + :service => service, }.merge(attributes) ) end def reload