Sha256: 05060f6772545b877b53cf3e4541739dfdb14acc34a5e22c639d9d994abd2f74

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'time'

module Resque
  module Plugins
    module Timeframe

      WEEK = [ :sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday ].freeze

      def week
        WEEK
      end

      def settings
        @options ||= WEEK.inject({:default => true}) {|c,v| c.merge({v => true})}
      end

      def timeframe(options={})
        settings.merge!(options)
      end

      def allowed_at?(weekday)
        case settings[weekday]
          when Range
            range(settings[weekday]).include?(Time.now)
          when FalseClass, TrueClass
            settings[weekday] && settings[:default]
          else
            settings[:default]
        end
      end

      def time_at(hr)
        t = Time.now
        if (0..23).include?(hr)
          Time.mktime(t.year, t.month, t.day, hr, 0, 0)
        else
          Time.mktime(t.year, t.month, t.day + 1, 0, 0, 0)
        end
      end

      def range(date_range)
        case date_range.begin
          when Integer
            time_at(date_range.begin)..time_at(date_range.end)
          when String
            Time.parse(date_range.begin)..Time.parse(date_range.end)
          when Time
            date_range
          else
            time_at(0)..time_at(24)
        end
      end

      def before_perform_timeframe(*args)
        raise Resque::Job::DontPerform unless allowed_at?(week[Time.new.wday])
      end

    end

    class TimeframedJob
      extend Timeframe
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resque-timeframe-0.1.0 lib/resque-timeframe/timeframe.rb