Class: ManaMana::Steps

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/manamana/steps.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSteps

Returns a new instance of Steps.



18
19
20
# File 'lib/manamana/steps.rb', line 18

def initialize
  @steps = []
end

Instance Attribute Details

#stepsObject (readonly)

Returns the value of attribute steps.



16
17
18
# File 'lib/manamana/steps.rb', line 16

def steps
  @steps
end

Class Method Details

.add(pattern, &block) ⇒ Object



8
9
10
# File 'lib/manamana/steps.rb', line 8

def self.add(pattern, &block)
  instance.add pattern, &block
end

.call(pattern) ⇒ Object



12
13
14
# File 'lib/manamana/steps.rb', line 12

def self.call(pattern)
  instance.call pattern
end

Instance Method Details

#add(pattern, &block) ⇒ Object



22
23
24
# File 'lib/manamana/steps.rb', line 22

def add(pattern, &block)
  steps << { pattern: pattern, block: block }
end

#call(step_name) ⇒ Object

The step_name variable below is a string that may or may not match the regex pattern of one of the hashes in the steps array.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/manamana/steps.rb', line 29

def call(step_name)
  vars = nil
  step = steps.find { |s| vars = s[:pattern].match(step_name) }

  raise "Undefined step '#{ step_name }'" unless step

  if vars.length > 1
    step[:block].call(*vars[1..-1])
  else
    step[:block].call
  end
end