Sha256: a1a88bde3388ca6a253d512cdd512bd8fa7b7895e33c37dd1c52affb79c3c2cc

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

# -*- encoding : utf-8 -*-

module Guacamole
  module Proxies
    # This is the base class for the association proxies. Proxies are only
    # needed for non-embedded relations between objects. Embedded objects are
    # taken care of by Virtus.
    #
    # The `Proxy` class undefines most methods and passes them to the
    # `@target`.  The `@target` will be a lambda which will lazy query the
    # requested objects from the database.
    #
    # Concrete proxy classes are:
    #
    #  * {Guacamole::Proxies::ReferencedBy}: This will handle one-to-many associations
    #  * {Guacamole::Proxies::References}: This will handle many-to-one associations
    class Proxy < BasicObject
      # Despite using BasicObject we still need to remove some method to get a near transparent proxy
      instance_methods.each do |method|
        undef_method(method) unless method =~ /^__/
      end

      # Convenience method to setup the proxy. The subclasses need to care of creating
      # the `target` correctly.
      #
      # @param [Object] base The class holding the reference. Currently not used.
      # @param [#call] target The lambda for getting the required objects from the database.
      def init(base, target)
        @base = base
        @target = target
      end

      def method_missing(meth, *args, &blk)
        @target.call.send meth, *args, &blk
      end

      def respond_to_missing?(name, include_private = false)
        @target.respond_to?(name, include_private)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
guacamole-0.3.0 lib/guacamole/proxies/proxy.rb
guacamole-0.2.0 lib/guacamole/proxies/proxy.rb
guacamole-0.1.0 lib/guacamole/proxies/proxy.rb