Sha256: 8aef90c86e0231adad455ea0dd3cac9f429a29bbef283856bb70a2b3ad49fb32

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

module RubyClock::AroundActions

  attr_accessor :around_actions

  def freeze_around_actions
    @around_actions.freeze
  end

  def set_up_around_actions
    @around_actions = []
    def schedule.around_trigger(job_info, &job_proc)
      RubyClock.instance.call_with_around_action_stack(
        RubyClock.instance.around_actions.reverse,
        job_proc,
        job_info
      )
    end

    self.around_trigger_code_location = schedule.method(:around_trigger).source_location
  end

  def ensure_around_trigger_has_not_been_redefined
    if around_trigger_code_location != schedule.method(:around_trigger).source_location
      raise <<~MESSAGE

        You need to change your around_trigger definition to use around_actions instead.

        It's easy and fun! https://github.com/jjb/ruby-clock/blob/main/CHANGELOG.md#migrating-from-ruby-clock-version-1-to-version-2
      MESSAGE
    end
  end

  def call_with_around_action_stack(wrappers, job_proc, job_info)
    case wrappers.count
    when 0
      job_proc.call(job_info)
    else
      call_with_around_action_stack(
        wrappers[1..],
        Proc.new{ wrappers.first.call(job_proc, job_info) },
        job_info
      )
    end
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ruby-clock-2.0.1 lib/ruby-clock/around_actions.rb
ruby-clock-2.0.0 lib/ruby-clock/around_actions.rb
ruby-clock-2.0.0.beta10 lib/ruby-clock/around_actions.rb
ruby-clock-2.0.0.beta9 lib/ruby-clock/around_actions.rb
ruby-clock-2.0.0.beta8 lib/ruby-clock/around_actions.rb
ruby-clock-2.0.0.beta7 lib/ruby-clock/around_actions.rb
ruby-clock-2.0.0.beta6 lib/ruby-clock/around_actions.rb
ruby-clock-2.0.0.beta5 lib/ruby-clock/around_actions.rb