Class: Changey::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/changey/track.rb

Defined Under Namespace

Classes: Nothing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, direction = nil, expected_value = nil, expected_other_value = Nothing) ⇒ Track

Returns a new instance of Track.



19
20
21
22
23
24
25
26
27
28
# File 'lib/changey/track.rb', line 19

def initialize(attribute, direction = nil, expected_value = nil, expected_other_value = Nothing)
  @attribute = attribute
  @direction = direction
  @expected_value = expected_value
  @expected_other_value = expected_other_value
  @validates = []
  @before_saves = []
  @after_saves = []
  @after_commits = []
end

Instance Attribute Details

#after_commitsObject

Returns the value of attribute after_commits.



17
18
19
# File 'lib/changey/track.rb', line 17

def after_commits
  @after_commits
end

#after_savesObject

Returns the value of attribute after_saves.



16
17
18
# File 'lib/changey/track.rb', line 16

def after_saves
  @after_saves
end

#attributeObject (readonly)

Returns the value of attribute attribute.



9
10
11
# File 'lib/changey/track.rb', line 9

def attribute
  @attribute
end

#before_savesObject

Returns the value of attribute before_saves.



15
16
17
# File 'lib/changey/track.rb', line 15

def before_saves
  @before_saves
end

#directionObject

:from or :to



10
11
12
# File 'lib/changey/track.rb', line 10

def direction
  @direction
end

#expected_other_valueObject

The value expected in the opposite state



12
13
14
# File 'lib/changey/track.rb', line 12

def expected_other_value
  @expected_other_value
end

#expected_valueObject

The value expected



11
12
13
# File 'lib/changey/track.rb', line 11

def expected_value
  @expected_value
end

#validatesObject

Returns the value of attribute validates.



14
15
16
# File 'lib/changey/track.rb', line 14

def validates
  @validates
end

Instance Method Details

#run?(record) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/changey/track.rb', line 30

def run?(record)
  return false unless record.send("#{attribute}_changed?")

  previous_value = record.send("#{attribute}_was")
  current_value  = record.send(attribute)

  if expected_other_value == Nothing
    if direction == :from && matches?(previous_value, expected_value)
      return true
    end

    if direction == :to && matches?(current_value, expected_value)
      return true
    end
  else
    if direction == :from && matches?(previous_value, expected_value) &&
       matches?(current_value, expected_other_value)
      return true
    end

    if direction == :to && matches?(current_value, expected_value) &&
       matches?(previous_value, expected_other_value)
      return true
    end
  end

  false
end