Class: DelegateCached::SourceInstaller

Inherits:
Installer
  • Object
show all
Defined in:
lib/delegate_cached/source_installer.rb

Instance Attribute Summary

Attributes inherited from Installer

#options, #source, #target

Instance Method Summary collapse

Methods inherited from Installer

#initialize

Constructor Details

This class inherits a constructor from DelegateCached::Installer

Instance Method Details

#callback_if_statementObject



53
54
55
56
# File 'lib/delegate_cached/source_installer.rb', line 53

def callback_if_statement
  return nil if @source.reflection.macro == :has_one
  ", if: :#{@source.column}_changed?"
end

#install_accessor_override_methodObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/delegate_cached/source_installer.rb', line 26

def install_accessor_override_method
  @source.model.class_eval %(
    def #{@source.column}
      unless self['#{@source.column}'].nil?
        return self['#{@source.column}']
      end
      #{update_if_update_when_nil_option}
      #{@source.association}.#{@target.column}
    end
  )
end

#install_callbackObject



47
48
49
50
51
# File 'lib/delegate_cached/source_installer.rb', line 47

def install_callback
  @source.model.class_eval %(
    before_save :#{set_method_name}#{callback_if_statement}
  )
end

#install_instance_methodsObject



3
4
5
6
7
8
# File 'lib/delegate_cached/source_installer.rb', line 3

def install_instance_methods
  install_update_method
  install_accessor_override_method
  install_setter_override_method
  install_callback
end

#install_setter_override_methodObject



38
39
40
41
42
43
44
45
# File 'lib/delegate_cached/source_installer.rb', line 38

def install_setter_override_method
  @source.model.class_eval %(
    def #{set_method_name}
      return unless #{@source.association}
      self['#{@source.column}'] = #{@source.association}.#{@target.column}
    end
  )
end

#install_update_methodObject



18
19
20
21
22
23
24
# File 'lib/delegate_cached/source_installer.rb', line 18

def install_update_method
  @source.model.class_eval %(
    def #{update_method_name}
      update(#{@source.column}: #{@source.association}.#{@target.column})
    end
  )
end

#set_method_nameObject



14
15
16
# File 'lib/delegate_cached/source_installer.rb', line 14

def set_method_name
  "set_delegate_cached_value_for_#{@source.column}"
end

#update_if_update_when_nil_optionObject



58
59
60
# File 'lib/delegate_cached/source_installer.rb', line 58

def update_if_update_when_nil_option
  update_method_name if options[:update_when_nil]
end

#update_method_nameObject



10
11
12
# File 'lib/delegate_cached/source_installer.rb', line 10

def update_method_name
  "update_delegate_cached_value_for_#{@source.column}"
end