Module: Card::Stage

Included in:
StageDirector
Defined in:
lib/card/stage.rb

Overview

The process of writing a card change to the database is divided into 8 stages that are grouped in 3 phases.

‘validation phase’

* initialize stage
* prepare_to_validate stage
* validate stage

‘storage phase’

* prepare_to_store stage
* store stage
* finalize stage

‘integration phase’

* integrate stage
* integrate_with_delay stage

Constant Summary collapse

STAGES =
[:initialize, :prepare_to_validate, :validate, :prepare_to_store,
:store, :finalize, :integrate, :integrate_with_delay].freeze
STAGE_INDEX =
{}

Instance Method Summary collapse

Instance Method Details

#after?(allowed_phase) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/card/stage.rb', line 63

def after? allowed_phase
  STAGE_INDEX[allowed_phase] < STAGE_INDEX[stage]
end

#before?(allowed_phase) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/card/stage.rb', line 59

def before? allowed_phase
  STAGE_INDEX[allowed_phase] > STAGE_INDEX[stage]
end

#in?(allowed_phase) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/card/stage.rb', line 67

def in? allowed_phase
  (allowed_phase.is_a?(Array) && allowed_phase.include?(stage)) ||
    allowed_phase == stage
end

#stage_index(stage) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/card/stage.rb', line 39

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

#stage_ok?(opts) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
# File 'lib/card/stage.rb', line 50

def stage_ok? opts
  stage && (
  (opts[:during] && in?(opts[:during])) ||
    (opts[:before] && before?(opts[:before])) ||
    (opts[:after] && after?(opts[:after])) ||
    true # no phase restriction in opts
  )
end

#stage_symbol(index) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
# File 'lib/card/stage.rb', line 29

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