class Changey::Track

Attributes

after_commits[RW]
after_saves[RW]
attribute[R]
before_saves[RW]
direction[RW]
expected_other_value[RW]
expected_value[RW]
validates[RW]

Public Class Methods

new(attribute, direction = nil, expected_value = nil, expected_other_value = Nothing) click to toggle source
# 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

Public Instance Methods

run?(record) click to toggle source
# 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

Private Instance Methods

matches?(value, expectation) click to toggle source
# File lib/changey/track.rb, line 61
def matches?(value, expectation)
  case expectation
  when Array
    expectation.include?(value)
  when Regexp
    !(expectation =~ value).nil?
  when Proc
    expectation.call(value) == true
  when :anything
    true
  else
    value == expectation
  end
end