Class: Montrose::Options

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/montrose/options.rb

Constant Summary

Constants included from Utils

Utils::MAX_DAYS_IN_MONTH, Utils::MAX_DAYS_IN_YEAR, Utils::MAX_HOURS_IN_DAY, Utils::MAX_WEEKS_IN_YEAR

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

as_date, as_time, current_time, days_in_month, days_in_year, normalize_time, parse_time, to_index

Constructor Details

#initialize(opts = {}) ⇒ Options

Returns a new instance of Options.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/montrose/options.rb', line 106

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,
    week_start: nil,
    exclude_end: nil
  }

  options = defaults.merge(opts || {})
  options.each { |(k, v)| self[k] ||= v unless v.nil? }
end

Class Attribute Details

.default_everyObject

Returns the value of attribute default_every.



28
29
30
# File 'lib/montrose/options.rb', line 28

def default_every
  @default_every
end

.default_startsObject

Return the default starting time.

Examples:

Recurrence.default_starts #=> <Date>



55
56
57
# File 'lib/montrose/options.rb', line 55

def default_starts
  ::Montrose::Utils.normalize_time determine_default_starts
end

.default_untilObject

Return the default ending time.

Examples:

Recurrence.default_until #=> <Date>



35
36
37
# File 'lib/montrose/options.rb', line 35

def default_until
  ::Montrose::Utils.normalize_time determine_default_until
end

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_optionsObject



77
78
79
80
81
82
# File 'lib/montrose/options.rb', line 77

def default_options
  {
    until: default_until,
    interval: 1
  }
end

.defined_optionsObject



18
19
20
# File 'lib/montrose/options.rb', line 18

def defined_options
  @defined_options ||= []
end

.determine_default_startsObject

private



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_untilObject

private



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



138
139
140
# File 'lib/montrose/options.rb', line 138

def [](option)
  send(:"#{option}")
end

#[]=(option, val) ⇒ Object



134
135
136
# File 'lib/montrose/options.rb', line 134

def []=(option, val)
  send(:"#{option}=", val)
end

#at=(time) ⇒ Object



233
234
235
# File 'lib/montrose/options.rb', line 233

def at=(time)
  @at = map_arg(time) { |t| time_of_day_parse(t).parts }
end

#between=(range) ⇒ Object



225
226
227
228
229
230
231
# File 'lib/montrose/options.rb', line 225

def between=(range)
  if Montrose.enable_deprecated_between_masking?
    @covering = range
  end
  self[:starts] = range.first unless self[:starts]
  self[:until] = range.last unless self[:until]
end

#day=(days) ⇒ Object



205
206
207
# File 'lib/montrose/options.rb', line 205

def day=(days)
  @day = Day.parse(days)
end

#during=(during_arg) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/montrose/options.rb', line 191

def during=(during_arg)
  @during = decompose_during_arg(during_arg)
    .each_with_object([]) { |(time_of_day_first, time_of_day_last), all|
    if time_of_day_last < time_of_day_first
      all.push(
        [time_of_day_first.parts, end_of_day.parts],
        [beginning_of_day.parts, time_of_day_last.parts]
      )
    else
      all.push([time_of_day_first.parts, time_of_day_last.parts])
    end
  }.presence
end

#every=(arg) ⇒ Object Also known as: frequency=



164
165
166
167
168
169
170
# File 'lib/montrose/options.rb', line 164

def every=(arg)
  parsed = parse_frequency(arg)

  self[:interval] = parsed[:interval] if parsed[:interval]

  @every = parsed.fetch(:every)
end

#except=(date) ⇒ Object



245
246
247
# File 'lib/montrose/options.rb', line 245

def except=(date)
  @except = map_arg(date) { |d| as_date(d) }
end

#fetch(key, *args) ⇒ Object

Raises:

  • (ArgumentError)


149
150
151
152
153
154
155
156
157
158
# File 'lib/montrose/options.rb', line 149

def fetch(key, *args)
  raise 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
  raise KeyError, "Key #{key.inspect} not found" unless block_given?

  yield
end

#hour=(hours) ⇒ Object



187
188
189
# File 'lib/montrose/options.rb', line 187

def hour=(hours)
  @hour = map_arg(hours) { |h| assert_hour(h) }
end

#inspectObject



249
250
251
# File 'lib/montrose/options.rb', line 249

def inspect
  "#<#{self.class} #{to_h.inspect}>"
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/montrose/options.rb', line 160

def key?(key)
  respond_to?(key) && !send(key).nil?
end

#mday=(mdays) ⇒ Object



209
210
211
# File 'lib/montrose/options.rb', line 209

def mday=(mdays)
  @mday = MonthDay.parse(mdays)
end

#merge(other) ⇒ Object



142
143
144
145
146
147
# File 'lib/montrose/options.rb', line 142

def merge(other)
  h1 = to_hash
  h2 = other.to_hash

  self.class.new(h1.merge(h2))
end

#minute=(minutes) ⇒ Object



183
184
185
# File 'lib/montrose/options.rb', line 183

def minute=(minutes)
  @minute = Minute.parse(minutes)
end

#month=(months) ⇒ Object



221
222
223
# File 'lib/montrose/options.rb', line 221

def month=(months)
  @month = Month.parse(months)
end

#on=(arg) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/montrose/options.rb', line 237

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_timeObject



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/montrose/options.rb', line 253

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



175
176
177
# File 'lib/montrose/options.rb', line 175

def starts=(time)
  @starts = normalize_time(as_time(time)) || default_starts
end

#to_hashObject Also known as: to_h



126
127
128
129
130
131
# File 'lib/montrose/options.rb', line 126

def to_hash
  hash_pairs = self.class.defined_options.flat_map { |opt_name|
    [opt_name, send(opt_name)]
  }
  Hash[*hash_pairs].reject { |_k, v| v.nil? }
end

#until=(time) ⇒ Object



179
180
181
# File 'lib/montrose/options.rb', line 179

def until=(time)
  @until = normalize_time(as_time(time)) || default_until
end

#week=(weeks) ⇒ Object



217
218
219
# File 'lib/montrose/options.rb', line 217

def week=(weeks)
  @week = Week.parse(weeks)
end

#yday=(ydays) ⇒ Object



213
214
215
# File 'lib/montrose/options.rb', line 213

def yday=(ydays)
  @yday = YearDay.parse(ydays)
end