Class: Cronos::Interval
- Inherits:
-
Object
- Object
- Cronos::Interval
- Defined in:
- lib/cronos.rb
Direct Known Subclasses
Defined Under Namespace
Classes: RepeatInterval
Constant Summary collapse
- MONTHS =
[nil, :jan, :feb, :mar, :apr, :may, :jun, :jul, :aug, :sep, :oct, :nov, :dec]
- DAYS =
[:sun, :mon, :tue, :wed, :thu, :fri, :sat]
Instance Attribute Summary collapse
-
#day ⇒ Object
Returns the value of attribute day.
-
#dow ⇒ Object
Returns the value of attribute dow.
-
#hour ⇒ Object
Returns the value of attribute hour.
-
#min ⇒ Object
Returns the value of attribute min.
-
#month ⇒ Object
Returns the value of attribute month.
Instance Method Summary collapse
-
#at(time) ⇒ Object
Time: at(12) at(1.30) at(‘01.30’) at(14.30) at(‘2pm’).
- #daily ⇒ Object (also: #once_a_day)
-
#days(*args) ⇒ Object
(also: #on_days, #on_day)
Days of the week: days(:monday) days(‘Monday’) days(:mon) days(1..3) days(‘mon’..‘wed’) days(1…4) on_day(:monday) days(:mon, :tues) on_days(:mon, :tues).
-
#every(*multiple) ⇒ Object
Repeats an interval: every(10).minutes every(6).hours every(2).months.
- #hourly ⇒ Object
-
#initialize(interval = nil) ⇒ Interval
constructor
A new instance of Interval.
- #midday ⇒ Object (also: #at_midday)
- #midnight ⇒ Object (also: #at_midnight)
- #monthly ⇒ Object (also: #once_a_month)
-
#of(*args) ⇒ Object
(also: #of_months, #in)
Months: of(:january) of(‘January’) of(:jan) of(:jan, :feb, :mar) of(1..3) of(‘jan’..‘mar’) of(1…4) of_months(1, 2, 3) in(:january).
-
#on(*args) ⇒ Object
(also: #on_the)
Days of month: on(13) on(‘13th’) on(13..17) on(‘13th’..‘17th’) on(13…18) on_the(‘13th’).
- #to_hash ⇒ Object
- #to_s ⇒ Object
- #weekdays ⇒ Object
- #weekends ⇒ Object
- #weekly ⇒ Object (also: #once_a_week)
Constructor Details
#initialize(interval = nil) ⇒ Interval
Returns a new instance of Interval.
16 17 18 |
# File 'lib/cronos.rb', line 16 def initialize(interval=nil) @min, @hour, @day, @month, @dow = *parse_cron(interval) if interval end |
Instance Attribute Details
#day ⇒ Object
Returns the value of attribute day.
10 11 12 |
# File 'lib/cronos.rb', line 10 def day @day end |
#dow ⇒ Object
Returns the value of attribute dow.
10 11 12 |
# File 'lib/cronos.rb', line 10 def dow @dow end |
#hour ⇒ Object
Returns the value of attribute hour.
10 11 12 |
# File 'lib/cronos.rb', line 10 def hour @hour end |
#min ⇒ Object
Returns the value of attribute min.
10 11 12 |
# File 'lib/cronos.rb', line 10 def min @min end |
#month ⇒ Object
Returns the value of attribute month.
10 11 12 |
# File 'lib/cronos.rb', line 10 def month @month end |
Instance Method Details
#at(time) ⇒ Object
Time:
at(12)
at(1.30)
at('01.30')
at(14.30)
at('2pm')
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cronos.rb', line 27 def at(time) @hour, @min, meridian = parse_time(time) raise ArgumentError, "invalid hour value for 'at'" if @hour > 12 && meridian raise ArgumentError, "invalid minute value for 'at'" if @min > 59 case meridian when 'am' then @hour = 0 if @hour == 12 when 'pm' then @hour += 12 if @hour < 12 end raise ArgumentError, "invalid hour value for 'at'" if @hour > 23 self end |
#daily ⇒ Object Also known as: once_a_day
136 137 138 139 140 141 |
# File 'lib/cronos.rb', line 136 def daily @min ||= 0 @hour ||= 0 @day = nil self end |
#days(*args) ⇒ Object Also known as: on_days, on_day
Days of the week:
days(:monday)
days('Monday')
days(:mon)
days(1..3)
days('mon'..'wed')
days(1...4)
on_day(:monday)
days(:mon, :tues)
on_days(:mon, :tues)
95 96 97 98 99 100 101 102 103 |
# File 'lib/cronos.rb', line 95 def days(*args) if args.first.is_a?(Range) @dow = format_range(args.first) else list = args.map {|day| day_value(day) unless day.is_a?(Numeric) } @dow = list.join(',') end self end |
#every(*multiple) ⇒ Object
Repeats an interval:
every(10).minutes
every(6).hours
every(2).months
or use as an alias for #on or #days
every(:monday)
every(:mon, :tues)
every('Monday'.. 'Wednesday')
every('February', :march)
every('Feb'..'June')
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cronos.rb', line 53 def every(*multiple) return RepeatInterval.new(multiple.first, self) if multiple.first.is_a?(Numeric) if multiple.all? {|abbr| is_month?(abbr) } of(*multiple) elsif multiple.all? {|abbr| is_day?(abbr) } days(*multiple) else raise ArgumentError, "Unknown interval type passed to #every" end end |
#hourly ⇒ Object
130 131 132 133 134 |
# File 'lib/cronos.rb', line 130 def hourly @min = 0 @hour = nil self end |
#midday ⇒ Object Also known as: at_midday
151 152 153 154 155 |
# File 'lib/cronos.rb', line 151 def midday @min = 0 @hour = 12 self end |
#midnight ⇒ Object Also known as: at_midnight
144 145 146 147 148 |
# File 'lib/cronos.rb', line 144 def midnight @min = 0 @hour = 0 self end |
#monthly ⇒ Object Also known as: once_a_month
168 169 170 171 172 173 174 175 |
# File 'lib/cronos.rb', line 168 def monthly @min ||= 0 @hour ||= 0 @day ||= 1 @month = nil @dow = nil self end |
#of(*args) ⇒ Object Also known as: of_months, in
Months:
of(:january)
of('January')
of(:jan)
of(:jan, :feb, :mar)
of(1..3)
of('jan'..'mar')
of(1...4)
of_months(1, 2, 3)
in(:january)
118 119 120 121 122 123 124 125 126 |
# File 'lib/cronos.rb', line 118 def of(*args) if args.first.is_a?(Range) @month = format_range(args.first) else list = args.map {|month| month_value(month) unless month.is_a?(Numeric) } @month = list.join(',') end self end |
#on(*args) ⇒ Object Also known as: on_the
Days of month:
on(13)
on('13th')
on(13..17)
on('13th'..'17th')
on(13...18)
on_the('13th')
73 74 75 76 77 78 79 80 81 |
# File 'lib/cronos.rb', line 73 def on(*args) if args.first.is_a?(Range) @day = format_range(args.first) else list = args.collect {|day| day.to_s.to_i } @day = list.join(',') end self end |
#to_hash ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/cronos.rb', line 196 def to_hash { :minute => "#{min || '*'}", :hour => "#{hour || '*'}", :day => "#{day || '*'}", :month => "#{month || '*'}", :weekday => "#{dow || '*'}" } end |
#to_s ⇒ Object
192 193 194 |
# File 'lib/cronos.rb', line 192 def to_s "#{min || '*'} #{hour || '*'} #{day || '*'} #{month || '*'} #{dow || '*'}" end |
#weekdays ⇒ Object
178 179 180 181 182 183 |
# File 'lib/cronos.rb', line 178 def weekdays @min ||= 0 @hour ||= 0 @dow = '1-5' self end |
#weekends ⇒ Object
185 186 187 188 189 190 |
# File 'lib/cronos.rb', line 185 def weekends @min ||= 0 @hour ||= 0 @dow = '0,6' self end |
#weekly ⇒ Object Also known as: once_a_week
158 159 160 161 162 163 164 165 |
# File 'lib/cronos.rb', line 158 def weekly @min ||= 0 @hour ||= 0 @dow ||= 0 @day = nil @month = nil self end |