Module: ActiveTools::ActiveModel::DelegateAttributes::ClassMethods

Defined in:
lib/active_tools/active_model/delegate_attributes.rb

Instance Method Summary collapse

Instance Method Details

#delegate_attributes(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_tools/active_model/delegate_attributes.rb', line 7

def delegate_attributes(*args)
  options = args.extract_options!
  errors_option = options.delete(:errors)
  writer_option = options.delete(:writer)
  prefix_option = options.delete(:prefix)

  writer_regexp = /=\z/
  readers = args.select {|a| a.to_s !=~ writer_regexp}
  writers = args.select {|a| a.to_s =~ writer_regexp}
  if writer_option == true
    writers |= readers.map {|a| "#{a}="}
  end

  class_eval do
    delegate *(readers + writers), options.dup
    unless errors_option == false
      valid_with options[:to], :attributes => Hash[readers.map {|a| [a, a]}], :fit => errors_option.to_s == "fit", :prefix => prefix_option
    end
  end          
end