Class: Cronos::Parser

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

Overview

Usage:

cron = Cronos::Interval.new.at('11pm').on_days(:monday, :tuesday).to_s # => '0 23 * * 1,2'
Cronos::Parser.new(cron).now? # => true or false

Instance Method Summary collapse

Constructor Details

#initialize(cron_string) ⇒ Parser

The cron_string should ideally come from Cronos::Interval#to_s



361
362
363
# File 'lib/cronos.rb', line 361

def initialize(cron_string)
  @cron_string = cron_string
end

Instance Method Details

#now?(time = Time.now) ⇒ Boolean

Returns true or false depending if the time matches the cron string given on initialization

Returns:

  • (Boolean)


368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/cronos.rb', line 368

def now?(time = Time.now)
  min, hour, day, month, wday = @cron_string.split ' '
  
  [:min, :hour, :day, :month, :wday].all? do |period|
    p = eval(period.to_s)
    now = eval("time.#{period}")
    p.gsub! /\*/, now.to_s
    
    p.split(',').collect{ |parts|
      if parts[/-/]
        eval(parts.gsub(/-/, '..')).to_a.include? now
      elsif parts[/\//]
        eval(parts.gsub(/\//, '%')).zero?
      else
        parts.to_s == now.to_s
      end
    }.flatten.include? true
  end
end