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
29 30 31 |
# File 'lib/remi/cucumber/business_rules.rb', line 29 def base_regex @base_regex ||= /\A\*(.*)\*\Z/ end |
#date_reference(formula, captured) ⇒ Object
53 54 55 56 |
# File 'lib/remi/cucumber/business_rules.rb', line 53 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
74 75 76 77 78 79 80 |
# File 'lib/remi/cucumber/business_rules.rb', line 74 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
58 59 60 61 62 63 64 |
# File 'lib/remi/cucumber/business_rules.rb', line 58 def date_reference_match_single_day(form, direction) { quantity: 1, unit: 'days', direction: { 'yesterday' => 'ago', 'tomorrow' => 'since' }[direction.downcase] } end |
#date_reference_match_single_unit(form, direction, unit) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/remi/cucumber/business_rules.rb', line 66 def date_reference_match_single_unit(form, direction, unit) { quantity: 1, unit: unit.downcase.pluralize, direction: { 'last' => 'ago', 'previous' => 'ago', 'next' => 'since' }[direction.downcase] } end |
#formulas ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/remi/cucumber/business_rules.rb', line 33 def formulas @formulas ||= Remi::Lookup::RegexSieve.new({ /(yesterday|tomorrow)/i => [:date_reference, :match_single_day], /(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
25 26 27 |
# File 'lib/remi/cucumber/business_rules.rb', line 25 def is_formula?(arg) !base_regex.match(arg).nil? end |
#parse(form) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/remi/cucumber/business_rules.rb', line 41 def parse(form) return form unless is_formula?(form) form_opt = formulas[form, :match] raise "Unknown formula #{form}" unless form_opt if form_opt[:value][0] == :date_reference date_reference(form_opt[:value][1], form_opt[:match]) end end |