Module: Card::Set::All::Phases

Extended by:
Card::Set
Defined in:
tmpsets/set/mod001-01_core/all/phases.rb

Constant Summary collapse

PHASES =

perhaps above should be in separate module? ~~~~~~

{}

Instance Method Summary collapse

Methods included from Card::Set

abstract_set?, all_set?, clean_empty_module_from_hash, clean_empty_modules, define_on_format, each_format, ensure_set, extended, format, include_set, include_set_formats, process_base_module_list, process_base_modules, register_set, register_set_format, shortname, stage_method, view, write_tmp_file

Methods included from Trait

#card_accessor, #card_reader, #card_writer

Methods included from Event

#define_event, #event

Instance Method Details

#abort(status, msg = 'action canceled') ⇒ Object

The Card#abort method is for cleanly exiting an action without continuing to process any further events.

Three statuses are supported:

failure: adds an error, returns false on save
success: no error, returns true on save
triumph: similar to success, but if called on a subcard
         it causes the entire action to abort (not just the subcard)

Raises:



15
16
17
18
19
20
21
22
23
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 15

def abort status, msg = 'action canceled'
  if status == :failure && errors.empty?
    errors.add :abort, msg
  elsif Hash === status && status[:success]
    success << status[:success]
    status = :success
  end
  raise Card::Abort.new(status, msg)
end

#abortableObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 25

def abortable
  yield
rescue Card::Abort => e
  if e.status == :triumph
    @supercard ? raise(e) : true
  elsif e.status == :success
    if @supercard
      @supercard.subcards.delete(key)
    end
    true
  end
end

#after?(allowed_phase) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 159

def after? allowed_phase
  PHASES[allowed_phase] < PHASES[phase] ||
    (PHASES[allowed_phase] == PHASES[phase] && subphase == :after)
end

#approveObject



96
97
98
99
100
101
102
103
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 96

def approve
  @action ||= identify_action
  run_phase :approve
  expire_pieces if errors.any?
  errors.empty?
rescue => e
  rescue_event e
end

#before?(allowed_phase) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
157
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 154

def before? allowed_phase
  PHASES[allowed_phase] > PHASES[phase] ||
    (PHASES[allowed_phase] == PHASES[phase] && subphase == :before)
end

#changed_condition_applies?(db_column) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 187

def changed_condition_applies? db_column
  if db_column
    db_column =
      case db_column.to_sym
      when :content then 'db_content'
      when :type    then 'type_id'
      else db_column.to_s
      end
    @action != :delete && changes[db_column]
  else
    true
  end
end

#event_applies?(opts) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 173

def event_applies? opts
  on_condition_applies?(opts[:on]) &&
    changed_condition_applies?(opts[:changed]) &&
    when_condition_applies?(opts[:when])
end

#extendObject



127
128
129
130
131
132
133
134
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 127

def extend
  run_phase :extend
  run_phase :subsequent
rescue => e
  rescue_event e
ensure
  @action = nil
end

#identify_actionObject



105
106
107
108
109
110
111
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 105

def identify_action
  case
  when trash     then :delete
  when new_card? then :create
  else :update
  end
end

#in?(allowed_phase) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
167
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 164

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

#on_condition_applies?(action) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
182
183
184
185
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 179

def on_condition_applies? action
  if action
    Array.wrap(action).member? @action
  else
    true
  end
end

#phaseObject



78
79
80
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 78

def phase
  @phase || (@supercard && @supercard.phase)
end

#phase_ok?(opts) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 145

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

#prepareObject



86
87
88
89
90
91
92
93
94
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 86

def prepare
  @action = identify_action
  # the following should really happen when type, name etc are changed
  reset_patterns
  include_set_modules
  run_phase :prepare
rescue => e
  rescue_event e
end

#rescue_event(e) ⇒ Object



136
137
138
139
140
141
142
143
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 136

def rescue_event e
  @action = nil
  expire_pieces
  subcards.each(&:expire_pieces)
  raise e
  # rescue Card::Cancel
  # false
end

#run_phase(phase, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 63

def run_phase phase, &block
  @phase = phase
  @subphase = :before
  if block_given?
    block.call
  else
    run_callbacks phase
  end
  @subphase = :after
end

#simulate_phase(opts, &block) ⇒ Object



74
75
76
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 74

def simulate_phase opts, &block
  @phase
end

#storeObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 113

def store
  run_phase :store do
    run_callbacks :store do
      yield # unless @draft
      @virtual = false
    end
  end
  run_phase :stored
rescue => e
  rescue_event e
ensure
  @from_trash =  @last_content_action_id = nil
end

#subphaseObject



82
83
84
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 82

def subphase
  @subphase || (@supercard && @supercard.subphase)
end

#successObject



209
210
211
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 209

def success
  Env.success(cardname)
end

#valid_subcard?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 38

def valid_subcard?
  abortable { valid? }
end

#when_condition_applies?(block) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
204
205
206
207
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 201

def when_condition_applies? block
  if block
    block.call self
  else
    true
  end
end

#with_transaction_returning_statusObject

this is an override of standard rails behavior that rescues abort makes it so that :success abortions do not rollback



44
45
46
47
48
49
50
51
52
# File 'tmpsets/set/mod001-01_core/all/phases.rb', line 44

def with_transaction_returning_status
  status = nil
  self.class.transaction do
    add_to_transaction
    status = abortable { yield }
    raise ActiveRecord::Rollback unless status
  end
  status
end