Class: Tzu::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/tzu/step.rb

Constant Summary collapse

DOUBLE_MUTATOR =
'You cannot define both receives and receives_many'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Step

Returns a new instance of Step.



8
9
10
11
# File 'lib/tzu/step.rb', line 8

def initialize(klass)
  @klass = klass
  @invoke_method = :run
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/tzu/step.rb', line 6

def klass
  @klass
end

#single_mutatorObject (readonly)

Returns the value of attribute single_mutator.



6
7
8
# File 'lib/tzu/step.rb', line 6

def single_mutator
  @single_mutator
end

#splat_mutatorObject (readonly)

Returns the value of attribute splat_mutator.



6
7
8
# File 'lib/tzu/step.rb', line 6

def splat_mutator
  @splat_mutator
end

Instance Method Details

#as(name) ⇒ Object



38
39
40
# File 'lib/tzu/step.rb', line 38

def as(name)
  @name = name
end

#invoke_with(method) ⇒ Object



42
43
44
# File 'lib/tzu/step.rb', line 42

def invoke_with(method)
  @invoke_method = method
end

#nameObject



23
24
25
26
# File 'lib/tzu/step.rb', line 23

def name
  return @name if @name && @name.is_a?(Symbol)
  @klass.to_s.split('::').last.symbolize
end

#receives(&block) ⇒ Object



28
29
30
31
# File 'lib/tzu/step.rb', line 28

def receives(&block)
  double_mutator_error if splat?
  @single_mutator = block
end

#receives_many(&block) ⇒ Object



33
34
35
36
# File 'lib/tzu/step.rb', line 33

def receives_many(&block)
  double_mutator_error if @single_mutator.present?
  @splat_mutator = block
end

#run(*params, prior_results) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/tzu/step.rb', line 13

def run(*params, prior_results)
  # Forward parameters as splat if no mutators are defined
  return @klass.send(@invoke_method, *params) if mutator.nil?

  command_params = process(*params, prior_results)

  return @klass.send(@invoke_method, command_params) unless splat?
  @klass.send(@invoke_method, *command_params)
end