Class: Montrose::Options
- Inherits:
-
Object
show all
- Includes:
- Utils
- Defined in:
- lib/montrose/options.rb
Constant Summary
Constants included
from Utils
Utils::DAYS, Utils::MAX_DAYS_IN_MONTH, Utils::MAX_DAYS_IN_YEAR, Utils::MAX_HOURS_IN_DAY, Utils::MAX_WEEKS_IN_YEAR, Utils::MONTHS
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Utils
as_date, as_time, current_time, day_number, day_number!, days_in_month, days_in_year, month_number, month_number!, normalize_time, parse_time, to_index
Constructor Details
#initialize(opts = {}) ⇒ Options
Returns a new instance of Options.
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/montrose/options.rb', line 103
def initialize(opts = {})
defaults = {
every: self.class.default_every,
interval: nil,
starts: nil,
until: nil,
day: nil,
mday: nil,
yday: nil,
week: nil,
month: nil,
total: nil,
exclude_end: nil
}
options = defaults.merge(opts || {})
options.each { |(k, v)| self[k] ||= v unless v.nil? }
end
|
Class Attribute Details
.default_every ⇒ Object
Returns the value of attribute default_every.
28
29
30
|
# File 'lib/montrose/options.rb', line 28
def default_every
@default_every
end
|
.default_starts ⇒ Object
Return the default starting time.
.default_until ⇒ Object
Return the default ending time.
Class Method Details
.def_option(name) ⇒ Object
22
23
24
25
26
|
# File 'lib/montrose/options.rb', line 22
def def_option(name)
defined_options << name.to_sym
attr_accessor name
protected :"#{name}="
end
|
.default_options ⇒ Object
77
78
79
80
81
82
|
# File 'lib/montrose/options.rb', line 77
def default_options
{
until: default_until,
interval: 1
}
end
|
.defined_options ⇒ Object
18
19
20
|
# File 'lib/montrose/options.rb', line 18
def defined_options
@defined_options ||= []
end
|
.determine_default_starts ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/montrose/options.rb', line 60
def determine_default_starts
case @default_starts
when String
::Montrose::Utils.parse_time(@default_starts)
when Proc
@default_starts.call
when nil
::Montrose::Utils.current_time
else
@default_starts
end
end
|
.determine_default_until ⇒ Object
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/montrose/options.rb', line 40
def determine_default_until
case @default_until
when String
::Montrose::Utils.parse_time(@default_until)
when Proc
@default_until.call
else
@default_until
end
end
|
.merge(opts = {}) ⇒ Object
73
74
75
|
# File 'lib/montrose/options.rb', line 73
def merge(opts = {})
new(default_options).merge(opts)
end
|
.new(options = {}) ⇒ Object
12
13
14
15
16
|
# File 'lib/montrose/options.rb', line 12
def new(options = {})
return options if options.is_a?(self)
super
end
|
Instance Method Details
#[](option) ⇒ Object
134
135
136
|
# File 'lib/montrose/options.rb', line 134
def [](option)
send(:"#{option}")
end
|
#[]=(option, val) ⇒ Object
130
131
132
|
# File 'lib/montrose/options.rb', line 130
def []=(option, val)
send(:"#{option}=", val)
end
|
#at=(time) ⇒ Object
218
219
220
|
# File 'lib/montrose/options.rb', line 218
def at=(time)
@at = map_arg(time) { |t| as_time_parts(t) }
end
|
#between=(range) ⇒ Object
212
213
214
215
216
|
# File 'lib/montrose/options.rb', line 212
def between=(range)
@between = range
self[:starts] = range.first unless self[:starts]
self[:until] = range.last unless self[:until]
end
|
#day=(days) ⇒ Object
192
193
194
|
# File 'lib/montrose/options.rb', line 192
def day=(days)
@day = nested_map_arg(days) { |d| day_number!(d) }
end
|
#during=(during) ⇒ Object
183
184
185
186
187
188
189
190
|
# File 'lib/montrose/options.rb', line 183
def during=(during)
@during = case during
when Range
[decompose_during_arg(during)]
else
map_arg(during) { |d| decompose_during_arg(d) }
end
end
|
#every=(arg) ⇒ Object
Also known as:
frequency=
160
161
162
163
164
165
166
|
# File 'lib/montrose/options.rb', line 160
def every=(arg)
parsed = parse_frequency(arg)
self[:interval] = parsed[:interval] if parsed[:interval]
@every = parsed.fetch(:every)
end
|
#except=(date) ⇒ Object
230
231
232
|
# File 'lib/montrose/options.rb', line 230
def except=(date)
@except = map_arg(date) { |d| as_date(d) }
end
|
#fetch(key, *args, &_block) ⇒ Object
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/montrose/options.rb', line 145
def fetch(key, *args, &_block)
fail ArgumentError, "wrong number of arguments (#{args.length} for 1..2)" if args.length > 1
found = send(key)
return found if found
return args.first if args.length == 1
fail "Key #{key.inspect} not found" unless block_given?
yield
end
|
#hour=(hours) ⇒ Object
179
180
181
|
# File 'lib/montrose/options.rb', line 179
def hour=(hours)
@hour = map_arg(hours) { |h| assert_hour(h) }
end
|
#inspect ⇒ Object
234
235
236
|
# File 'lib/montrose/options.rb', line 234
def inspect
"#<#{self.class} #{to_h.inspect}>"
end
|
#key?(key) ⇒ Boolean
156
157
158
|
# File 'lib/montrose/options.rb', line 156
def key?(key)
respond_to?(key) && !send(key).nil?
end
|
#mday=(mdays) ⇒ Object
196
197
198
|
# File 'lib/montrose/options.rb', line 196
def mday=(mdays)
@mday = map_mdays(mdays)
end
|
#merge(other) ⇒ Object
138
139
140
141
142
143
|
# File 'lib/montrose/options.rb', line 138
def merge(other)
h1 = to_hash
h2 = other.to_hash
self.class.new(h1.merge(h2))
end
|
#month=(months) ⇒ Object
208
209
210
|
# File 'lib/montrose/options.rb', line 208
def month=(months)
@month = map_arg(months) { |d| month_number!(d) }
end
|
#on=(arg) ⇒ Object
222
223
224
225
226
227
228
|
# File 'lib/montrose/options.rb', line 222
def on=(arg)
result = decompose_on_arg(arg)
self[:day] = result[:day] if result[:day]
self[:month] = result[:month] if result[:month]
self[:mday] = result[:mday] if result[:mday]
@on = arg
end
|
#start_time ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/montrose/options.rb', line 238
def start_time
time = starts || default_starts
if at
at.map { |hour, min, sec = 0| time.change(hour: hour, min: min, sec: sec) }
.select { |t| t >= time }
.min || time
else
time
end
end
|
#starts=(time) ⇒ Object
171
172
173
|
# File 'lib/montrose/options.rb', line 171
def starts=(time)
@starts = normalize_time(as_time(time)) || default_starts
end
|
#to_hash ⇒ Object
Also known as:
to_h
122
123
124
125
126
127
|
# File 'lib/montrose/options.rb', line 122
def to_hash
hash_pairs = self.class.defined_options.flat_map do |opt_name|
[opt_name, send(opt_name)]
end
Hash[*hash_pairs].reject { |_k, v| v.nil? }
end
|
#until=(time) ⇒ Object
175
176
177
|
# File 'lib/montrose/options.rb', line 175
def until=(time)
@until = normalize_time(as_time(time)) || default_until
end
|
#week=(weeks) ⇒ Object
204
205
206
|
# File 'lib/montrose/options.rb', line 204
def week=(weeks)
@week = map_arg(weeks) { |w| assert_week(w) }
end
|
#yday=(ydays) ⇒ Object
200
201
202
|
# File 'lib/montrose/options.rb', line 200
def yday=(ydays)
@yday = map_ydays(ydays)
end
|