Module: Dooly::IdProxy::ClassMethods
- Defined in:
- lib/dooly/id_proxy.rb
Instance Method Summary collapse
- #id_proxy(*args) ⇒ Object (also: #idx)
- #id_proxy_class(klass, options = {}) ⇒ Object
- #id_proxy_delegate ⇒ Object
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) = args. if args.length > 1 return args.map {|arg| id(arg, )} 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 [:relate] fn = [:relate] == true ? "#{self.name.underscore}_id".to_sym : [: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, = {}) 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 = [:finders] || [:finder] self.id_proxy_klass.finders = finders if finders end |
#id_proxy_delegate ⇒ Object
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 |