Class: Sequins::Sequence
- Inherits:
-
Object
- Object
- Sequins::Sequence
- Defined in:
- lib/sequins/sequence.rb
Defined Under Namespace
Classes: StepProxy
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #add_hook(stage, &block) ⇒ Object
- #add_step(name, options = {}, &block) ⇒ Object
- #delay(duration, target, options) ⇒ Object
-
#initialize(klass) ⇒ Sequence
constructor
A new instance of Sequence.
- #prepend_hook(stage, &block) ⇒ Object
- #run_hooks_for_target(stage, target, step_name) ⇒ Object
- #run_step_for_target(step_name, target, *args) ⇒ Object
- #trigger(target, *args) ⇒ Object
Constructor Details
#initialize(klass) ⇒ Sequence
Returns a new instance of Sequence.
7 8 9 10 11 |
# File 'lib/sequins/sequence.rb', line 7 def initialize(klass) @klass = klass @steps = {} @hooks = {} end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/sequins/sequence.rb', line 5 def klass @klass end |
Instance Method Details
#add_hook(stage, &block) ⇒ Object
17 18 19 20 |
# File 'lib/sequins/sequence.rb', line 17 def add_hook(stage, &block) @hooks[stage] ||= [] @hooks[stage] << StepProxy.new({}, block) end |
#add_step(name, options = {}, &block) ⇒ Object
13 14 15 |
# File 'lib/sequins/sequence.rb', line 13 def add_step(name, ={}, &block) @steps[name] = StepProxy.new(, block) end |
#delay(duration, target, options) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sequins/sequence.rb', line 55 def delay(duration, target, ) if target.respond_to?(:local_time_zone) zone = ActiveSupport::TimeZone[target.local_time_zone] else zone = ActiveSupport::TimeZone[Sequins.configuration.default_time_zone] end delay_until = zone.now + duration if [:only] == :weekdays current_wday = delay_until.wday if current_wday == 0 delay_until += 1.day elsif current_wday == 6 delay_until += 2.days end end if ![:at].nil? tod = Tod::TimeOfDay.parse([:at]) delay_until = delay_until.to_date.at(tod, zone) end next_step = [:then] Sequins.schedule_delay(delay_until, @klass, target, next_step) end |
#prepend_hook(stage, &block) ⇒ Object
22 23 24 25 |
# File 'lib/sequins/sequence.rb', line 22 def prepend_hook(stage, &block) @hooks[stage] ||= [] @hooks[stage].unshift StepProxy.new({}, block) end |
#run_hooks_for_target(stage, target, step_name) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/sequins/sequence.rb', line 46 def run_hooks_for_target(stage, target, step_name) return if @hooks[stage].nil? || @hooks[stage].empty? @hooks[stage].each do |hook| step = Docile.dsl_eval(Sequins::Step.new(target, self, step_name), &(hook.block)) return false if step.sequence_ended? end end |
#run_step_for_target(step_name, target, *args) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sequins/sequence.rb', line 27 def run_step_for_target(step_name, target, *args) proxy = @steps[step_name] raise UnknownStepError.new(step_name) if proxy.nil? unless run_hooks_for_target(:before_each_step, target, step_name) run_hooks_for_target(:after_sequence, target, :_after_sequence) return false end step = Docile.dsl_eval(Sequins::Step.new(target, self, step_name), args, &(proxy.block)) ended_after_each = !run_hooks_for_target(:after_each_step, target, step_name) if step.sequence_ended? || ended_after_each run_hooks_for_target(:after_sequence, target, :_after_sequence) return false end end |
#trigger(target, *args) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/sequins/sequence.rb', line 84 def trigger(target, *args) = {} if args.last.is_a?(Hash) = args.pop end if [:override_initial_step].present? step_name = [:override_initial_step] else step_name, _ = @steps.detect { |_, s| s.[:initial] } end raise NoInitialStepError.new unless step_name.present? unless run_hooks_for_target(:before_sequence, target, :_before_sequence) run_hooks_for_target(:after_sequence, target, :_after_sequence) return false end run_step_for_target(step_name, target, *args) end |