Module: Card::Director::Stages

Included in:
Card::Director
Defined in:
lib/card/director/stages.rb

Overview

Methods for interpreting stages of an action

Constant Summary collapse

SYMBOLS =

Validation phase

%i[initialize prepare_to_validate validate] +      # Validation phase
%i[prepare_to_store store finalize] +              # Storage phase
%i[integrate after_integrate integrate_with_delay] # Integration phase
.freeze
INDECES =
SYMBOLS.each_with_index.with_object({}) do |(stage, index), hash|
  Card.define_callbacks "#{stage}_stage", "#{stage}_final_stage"
  hash[stage] = index
end.freeze

Instance Method Summary collapse

Instance Method Details

#finished_stage?(stage) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/card/director/stages.rb', line 45

def finished_stage? stage
  @current_stage_index > stage_index(stage)
end

#reset_stageObject



49
50
51
# File 'lib/card/director/stages.rb', line 49

def reset_stage
  @current_stage_index = -1
end

#stage_index(stage) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/card/director/stages.rb', line 25

def stage_index stage
  case stage
  when Symbol
    INDECES[stage]
  when Integer
    stage
  when nil
    -1
  else
    raise Card::Error, "not a valid stage: #{stage}"
  end
end

#stage_ok?(opts) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/card/director/stages.rb', line 38

def stage_ok? opts
  return false unless stage

  test = %i[during before after].find { |t| opts[t] }
  test ? send("#{test}?", opts[t]) : true
end

#stage_symbol(index) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/card/director/stages.rb', line 15

def stage_symbol index
  if index.is_a?(Symbol) && INDECES[index]
    index
  elsif index.is_a?(Integer) && index < SYMBOLS.size
    SYMBOLS[index]
  else
    raise Card::Error, "not a valid stage index: #{index}"
  end
end