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)


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

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

#reset_stageObject



47
48
49
# File 'lib/card/director/stages.rb', line 47

def reset_stage
  @stage = -1
end

#stage_index(stage) ⇒ Object



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

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

#stage_ok?(opts) ⇒ Boolean

Returns:

  • (Boolean)


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

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