Module: Card::Director::Stages

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

Overview

Methods for intepreting stages of an action

Constant Summary collapse

STAGES =
%i[initialize prepare_to_validate validate
prepare_to_store store finalize integrate
after_integrate integrate_with_delay].freeze
STAGE_INDEX =
STAGES.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)


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

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

#reset_stageObject



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

def reset_stage
  @stage = -1
end

#stage_index(stage) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/card/director/stages.rb', line 23

def stage_index stage
  case stage
  when Symbol then
    STAGE_INDEX[stage]
  when Integer then
    stage
  else
    raise Card::Error, "not a valid stage: #{stage}"
  end
end

#stage_ok?(opts) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/card/director/stages.rb', line 34

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

Raises:



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

def stage_symbol index
  case index
  when Symbol
    return index if STAGE_INDEX[index]
  when Integer
    return STAGES[index] if index < STAGES.size
  end
  raise Card::Error, "not a valid stage index: #{index}"
end