Class: Redundancy::UpdateBase

Inherits:
Object
  • Object
show all
Defined in:
lib/redundancy/update_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UpdateBase

Returns a new instance of UpdateBase.



9
10
11
12
13
14
15
16
# File 'lib/redundancy/update_base.rb', line 9

def initialize options
  @options = options
  @source, @dest = options[:source], options[:dest]
  @klass = options[:klass]

  @change_if = options[:change_if]
  @update = options[:update] || false
end

Instance Attribute Details

#change_ifObject (readonly)

Returns the value of attribute change_if.



6
7
8
# File 'lib/redundancy/update_base.rb', line 6

def change_if
  @change_if
end

#destObject (readonly)

Returns the value of attribute dest.



5
6
7
# File 'lib/redundancy/update_base.rb', line 5

def dest
  @dest
end

#forceObject (readonly)

Returns the value of attribute force.



6
7
8
# File 'lib/redundancy/update_base.rb', line 6

def force
  @force
end

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/redundancy/update_base.rb', line 5

def klass
  @klass
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/redundancy/update_base.rb', line 4

def options
  @options
end

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/redundancy/update_base.rb', line 5

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



7
8
9
# File 'lib/redundancy/update_base.rb', line 7

def target
  @target
end

#updateObject (readonly)

Returns the value of attribute update.



6
7
8
# File 'lib/redundancy/update_base.rb', line 6

def update
  @update
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/redundancy/update_base.rb', line 7

def value
  @value
end

Instance Method Details

#after_save(record) ⇒ Object



28
29
# File 'lib/redundancy/update_base.rb', line 28

def after_save record
end

#before_save(record) ⇒ Object



25
26
# File 'lib/redundancy/update_base.rb', line 25

def before_save record
end

#force_update!(record) ⇒ Object



18
19
20
21
22
23
# File 'lib/redundancy/update_base.rb', line 18

def force_update! record
  @force = true
  @update = true
  before_save record
  after_save record
end

#force_update_target(record) ⇒ Object



81
82
83
84
# File 'lib/redundancy/update_base.rb', line 81

def force_update_target record
  @update = true
  update_target record
end

#get_target_from_association(record) ⇒ Object



31
32
33
# File 'lib/redundancy/update_base.rb', line 31

def get_target_from_association record
  @target = dest[:association] ? record.send(dest[:association]) : record
end

#get_target_from_prev_id(record) ⇒ Object



35
36
37
38
39
# File 'lib/redundancy/update_base.rb', line 35

def get_target_from_prev_id record
  prev_id = record.send(:attribute_was, dest[:prev_id])
  return unless prev_id
  @target = dest[:klass].where(id: prev_id)
end

#get_target_from_relation_first_recordObject



41
42
43
# File 'lib/redundancy/update_base.rb', line 41

def get_target_from_relation_first_record
  @target = @target.first if @target.kind_of? ActiveRecord::Relation
end

#get_value_from_association(record) ⇒ Object



45
46
47
48
49
50
# File 'lib/redundancy/update_base.rb', line 45

def get_value_from_association record
  @value = source[:association] ? record.send(source[:association]) : record
  @value = value && source[:attribute] && value.send(source[:attribute])
  @value = nil if source[:nil_unless] && !record.send(source[:nil_unless])
  @value
end

#get_value_from_source(record) ⇒ Object



52
53
54
# File 'lib/redundancy/update_base.rb', line 52

def get_value_from_source record
  @value = source
end

#get_value_from_target(record) ⇒ Object



56
57
58
# File 'lib/redundancy/update_base.rb', line 56

def get_value_from_target record
  @value = target && source[:attribute] && target.send(source[:attribute])
end

#log(message) ⇒ Object

require ‘colorize’



91
92
93
# File 'lib/redundancy/update_base.rb', line 91

def log message
  # puts "  Redundancy  ".colorize(:green) + message
end

#need_update?(record) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/redundancy/update_base.rb', line 86

def need_update? record
  @force || !change_if || record.send(:attribute_changed?, change_if)
end

#raise_if_class_mismatch(record) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
# File 'lib/redundancy/update_base.rb', line 60

def raise_if_class_mismatch record
  raise ArgumentError, "record class mismatch, expected #{klass}, got #{record.class}" unless record.kind_of? klass
end

#update_target(record) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/redundancy/update_base.rb', line 64

def update_target record
  case target
  when ActiveRecord::Base
    return if target.send(:read_attribute, dest[:attribute]) == value
    log "#{ update ? "update" : "write" } #{target.class}(#{target.id})##{dest[:attribute]} with #{value.inspect}"
    log "#{change_if}: #{record.send(change_if).inspect}, #{dest[:association]||"self"}.id: #{target.id}" if change_if
    if update
      target.send(:update_attribute, dest[:attribute], value)
    else
      target.send(:write_attribute, dest[:attribute], value)
    end
  when ActiveRecord::Relation
    log "update #{target.class}##{dest[:attribute]} with #{value.inspect}"
    target.send(:update_all, dest[:attribute] => value)
  end
end