Module: Dooly::IdProxy::ClassMethods

Defined in:
lib/dooly/id_proxy.rb

Instance Method Summary collapse

Instance Method Details

#id_proxy(*args) ⇒ Object Also known as: idx



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dooly/id_proxy.rb', line 24

def id_proxy(*args)
  options = args.extract_options!

  if args.length > 1
    return args.map {|arg| id(arg, options)}
  end

  value = args[0]
  begin
    return value.idx if value.is_a? self
    return self.id_proxy_klass.new(value) if value.is_a? Integer
    return self.id_proxy_klass.new(Integer(value)) if value.is_a? String
    return value if Dooly::IdProxy::Base === value
  
    if options[:relate]
      fn = options[:relate] == true ? "#{self.name.underscore}_id".to_sym : options[:relate].to_sym
      return self.id_proxy_klass.new(value.send(fn)) if value.respond_to?(fn)
    end
  rescue => e
    raise "Cannot make id_proxy from #{self.name} with #{value.to_s}: #{e}"
  else
    raise "Cannot make id_proxy from #{self.name} with #{value.to_s}"
  end
end

#id_proxy_class(klass, options = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/dooly/id_proxy.rb', line 11

def id_proxy_class(klass, options = {})
  raise "Id Proxy class must inherit Dooly::IdProxyBase: #{klass}" unless klass <= Dooly::IdProxy::Base
  self.id_proxy_klass = klass
  self.id_proxy_klass.model = self
  finders = options[:finders] || options[:finder]
  self.id_proxy_klass.finders = finders if finders
end

#id_proxy_delegateObject



19
20
21
22
# File 'lib/dooly/id_proxy.rb', line 19

def id_proxy_delegate
  self.extend Forwardable
  self.def_delegators 'self.id_proxy', *self.id_proxy_klass.instance_methods(false)
end