Class: UseCases::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/use_cases/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(steps) ⇒ Stack

Returns a new instance of Stack.



9
10
11
# File 'lib/use_cases/stack.rb', line 9

def initialize(steps)
  @steps = steps
end

Instance Attribute Details

#current_stepObject

Returns the value of attribute current_step.



7
8
9
# File 'lib/use_cases/stack.rb', line 7

def current_step
  @current_step
end

#prev_step_resultObject

Returns the value of attribute prev_step_result.



7
8
9
# File 'lib/use_cases/stack.rb', line 7

def prev_step_result
  @prev_step_result
end

#stepsObject (readonly)

Returns the value of attribute steps.



5
6
7
# File 'lib/use_cases/stack.rb', line 5

def steps
  @steps
end

Instance Method Details

#bind(object) ⇒ Object



13
14
15
16
# File 'lib/use_cases/stack.rb', line 13

def bind(object)
  steps.map! { |step| step.bind(object) }
  self
end

#call(initial_value = nil) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/use_cases/stack.rb', line 18

def call(initial_value = nil)
  steps.reduce(initial_value) do |prev_result, current_step|
    self.current_step = current_step
    self.prev_step_result = prev_result

    yield
  end
end

#find_step(step_name) ⇒ Object



47
48
49
# File 'lib/use_cases/stack.rb', line 47

def find_step(step_name)
  steps.find { |step| step.name == step_name }
end

#in_first_step?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/use_cases/stack.rb', line 27

def in_first_step?
  steps.find_index(current_step).zero?
end

#include_step?(step_name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/use_cases/stack.rb', line 43

def include_step?(step_name)
  step_names.include?(step_name)
end

#previous_result_empty?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/use_cases/stack.rb', line 31

def previous_result_empty?
  prev_step_result.nil?
end

#previous_step_valueObject



35
36
37
# File 'lib/use_cases/stack.rb', line 35

def previous_step_value
  prev_step_result.value
end

#step_namesObject



39
40
41
# File 'lib/use_cases/stack.rb', line 39

def step_names
  steps.map(&:name)
end