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



11
12
13
14
# File 'lib/tzu/step.rb', line 11

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

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

#single_mutatorObject (readonly)

Returns the value of attribute single_mutator.



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

def single_mutator
  @single_mutator
end

#splat_mutatorObject (readonly)

Returns the value of attribute splat_mutator.



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

def splat_mutator
  @splat_mutator
end

Instance Method Details

#as(name) ⇒ Object



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

def as(name)
  @name = name
end

#invoke_with(method) ⇒ Object



46
47
48
# File 'lib/tzu/step.rb', line 46

def invoke_with(method)
  @invoke_method = method
end

#nameObject



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

def name
  return @name if @name&.is_a?(Symbol)

  @klass.to_s.split("::").last.underscore.to_sym
end

#receives(&block) ⇒ Object



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

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

#receives_many(&block) ⇒ Object



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

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

#run(*params, prior_results) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/tzu/step.rb', line 16

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