Class: Montrose::Options

Inherits:
Object
  • 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!, parse_time

Constructor Details

#initialize(opts = {}) ⇒ Options

Returns a new instance of Options


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/montrose/options.rb', line 87

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
  }

  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


26
27
28
# File 'lib/montrose/options.rb', line 26

def default_every
  @default_every
end

.default_startsObject

Return the default starting time.

Examples:

Recurrence.default_starts #=> <Date>


47
48
49
# File 'lib/montrose/options.rb', line 47

def default_starts
  @default_starts
end

.default_untilObject

Return the default ending time.

Examples:

Recurrence.default_until #=> <Date>


32
33
34
# File 'lib/montrose/options.rb', line 32

def default_until
  @default_until
end

Class Method Details

.def_option(name) ⇒ Object


20
21
22
23
24
# File 'lib/montrose/options.rb', line 20

def def_option(name)
  defined_options << name.to_sym
  attr_accessor name
  protected :"#{name}="
end

.default_optionsObject


64
65
66
67
68
69
# File 'lib/montrose/options.rb', line 64

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

.defined_optionsObject


16
17
18
# File 'lib/montrose/options.rb', line 16

def defined_options
  @defined_options ||= []
end

.merge(opts = {}) ⇒ Object


60
61
62
# File 'lib/montrose/options.rb', line 60

def merge(opts = {})
  new(default_options).merge(opts)
end

.new(options = {}) ⇒ Object


11
12
13
14
# File 'lib/montrose/options.rb', line 11

def new(options = {})
  return options if options.is_a?(self)
  super
end

Instance Method Details

#[](option) ⇒ Object


117
118
119
# File 'lib/montrose/options.rb', line 117

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

#[]=(option, val) ⇒ Object


113
114
115
# File 'lib/montrose/options.rb', line 113

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

#at=(time) ⇒ Object


196
197
198
# File 'lib/montrose/options.rb', line 196

def at=(time)
  @at = map_arg(time) { |t| as_time_parts(t) }
end

#betweenObject


190
191
192
193
194
# File 'lib/montrose/options.rb', line 190

def between
  return nil unless self[:starts] && self[:until]

  (self[:starts]..self[:until])
end

#between=(range) ⇒ Object


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

def between=(range)
  self[:starts] = range.first
  self[:until] = range.last
end

#day=(days) ⇒ Object


165
166
167
# File 'lib/montrose/options.rb', line 165

def day=(days)
  @day = nested_map_arg(days) { |d| day_number!(d) }
end

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


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

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

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

  @every = parsed.fetch(:every)
end

#except=(date) ⇒ Object


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

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

#fetch(key, *args, &_block) ⇒ Object


128
129
130
131
132
133
134
135
136
# File 'lib/montrose/options.rb', line 128

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


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

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

#inspectObject


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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#mday=(mdays) ⇒ Object


169
170
171
# File 'lib/montrose/options.rb', line 169

def mday=(mdays)
  @mday = map_mdays(mdays)
end

#merge(other) ⇒ Object


121
122
123
124
125
126
# File 'lib/montrose/options.rb', line 121

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

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

#month=(months) ⇒ Object


181
182
183
# File 'lib/montrose/options.rb', line 181

def month=(months)
  @month = map_arg(months) { |d| month_number!(d) }
end

#on=(arg) ⇒ Object


200
201
202
203
204
205
206
# File 'lib/montrose/options.rb', line 200

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


216
217
218
219
220
221
222
223
224
# File 'lib/montrose/options.rb', line 216

def start_time
  time = starts || default_starts

  if at
    at.map { |(hour, min)| time.change(hour: hour, min: min) }.min || time
  else
    time
  end
end

#starts=(time) ⇒ Object


153
154
155
# File 'lib/montrose/options.rb', line 153

def starts=(time)
  @starts = as_time(time) || self.class.default_starts
end

#to_hashObject Also known as: to_h


105
106
107
108
109
110
# File 'lib/montrose/options.rb', line 105

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


157
158
159
# File 'lib/montrose/options.rb', line 157

def until=(time)
  @until = as_time(time) || self.class.default_until
end

#week=(weeks) ⇒ Object


177
178
179
# File 'lib/montrose/options.rb', line 177

def week=(weeks)
  @week = map_arg(weeks) { |w| assert_week(w) }
end

#yday=(ydays) ⇒ Object


173
174
175
# File 'lib/montrose/options.rb', line 173

def yday=(ydays)
  @yday = map_ydays(ydays)
end