Module: ActsAsLiving::MethodsDefiner::ClassMethods

Defined in:
lib/acts_as_living/methods_definer.rb

Instance Method Summary collapse

Instance Method Details

#alive_stagesObject



103
104
105
# File 'lib/acts_as_living/methods_definer.rb', line 103

def alive_stages
  stages.except(@death).keys
end

#deathObject



154
155
156
# File 'lib/acts_as_living/methods_definer.rb', line 154

def death
  @death
end

#define_phase_queries(phase, delimiters) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/acts_as_living/methods_definer.rb', line 176

def define_phase_queries(phase, delimiters)
  define_method("#{phase}?") do
    if delimiters.length == 1
      klass_stages[stage] == klass_stages[delimiters]
    else
      klass_stages[stage] >= klass_stages[delimiters.first] &&
        klass_stages[stage] <= klass_stages[delimiters.second]
    end
  end

  define_method("pre_#{phase}?") do
    klass_stages[stage] < klass_stages[delimiters.first] unless cancelled?
  end

  define_method("past_#{phase}?") do
    klass_stages[stage] > klass_stages[delimiters.last] || cancelled?
  end
end

#define_stage_queries(stage_key) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/acts_as_living/methods_definer.rb', line 195

def define_stage_queries(stage_key)
  define_method("stage_changed_to_#{stage_key}?") do
    stage_changed? && stage_was.to_s == stage_key.to_s
  end

  define_method("stage_saved_to_#{stage_key}?") do
    saved_change_to_stage? && stage.to_s == stage_key.to_s
  end

  define_method("pre_#{stage_key}?") do
    klass_stages[stage] < klass_stages[stage_key] unless cancelled?
  end

  define_method("past_#{stage_key}?") do
    klass_stages[stage] > klass_stages[stage_key] || cancelled?
  end

  define_method("cancelled_from_#{stage_key}?") do
    stage == 'cancelled' && stage_was.to_s == stage_key.to_s
  end
end

#first_stageObject



146
147
148
# File 'lib/acts_as_living/methods_definer.rb', line 146

def first_stage
  (stage_keys - %i[cancelled]).first
end

#first_stage?(stage) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/acts_as_living/methods_definer.rb', line 150

def first_stage?(stage)
  first_stage.to_s == stage.to_s
end

#last_stageObject



138
139
140
# File 'lib/acts_as_living/methods_definer.rb', line 138

def last_stage
  (stage_keys - %i[cancelled]).last
end

#last_stage?(stage) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/acts_as_living/methods_definer.rb', line 142

def last_stage?(stage)
  last_stage.to_s == stage.to_s
end

#phasesObject



166
167
168
# File 'lib/acts_as_living/methods_definer.rb', line 166

def phases
  @phases
end

#phases_for(stage) ⇒ Object



170
171
172
173
174
# File 'lib/acts_as_living/methods_definer.rb', line 170

def phases_for(stage)
  phases_with_ranges.keys.select do |phase|
    phases_with_ranges[phase].include? stages[stage]
  end
end

#phases_with_rangesObject



158
159
160
# File 'lib/acts_as_living/methods_definer.rb', line 158

def phases_with_ranges
  phases.map(&method(:to_phase_with_range)).to_h
end

#stage_after(stage) ⇒ Object



124
125
126
127
128
129
# File 'lib/acts_as_living/methods_definer.rb', line 124

def stage_after(stage)
  return if dying?(stage)

  index = stage_keys.find_index(stage)
  stage_keys[index + 1]
end

#stage_before(stage) ⇒ Object



131
132
133
134
135
136
# File 'lib/acts_as_living/methods_definer.rb', line 131

def stage_before(stage)
  return if first_stage?(stage)

  index = stage_keys.find_index(stage)
  stage_keys[index - 1]
end

#stage_keysObject



107
108
109
# File 'lib/acts_as_living/methods_definer.rb', line 107

def stage_keys
  @stage_keys
end

#stages_after(stage) ⇒ Object



111
112
113
114
115
# File 'lib/acts_as_living/methods_definer.rb', line 111

def stages_after(stage)
  return [] if dying?(stage)

  stages[stage_after(stage)..]
end

#stages_before(stage) ⇒ Object



117
118
119
120
121
122
# File 'lib/acts_as_living/methods_definer.rb', line 117

def stages_before(stage)
  return [] if first_stage?(stage)

  index = stage_keys.find_index(stage)
  stage_keys[0...index]
end

#to_phase_with_range(phase, delimiter) ⇒ Object



162
163
164
# File 'lib/acts_as_living/methods_definer.rb', line 162

def to_phase_with_range(phase, delimiter)
  [phase, (stages_enum[delimiter.first]..stages_enum[delimiter.last])]
end