Module: Card::Director::Stages

Included in:
Card::Director
Defined in:
card/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)


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

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

#reset_stageObject



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

def reset_stage
  @stage = -1
end

#stage_index(stage) ⇒ Object



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

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

#stage_ok?(opts) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'card/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 'card/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