lib/mongodoc/proxy.rb in mongodoc-0.1.2 vs lib/mongodoc/proxy.rb in mongodoc-0.2.0

- old
+ new

@@ -37,19 +37,18 @@ @_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 + def <<(item) + item = build(item) if Hash === item + if Document === item item._parent = self item._root = _root + _root.send(:register_save_observer, item) end + append item self end alias_method :push, :<< alias_method :concat, :<< @@ -58,17 +57,27 @@ def is_a?(klass) klass == Array || super(klass) end alias kind_of? is_a? - def path_to_root(attrs) - _parent.path_to_root(attrs) + def _path_to_root(src, attrs) + assoc_path = "#{assoc_name}.#{index(src)}" + assoc_attrs = attrs.inject({}) do |assoc_attrs, (key, value)| + assoc_attrs["#{assoc_path}.#{key}"] = value + assoc_attrs + end + _parent._path_to_root(src, assoc_attrs) end - protected + def _selector_path_to_root(selector) + annotated_selector = selector.inject({}) do |annotated, (key, value)| + annotated["#{assoc_name}.#{key}"] = value + annotated + end + _parent._selector_path_to_root(annotated_selector) + end - def _propose_update_attributes(src, attrs, safe) - src.errors.add(:base, 'update_attributes called through a has_many') - false + def build(attrs) + collection_class.new(attrs) end end -end \ No newline at end of file +end