Sha256: 6f2ce8d391c502ba8061685d2ac19f8c77fe53ba56fd26c4fb7d76628e1965c5

Contents?: true

Size: 1.17 KB

Versions: 37

Compression:

Stored size: 1.17 KB

Contents

module MongoDoc
  module Associations
    module ProxyBase
      def self.included(klass)
        klass.class_eval do
          attr_reader :_assoc_class, :_assoc_name, :_modifier_path, :_root, :_selector_path
        end
      end

      %w(_modifier_path _selector_path).each do |setter|
        module_eval(<<-RUBY, __FILE__, __LINE__)
          def #{setter}=(path)
            @#{setter} = (path.blank? ? '' : path + '.') + _assoc_name.to_s
          end
        RUBY
      end

      def _root=(root)
        @_root = root
      end

      def initialize(options)
        @_assoc_name = options[:assoc_name]
        @_assoc_class = options[:assoc_class]
        self._root = options[:root]
        self._selector_path = self._modifier_path = options[:path]
      end

      def self.is_document?(object)
        object.respond_to?(:_root)
      end

      protected

      def attach(obj)
        attach_document(obj) if ProxyBase.is_document?(obj)
        obj
      end

      def attach_document(doc)
        doc._modifier_path = _modifier_path
        doc._selector_path = _selector_path
        doc._root = _root
        _root.send(:register_save_observer, doc)
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 2 rubygems

Version Path
mongo_doc-0.6.34 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.33 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.32 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.31 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.30 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.29 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.28 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.27 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.26 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.25 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.23 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.22 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.21 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.20 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.19 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.18 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.17 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.16 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.15 lib/mongo_doc/associations/proxy_base.rb
mongo_doc-0.6.14 lib/mongo_doc/associations/proxy_base.rb