Class: IceCubeCron::ExpressionParser
- Inherits:
-
Object
- Object
- IceCubeCron::ExpressionParser
- Includes:
- ParserAttribute
- Defined in:
- lib/ice_cube_cron/expression_parser.rb
Overview
Parses the incoming expression and splits it into meaningful parts.
Constant Summary collapse
- EXPRESSION_PART_DEFAULTS =
{ :interval => 1, :year => nil, :month => nil, :day_of_month => nil, :day_of_week => nil, :hour => nil, :minute => nil, :until => nil }.freeze
- EXPRESSION_PART_KEYS =
[ :minute, :hour, :day_of_month, :month, :day_of_week, :year ].freeze
Class Method Summary collapse
-
.sanitize_day_param(param) ⇒ Object
Sanitize given value to a valid day parameter.
-
.sanitize_integer_array_param(param) ⇒ Object
Sanitize given value to an integer array.
-
.sanitize_integer_param(param, default = nil) ⇒ Object
Sanitize given value to an integer.
-
.sanitize_week_day_param(param) ⇒ Object
Sanitize given value to a valid weekday parameter.
Instance Method Summary collapse
-
#initialize(*expression) ⇒ ExpressionParser
constructor
Create a parsed expression.
Methods included from ParserAttribute
Constructor Details
#initialize(*expression) ⇒ ExpressionParser
Create a parsed expression
Takes a hash of cron expression parts.
### Expression values:
-
interval
-
year
-
month
-
day
-
weekday
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ice_cube_cron/expression_parser.rb', line 39 def initialize(*expression) expression_parts = expression.last.is_a?(::Hash) ? expression.last : {} expression_str = expression[0].is_a?(::String) ? expression[0] : nil expression_parts.merge!(string_to_expression_parts(expression_str)) self.expression_hash = EXPRESSION_PART_DEFAULTS.dup expression_parts.each do |name, val| begin send("#{name}=", val) rescue NoMethodError raise ArgumentError, "Invalid parameter: #{name}" end end end |
Class Method Details
.sanitize_day_param(param) ⇒ Object
Sanitize given value to a valid day parameter
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ice_cube_cron/expression_parser.rb', line 93 def self.sanitize_day_param(param) return nil if param.blank? return param if param.is_a?(::Array) return [param] if param.is_a?(::Integer) param.to_s.split(',').map do |element| next -1 if element == 'L' ExpressionParser.sanitize_integer_array_param(element) end.flatten.uniq end |
.sanitize_integer_array_param(param) ⇒ Object
Sanitize given value to an integer array
– rubocop:disable Metrics/AbcSize ++
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ice_cube_cron/expression_parser.rb', line 120 def self.sanitize_integer_array_param(param) return nil if param.blank? return param if param.is_a?(::Array) return [param] if param.is_a?(::Integer) param.split(',').map do |element| if element =~ /[0-9]+\-[0-9]+/ parts = element.split('-') (parts[0].to_i..parts[1].to_i).to_a else element.to_i end end.flatten.uniq end |
.sanitize_integer_param(param, default = nil) ⇒ Object
Sanitize given value to an integer
108 109 110 111 112 |
# File 'lib/ice_cube_cron/expression_parser.rb', line 108 def self.sanitize_integer_param(param, default = nil) return default if param.blank? param.to_i end |
.sanitize_week_day_param(param) ⇒ Object
Sanitize given value to a valid weekday parameter
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/ice_cube_cron/expression_parser.rb', line 139 def self.sanitize_week_day_param(param) return nil if param.blank? param.to_s.split(',').map do |element| if element =~ /[0-9]+#[0-9]+/ parts = element.split('#') { ExpressionParser.sanitize_integer_param(parts[0]) => ExpressionParser.sanitize_integer_array_param(parts[1]) } elsif element =~ /[0-9]+L/ { ExpressionParser.sanitize_integer_param(element) => [-1] } else ExpressionParser.sanitize_integer_param(element) end end end |