Module: Remi::Testing::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
- #parse_colon_time_format(str) ⇒ Object
- #time_reference(formula, captured) ⇒ Object
- #time_reference_match_now(form, format = nil) ⇒ Object
Instance Method Details
#base_regex ⇒ Object
26 27 28 |
# File 'lib/remi/testing/business_rules.rb', line 26 def base_regex @base_regex ||= /\*(.*)\*/ end |
#date_reference(formula, captured) ⇒ Object
62 63 64 65 |
# File 'lib/remi/testing/business_rules.rb', line 62 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
102 103 104 105 106 107 108 109 |
# File 'lib/remi/testing/business_rules.rb', line 102 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
84 85 86 87 88 89 90 91 |
# File 'lib/remi/testing/business_rules.rb', line 84 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
93 94 95 96 97 98 99 100 |
# File 'lib/remi/testing/business_rules.rb', line 93 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 37 |
# File 'lib/remi/testing/business_rules.rb', line 30 def formulas @formulas ||= RegexSieve.new({ /\*now(|:[^*]+)\*/i => [:time_reference, :match_now], /\*(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/testing/business_rules.rb', line 22 def is_formula?(arg) !base_regex.match(arg).nil? end |
#parse(form) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/remi/testing/business_rules.rb', line 39 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]) elsif form_opt[:value][0] == :time_reference time_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
67 68 69 |
# File 'lib/remi/testing/business_rules.rb', line 67 def parse_colon_date_format(str) str.blank? ? '%Y-%m-%d' : str.slice(1..-1).strip end |
#parse_colon_time_format(str) ⇒ Object
71 72 73 |
# File 'lib/remi/testing/business_rules.rb', line 71 def parse_colon_time_format(str) str.blank? ? '%Y-%m-%d %H:%M:%S' : str.slice(1..-1).strip end |
#time_reference(formula, captured) ⇒ Object
57 58 59 60 |
# File 'lib/remi/testing/business_rules.rb', line 57 def time_reference(formula, captured) parsed = self.send("time_reference_#{formula}", *captured) Time.current.send("#{parsed[:unit]}_#{parsed[:direction]}", parsed[:quantity]).strftime(parsed[:format]) end |
#time_reference_match_now(form, format = nil) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/remi/testing/business_rules.rb', line 75 def time_reference_match_now(form, format=nil) { quantity: 0, unit: 'days', direction: 'ago', format: parse_colon_time_format(format) } end |