Class: Sequins::Sequence

Inherits:
Object
  • Object
show all
Defined in:
lib/sequins/sequence.rb

Defined Under Namespace

Classes: StepProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#klassObject (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, options={}, &block)
  @steps[name] = StepProxy.new(options, 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, options)
  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 options[: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 !options[:at].nil?
    tod = Tod::TimeOfDay.parse(options[:at])
    delay_until = delay_until.to_date.at(tod, zone)
  end

  next_step = options[: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

Raises:



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

Raises:



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)
  options = {}
  if args.last.is_a?(Hash)
    options = args.pop
  end

  if options[:override_initial_step].present?
    step_name = options[:override_initial_step]
  else
    step_name, _ = @steps.detect { |_, s| s.options[: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