Sha256: 3ab82b19a0dd9beab2958e25edfe766f7ded6858ec316ecb92a9f70ec6db8fb9

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module MongoDoc
  class ParentProxy
    attr_reader :assoc_name, :_parent

    def initialize(parent, assoc_name)
      raise ArgumentError.new('ParentProxy requires a parent') if parent.nil?
      raise ArgumentError.new('ParentProxy require an association name') if assoc_name.blank?
      @_parent = parent
      @assoc_name = assoc_name
    end

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

    def _selector_path_to_root(selector)
      _parent._selector_path_to_root(_annotated_keys(selector))
    end

    protected

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

    private

    def method_missing(method, *args)
      unless @_parent.respond_to?(method)
        message = "undefined method `#{method.to_s}' for proxied \"#{@_parent}\":#{@_parent.class.to_s}"
        raise NoMethodError, message
      end

      if block_given?
        @_parent.send(method, *args)  { |*block_args| yield(*block_args) }
      else
        @_parent.send(method, *args)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongodoc-0.2.1 lib/mongodoc/parent_proxy.rb
mongodoc-0.2.0 lib/mongodoc/parent_proxy.rb