lib/mongodoc/proxy.rb in mongodoc-0.0.0 vs lib/mongodoc/proxy.rb in mongodoc-0.1.0

- old
+ new

@@ -1,76 +1,74 @@ # Thanks Sandro! # http://github.com/sandro module MongoDoc - module Document - class Proxy - # List of array methods (that are not in +Object+) that need to be - # delegated to +collection+. - ARRAY_METHODS = (Array.instance_methods - Object.instance_methods).map { |n| n.to_s } + class Proxy + # List of array methods (that are not in +Object+) that need to be + # delegated to +collection+. + ARRAY_METHODS = (Array.instance_methods - Object.instance_methods).map { |n| n.to_s } - # List of additional methods that must be delegated to +collection+. - MUST_DEFINE = %w[to_a to_ary inspect to_bson ==] + # List of additional methods that must be delegated to +collection+. + MUST_DEFINE = %w[to_a to_ary inspect to_bson ==] - (ARRAY_METHODS + MUST_DEFINE).uniq.each do |method| - class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def #{method}(*args, &block) # def each(*args, &block) - collection.send(:#{method}, *args, &block) # collection.send(:each, *args, &block) - end # end - RUBY - end + (ARRAY_METHODS + MUST_DEFINE).uniq.each do |method| + class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def #{method}(*args, &block) # def each(*args, &block) + collection.send(:#{method}, *args, &block) # collection.send(:each, *args, &block) + end # end + RUBY + end - attr_reader :assoc_name, :collection, :collection_class, :_parent, :_root + attr_reader :assoc_name, :collection, :collection_class, :_parent, :_root - def _parent=(parent) - @_parent = parent - end + def _parent=(parent) + @_parent = parent + end - def _root=(root) - @_root = root - collection.each do |item| - item._root = root - end + def _root=(root) + @_root = root + collection.each do |item| + item._root = root end + end - def initialize(options) - @assoc_name = options[:assoc_name] - @collection = [] - @collection_class = options[:collection_class] - @_root = options[:root] - @_parent = options[:parent] - end + def initialize(options) + @assoc_name = options[:assoc_name] + @collection = [] + @collection_class = options[:collection_class] + @_root = options[:root] + @_parent = options[:parent] + end - alias_method :append, :<< - def <<(items) - items = [items] unless items.kind_of?(Array) - items.each do |item| - item = collection_class.new(item) if Hash === item - raise NotADocumentError unless collection_class === item - append item - item._parent = self - item._root = _root - end - self + alias_method :append, :<< + def <<(items) + items = [items] unless items.kind_of?(Array) + items.each do |item| + item = collection_class.new(item) if Hash === item + raise NotADocumentError unless collection_class === item + append item + item._parent = self + item._root = _root end - alias_method :push, :<< - alias_method :concat, :<< + self + end + alias_method :push, :<< + alias_method :concat, :<< - # Lie about our class. Borrowed from Rake::FileList - # Note: Does not work for case equality (<tt>===</tt>) - def is_a?(klass) - klass == Array || super(klass) - end - alias kind_of? is_a? + # Lie about our class. Borrowed from Rake::FileList + # Note: Does not work for case equality (<tt>===</tt>) + def is_a?(klass) + klass == Array || super(klass) + end + alias kind_of? is_a? - def path_to_root(attrs) - _parent.path_to_root(attrs) - end + def path_to_root(attrs) + _parent.path_to_root(attrs) + end - protected + protected - def _propose_update_attributes(src, attrs, safe) - src.errors.add(:base, 'update_attributes called through a has_many') - false - end + def _propose_update_attributes(src, attrs, safe) + src.errors.add(:base, 'update_attributes called through a has_many') + false end end end \ No newline at end of file