Sha256: bb22a6dd192cab8d40ffb39266e81d2d0d1c5345c19b143c7aa6908bda522ecd

Contents?: true

Size: 1.11 KB

Versions: 6

Compression:

Stored size: 1.11 KB

Contents

module MongoDoc
  module Associations
    module ProxyBase
      def self.included(klass)
        klass.class_eval do
          attr_reader :assoc_name, :assoc_class, :_parent, :_root
        end
      end

      def _parent=(parent)
        @_parent = parent
      end

      def _path_to_root(src, attrs)
        _parent._path_to_root(src, annotated_keys(src, attrs))
      end

      def _root=(root)
        @_root = root
      end

      def initialize(options)
        @assoc_name = options[:assoc_name]
        @assoc_class = options[:assoc_class]
        @_root = options[:root]
        @_parent = options[:parent]
      end

      def attach(item)
        if is_document?(item)
          item._parent = self
          item._root = _root
          _root.send(:register_save_observer, item)
        end
        item
      end

      protected

      def annotated_keys(src, hash)
        annotated = {}
        hash.each do |(key, value)|
          annotated["#{assoc_name}.#{key}"] = value
        end
        annotated
      end

      def is_document?(object)
        object.respond_to?(:_parent)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
mongo_doc-0.4.1 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.4.0 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.3.2 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.3.1 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.3.0 lib/mongo_doc/associations/proxy_base.rb
mongodoc-0.2.4 lib/mongodoc/associations/proxy_base.rb