Class: Syctask::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/syctask/evaluator.rb

Overview

Evaluator provides different evaluatons for comparing numbers, dates and strings. Also provides methods to check whether a value is part of a list

Constant Summary collapse

NUMBER_COMPARE_PATTERN =

Pattern to match operands <|=|> followed by a number or a single number

/^(<|=|>)(\d+)|^(\d+)/
CSV_PATTERN =

Pattern to match comma separated values

/\w+(?=,)|(?<!<|=|>)\w+$/
NUMBER_CSV_PATTERN =

Pattern to match comma separated numbers

/\d+(?=,)|\d+$/
DATE_COMPARE_PATTERN =

Pattern to match a date prepended by <|=|> or a single date

/^(<|=|>)(\d{4}-\d{2}-\d{2})|(\d{4}-\d{2}-\d{2})/
DATE_PATTERN =

Pattern to match a date in the form of yyyy-mm-dd

/^\d{4}-\d{2}-\d{2}/
NON_COMPARE_PATTERN =

Pattern that matches anything that is not prepended with <|=|>

/[^<=>]*/

Instance Method Summary collapse

Instance Method Details

#compare(value, operands) ⇒ Object

Compares two values regarding <|=|>. Returns true if the comparisson succeeds otherwise false. If eather value or operand is nil false is returned.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/syctask/evaluator.rb', line 49

def compare(value, operands)

  if operands[2]
    operands[0] = "=="
    operands[1] = operands[2]
  elsif operands[0] == "="
    operands[0] = "=="
  end

  expression = "#{value} #{operands[0]} #{operands[1]}"
  eval(expression) 
end

#compare_dates(value, pattern) ⇒ Object

Compares two dates regarding <|=|>. Returns true if the comparisson succeeds otherwise false. If eather value or pattern is nil false is returned.



35
36
37
38
39
40
41
42
43
44
# File 'lib/syctask/evaluator.rb', line 35

def compare_dates(value, pattern)
  return false if value.nil? or pattern.nil?
  result = pattern.match(DATE_COMPARE_PATTERN)
  return false unless result
  value = "'#{value}'"
  captures = result.captures.collect! do |c| 
    c and c.match(DATE_PATTERN) ? "'#{c}'" : c
  end
  compare(value, captures)
end

#compare_numbers(value, pattern) ⇒ Object

Compares two numbers regarding <|=|>. Returns true if the comparisson succeeds otherwise false. If eather value or pattern is nil false is returned. If value is empty false is returned.



24
25
26
27
28
29
30
# File 'lib/syctask/evaluator.rb', line 24

def compare_numbers(value, pattern)
  return false if value.nil? or pattern.nil?
  return false if value.class == String and value.empty?
  result = pattern.match(NUMBER_COMPARE_PATTERN)
  return false unless result
  compare(value, result.captures)
end

#includes?(value, pattern) ⇒ Boolean

Evaluates whether value is part of the provided csv pattern. Returns true if it evaluates to true otherwise false. If value or pattern is nil false is returned.

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/syctask/evaluator.rb', line 65

def includes?(value, pattern)
  return false if value.nil? or pattern.nil?
  captures = normalize_ranges(pattern).scan(CSV_PATTERN)
  !captures.find_index(value.to_s).nil?
end

#matches?(value, regex) ⇒ Boolean

Evaluates if value matches the provided regex. Returns true if a match is found. If value or regex is nil false is returned.

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/syctask/evaluator.rb', line 73

def matches?(value, regex)
  return false if value.nil? or regex.nil?
  !value.match(Regexp.new(regex, true)).nil?
end

#normalize_ranges(pattern) ⇒ Object

Checks if the pattern contains number ranges then the ranges are normalized e.g. 1-5 will become 1,2,3,4,5



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/syctask/evaluator.rb', line 80

def normalize_ranges(pattern)
  if pattern.include? "-"
    pattern.gsub(/\s/, "").split(',').map do |value|
      if value.include? '-' 
        if value =~ /^\d+-\d+$/
          a, b = value.split('-')
          Array(a..b)
        else
          value
        end
      else 
        value =~ /^\d+$/ ? value.to_i : value
      end
    end.uniq.join(',')
  else
    pattern
  end
end