1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
module ActiveLdap
module Association
class Proxy
alias_method :proxy_respond_to?, :respond_to?
alias_method :proxy_extend, :extend
delegate :to_param, :to => :target
def initialize(owner, options)
@owner = owner
@options = options
reset
extend(options[:extend]) if options[:extend]
end
def respond_to?(symbol, include_priv=false)
proxy_respond_to?(symbol, include_priv) or
(load_target && @target.respond_to?(symbol, include_priv))
end
def ===(other)
load_target and other === @target
end
def reset
@target = nil
@loaded = false
end
def reload
reset
load_target
end
def loaded?
@loaded
end
def loaded
@loaded = true
end
def target
@target
end
def target=(target)
@target = target
loaded
end
def exists?
load_target
not @target.nil?
end
private
def method_missing(method, *args, &block)
load_target
@target.send(method, *args, &block)
end
def foreign_class
klass = @owner.class.associated_class(@options[:association_id])
klass = @owner.class.module_eval(klass) if klass.is_a?(String)
klass
end
def have_foreign_key?
false
end
def primary_key
@options[:primary_key_name] || @owner.dn_attribute
end
def foreign_key
@options[:foreign_key_name] || foreign_class.dn_attribute
end
def load_target
if !@owner.new_entry? or have_foreign_key?
begin
@target = find_target unless loaded?
rescue EntryNotFound
reset
end
end
loaded if target
target
end
def find_options(options={})
if @owner.connection != @owner.class.connection
{:connection => @owner.connection}.merge(options)
else
options
end
end
def infect_connection(target)
conn = @owner.instance_variable_get("@connection")
target.connection = conn if conn
end
end
end
end
|