Module: Workout

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations
Defined in:
lib/workout.rb,
lib/workout/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#callObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/workout.rb', line 105

def call
  # don't even try if we are already invalid
  return self if invalid?

  rescuing do
    # run each step, possibly feeding the last result to the next
    self.class.steps.reduce(nil) do |last, name|
      self.current_step = name

      # #fail will throw :failure instead of raising
      result = catch :failure do
        if method(name).arity == 1
          send name, last
        else
          send name
        end
      end

      if invalid?
        break
      else
        result
      end
    end

    complete if valid?
  end
ensure
  self.current_step = nil
end

#completeObject



44
45
46
# File 'lib/workout.rb', line 44

def complete
  @_fsm.trigger(:complete)
end

#complete!Object



48
49
50
# File 'lib/workout.rb', line 48

def complete!
  @_fsm.trigger!(:complete)
end

#complete?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/workout.rb', line 40

def complete?
  @_fsm.state == :completed
end

#fail(**args) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/workout.rb', line 52

def fail(**args)
  should_throw = args.delete(:throw) != false
  @_failures.push({ step: current_step, args: args })
  @_fsm.trigger(:fail)
  validate
  throw :failure if should_throw
end

#initialize(**opts) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/workout.rb', line 32

def initialize(**opts)
  @_fsm = MicroMachine.new(:pending).tap do |fsm|
    fsm.when(:complete, :pending => :completed)
    fsm.when(:fail,     :pending => :failed)
  end
  @_failures = []
end

#success?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/workout.rb', line 66

def success?
  complete? && valid?
end