Class: SayWhen::CronValue

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p, min, max, exp) ⇒ CronValue

Returns a new instance of CronValue.



124
125
126
127
128
129
130
131
# File 'lib/say_when/cron_expression.rb', line 124

def initialize(p, min, max, exp)
  self.part = p
  self.min = min
  self.max = max
  self.values = []
  self.expression = exp
  parse(exp)
end

Instance Attribute Details

#expressionObject

Returns the value of attribute expression.



122
123
124
# File 'lib/say_when/cron_expression.rb', line 122

def expression
  @expression
end

#maxObject

Returns the value of attribute max.



122
123
124
# File 'lib/say_when/cron_expression.rb', line 122

def max
  @max
end

#minObject

Returns the value of attribute min.



122
123
124
# File 'lib/say_when/cron_expression.rb', line 122

def min
  @min
end

#partObject

Returns the value of attribute part.



122
123
124
# File 'lib/say_when/cron_expression.rb', line 122

def part
  @part
end

#valuesObject

Returns the value of attribute values.



122
123
124
# File 'lib/say_when/cron_expression.rb', line 122

def values
  @values
end

Class Method Details

.parse_number(min, max, val) ⇒ Object

works for secs, mins, hours



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/say_when/cron_expression.rb', line 146

def self.parse_number(min, max, val)
  values = []
  case val
    #check for a '/' for increments
    when /(.+)\/(\d+)/
      (( $1 == "*") ? min : $1.to_i).step(max, $2.to_i) { |x| values << x }

    #check for ',' for list of values
    when /(\d+)(,\d+)+/
      values = val.split(',').map{ |v| v.to_i }.sort

    #check for '-' for range of values
    when /(\d+)-(\d+)/
      values = (($1.to_i)..($2.to_i)).to_a

    #check for '*' for all values between min and max
    when /^(\*)$/
      values = (min..max).to_a

    #lastly, should just be a number
    when /^(\d+)$/
      values << $1.to_i

    #if nothing else, leave values as []
    else values = []
  end
  values
end

Instance Method Details

#include?(date) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/say_when/cron_expression.rb', line 141

def include?(date)
  values.include?(date.send(part))
end

#parse(exp) ⇒ Object



133
134
135
# File 'lib/say_when/cron_expression.rb', line 133

def parse(exp)
  self.values = CronValue.parse_number(min, max, exp.upcase)
end

#to_sObject



137
138
139
# File 'lib/say_when/cron_expression.rb', line 137

def to_s
  "[e:#{expression}, v:#{values.inspect}]\n"
end