Sha256: 6e0e30f311f8502c2a3c907439fff00ee99d1c2b5bbcb5d294a36d1bb0b380f3

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'polling/engine'
require 'polling/sleep'
require 'polling/validate'
require 'polling/utils'

module Polling
  class << self
    def setting opts={}
      set_instance_variables opts
    end

    def run interval=@interval, debug=false
      @offset ||= 0
      @debug ||= debug
      e = Engine.new
      e.offset = @offset

      exec = lambda do |time|
        yield if block_given?
        e.target = e.increment! time
        Sleep.exec e.stime(:debug=>@debug)
      end

      exec_arr = lambda do |time_arr|
        time_arr.each do |time|
          e.target = e.substitution! time
          Sleep.exec e.stime(:debug=>@debug)
          yield if block_given?
        end
      end

      interval = Validate.value interval
      e.__send__(:start_print, e.stime)

      case interval
      when Array
        loop { exec_arr.call interval }
      else
        Sleep.exec e.stime(:debug=>@debug)
        loop { exec.call interval }
      end
    rescue => ex
      $stderr.puts ex.to_s
    end

    def async_run interval=@interval, debug=false
      @debug ||= debug
      e = Engine.new

      exec = lambda do |time|
        before = Time.now
        yield if block_given?
        opts = {
          interval: time,
          before: before,
          after: Time.now,
          debug: @debug
        }
        stime = e.stime_async opts
        Sleep.exec stime
      end

      interval = Validate.value interval
      if interval.class == Array
        raise "Can't input Array class : #{interval}"
      end

      e.__send__(:start_print, 0)
      loop { exec.call interval }
    rescue => ex
      $stderr.puts ex.to_s
    end

    private

    def set_instance_variables variables
      variables.each do |key,var|
        instance_variable_set "@#{key}", var
      end
    end

    alias start run
    alias sync_run run
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polling-0.1.3 lib/polling.rb