Module: Operabl::ClassMethods

Defined in:
lib/operabl.rb

Instance Method Summary collapse

Instance Method Details

#call(context_params = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/operabl.rb', line 12

def call(context_params=nil)

  ctx = case context_params
  when Hash
    Operabl::Context.new(context_params)
  when Operabl::Context
    context_params
  else
    Operabl::Context.new({})
  end

  operable = self.new

  steps.each do |step_branch, step_obj, options|
    if step_branch == ctx.branch
      case step_obj
      when String, Symbol
        operable.method(step_obj).call(ctx)
      when Proc
        operable.instance_exec ctx, &step_obj
      else
        if step_obj.respond_to? :call
          step_obj.call(ctx)
        else
          raise "#{step_obj.inspect} is invalid step argument"
        end
      end
      break if options[:fastforward]
    end

  end

  return ctx

end

#failure(symbol, options = {}) ⇒ Object



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

def failure(symbol, options={})
  steps << [:failure, symbol, options]
end

#step(symbol, options = {}) ⇒ Object



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

def step(symbol, options={})
  steps << [:success, symbol, options]
end

#stepsObject



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

def steps
  @steps ||= []
end