Module: Remi::BusinessRules::ParseFormula
Instance Method Summary collapse
- #base_regex ⇒ Object
- #date_reference(formula, captured) ⇒ Object
- #date_reference_match_multiple(form, quantity, unit, direction, format = nil) ⇒ Object
- #date_reference_match_single_day(form, direction, format = nil) ⇒ Object
- #date_reference_match_single_unit(form, direction, unit, format = nil) ⇒ Object
- #formulas ⇒ Object
- #is_formula?(arg) ⇒ Boolean
- #parse(form) ⇒ Object
- #parse_colon_date_format(str) ⇒ Object
Instance Method Details
#base_regex ⇒ Object
26 27 28 |
# File 'lib/remi/cucumber/business_rules.rb', line 26 def base_regex @base_regex ||= /\*(.*)\*/ end |
#date_reference(formula, captured) ⇒ Object
55 56 57 58 |
# File 'lib/remi/cucumber/business_rules.rb', line 55 def date_reference(formula, captured) parsed = self.send("date_reference_#{formula}", *captured) Date.current.send("#{parsed[:unit]}_#{parsed[:direction]}", parsed[:quantity]).strftime(parsed[:format]) end |
#date_reference_match_multiple(form, quantity, unit, direction, format = nil) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/remi/cucumber/business_rules.rb', line 82 def date_reference_match_multiple(form, quantity, unit, direction, format=nil) { quantity: quantity.to_i, unit: unit.downcase.pluralize, direction: { 'ago' => 'ago', 'from now' => 'since' }[direction.downcase], format: parse_colon_date_format(format) } end |
#date_reference_match_single_day(form, direction, format = nil) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/remi/cucumber/business_rules.rb', line 64 def date_reference_match_single_day(form, direction, format=nil) { quantity: direction.downcase == 'today' ? 0 : 1, unit: 'days', direction: { 'today' => 'ago', 'yesterday' => 'ago', 'tomorrow' => 'since' }[direction.downcase], format: parse_colon_date_format(format) } end |
#date_reference_match_single_unit(form, direction, unit, format = nil) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/remi/cucumber/business_rules.rb', line 73 def date_reference_match_single_unit(form, direction, unit, format=nil) { quantity: direction.downcase == 'this' ? 0 : 1, unit: unit.downcase.pluralize, direction: { 'this' => 'ago', 'last' => 'ago', 'previous' => 'ago', 'next' => 'since' }[direction.downcase], format: parse_colon_date_format(format) } end |
#formulas ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/remi/cucumber/business_rules.rb', line 30 def formulas @formulas ||= RegexSieve.new({ /(today|yesterday|tomorrow)(|:[^*]+)\*/i => [:date_reference, :match_single_day], /(this|last|previous|next) (day|month|year|week)(|:[^*]+)\*/i => [:date_reference, :match_single_unit], /(\d+)\s(day|days|month|months|year|years|week|weeks) (ago|from now)(|:[^*]+)\*/i => [:date_reference, :match_multiple] }) end |
#is_formula?(arg) ⇒ Boolean
22 23 24 |
# File 'lib/remi/cucumber/business_rules.rb', line 22 def is_formula?(arg) !base_regex.match(arg).nil? end |
#parse(form) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/remi/cucumber/business_rules.rb', line 38 def parse(form) return form unless is_formula?(form) form_opt = formulas[form, :match] raise "Unknown formula #{form}" unless form_opt[:match] to_replace = form.match(base_regex)[0] replace_with = if form_opt[:value][0] == :date_reference date_reference(form_opt[:value][1], form_opt[:match]) else to_replace end form.gsub(to_replace, replace_with) end |
#parse_colon_date_format(str) ⇒ Object
60 61 62 |
# File 'lib/remi/cucumber/business_rules.rb', line 60 def parse_colon_date_format(str) str.blank? ? '%Y-%m-%d' : str.slice(1..-1).strip end |