Module: FlexaLib::ActiveRecord::Lookup
- Defined in:
- lib/flexa_lib/model_extensions.rb
Overview
fim do modulo PesquisaWrapper
Instance Method Summary collapse
Instance Method Details
#delegate_lookup(*methods) ⇒ Object
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 |
# File 'lib/flexa_lib/model_extensions.rb', line 36 def delegate_lookup(*methods) = methods.pop unless .is_a?(Hash) && to = [:to] raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)." end to = to.to_s prefix, allow_nil = .values_at(:prefix, :allow_nil) prefix = true if prefix.nil? allow_nil = true if allow_nil.nil? if prefix == true && to =~ /^[^a-z_]/ raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method." end method_prefix = if prefix "#{prefix == true ? to : prefix}_" else '' end file, line = caller.first.split(':', 2) line = line.to_i methods.each do |method| method = method.to_s # Attribute writer methods only accept one argument. Makes sure []= # methods still accept two arguments. definition = (method =~ /[^\]]=$/) ? "arg" : "*args, &block" if allow_nil module_eval(<<-EOS, file, line - 2) def #{method_prefix}#{method}(#{definition}) # def customer_name(*args, &block) if #{to} || #{to}.respond_to?(:#{method}) # if client || client.respond_to?(:name) #{to}.#{method}(#{definition}) # client.name(*args, &block) end # end end # end EOS else exception = %(raise "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}") module_eval(<<-EOS, file, line - 1) def #{method_prefix}#{method}(#{definition}) # def customer_name(*args, &block) #{to}.#{method}(#{definition}) # client.name(*args, &block) rescue NoMethodError # rescue NoMethodError if #{to}.nil? # if client.nil? #{exception} # # add helpful message to the exception else # else raise # raise end # end end # end EOS end #cria funcao com valor false module_eval(<<-EOS, file, line - 2) def #{method_prefix}#{method}=(val) # def customer_name=(val) false # false end # end EOS #cria funcao com valor false end end |