Class: SayWhen::CronValue
- Inherits:
-
Object
- Object
- SayWhen::CronValue
- Defined in:
- lib/say_when/cron_expression.rb
Direct Known Subclasses
DaysOfMonthCronValue, DaysOfWeekCronValue, HoursCronValue, MinutesCronValue, MonthsCronValue, SecondsCronValue, YearsCronValue
Instance Attribute Summary collapse
-
#expression ⇒ Object
Returns the value of attribute expression.
-
#max ⇒ Object
Returns the value of attribute max.
-
#min ⇒ Object
Returns the value of attribute min.
-
#part ⇒ Object
Returns the value of attribute part.
-
#values ⇒ Object
Returns the value of attribute values.
Class Method Summary collapse
-
.parse_number(min, max, val) ⇒ Object
works for secs, mins, hours.
Instance Method Summary collapse
- #include?(date) ⇒ Boolean
-
#initialize(p, min, max, exp) ⇒ CronValue
constructor
A new instance of CronValue.
- #parse(exp) ⇒ Object
- #to_s ⇒ Object
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
#expression ⇒ Object
Returns the value of attribute expression.
122 123 124 |
# File 'lib/say_when/cron_expression.rb', line 122 def expression @expression end |
#max ⇒ Object
Returns the value of attribute max.
122 123 124 |
# File 'lib/say_when/cron_expression.rb', line 122 def max @max end |
#min ⇒ Object
Returns the value of attribute min.
122 123 124 |
# File 'lib/say_when/cron_expression.rb', line 122 def min @min end |
#part ⇒ Object
Returns the value of attribute part.
122 123 124 |
# File 'lib/say_when/cron_expression.rb', line 122 def part @part end |
#values ⇒ Object
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
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_s ⇒ Object
137 138 139 |
# File 'lib/say_when/cron_expression.rb', line 137 def to_s "[e:#{expression}, v:#{values.inspect}]\n" end |