Sha256: e65f66615a5ab4f89b62d476b0fc2b48591ef460a686a39f024a051fbed3a811
Contents?: true
Size: 1.09 KB
Versions: 1
Compression:
Stored size: 1.09 KB
Contents
module ActiveRecord class Base class << self def delegate(*methods) options = methods.pop unless options.is_a?(Hash) && to = options[: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 if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/ raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method." end prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_" methods.each do |method| define_method method do send(:delegator_for, to).send(method) end define_method "#{method}=" do |value| send(:delegator_for, to).send("#{method}=", value) end end end end protected def delegator_for(association) send("#{association}=", self.class.reflect_on_association(association).klass.new) if send(association).nil? send(association) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
better_delegation-1.0.0 | lib/active_support/core_ext/module/better_delegation.rb |