Module: Remi::BusinessRules::ParseFormula
Instance Method Summary collapse
- #base_regex ⇒ Object
- #date_reference(formula, captured) ⇒ Object
- #date_reference_match_multiple(form, quantity, unit, direction) ⇒ Object
- #date_reference_match_single_day(form, direction) ⇒ Object
- #date_reference_match_single_unit(form, direction, unit) ⇒ Object
- #formulas ⇒ Object
- #is_formula?(arg) ⇒ Boolean
- #parse(form) ⇒ Object
Instance Method Details
#base_regex ⇒ Object
30 31 32 |
# File 'lib/remi/cucumber/business_rules.rb', line 30 def base_regex @base_regex ||= /\*(.*)\*/ end |
#date_reference(formula, captured) ⇒ Object
59 60 61 62 |
# File 'lib/remi/cucumber/business_rules.rb', line 59 def date_reference(formula, captured) parsed = self.send("date_reference_#{formula}", *captured) Date.current.send("#{parsed[:unit]}_#{parsed[:direction]}", parsed[:quantity]).strftime('%Y-%m-%d') end |
#date_reference_match_multiple(form, quantity, unit, direction) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/remi/cucumber/business_rules.rb', line 80 def date_reference_match_multiple(form, quantity, unit, direction) { quantity: quantity.to_i, unit: unit.downcase.pluralize, direction: { 'ago' => 'ago', 'from now' => 'since' }[direction.downcase] } end |
#date_reference_match_single_day(form, direction) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/remi/cucumber/business_rules.rb', line 64 def date_reference_match_single_day(form, direction) { quantity: direction.downcase == 'today' ? 0 : 1, unit: 'days', direction: { 'today' => 'ago', 'yesterday' => 'ago', 'tomorrow' => 'since' }[direction.downcase] } end |
#date_reference_match_single_unit(form, direction, unit) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/remi/cucumber/business_rules.rb', line 72 def date_reference_match_single_unit(form, direction, unit) { quantity: direction.downcase == 'this' ? 0 : 1, unit: unit.downcase.pluralize, direction: { 'this' => 'ago', 'last' => 'ago', 'previous' => 'ago', 'next' => 'since' }[direction.downcase] } end |
#formulas ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/remi/cucumber/business_rules.rb', line 34 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
26 27 28 |
# File 'lib/remi/cucumber/business_rules.rb', line 26 def is_formula?(arg) !base_regex.match(arg).nil? end |
#parse(form) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/remi/cucumber/business_rules.rb', line 42 def parse(form) return form unless is_formula?(form) form_opt = formulas[form, :match] raise "Unknown formula #{form}" unless form_opt 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 |