Sha256: 7cbcea35b4c7224e3c1b24376dd55d62c2725ed38c129fb798f2316d3edbc0fc

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

# CSV Decision: CSV based Ruby decision tables.
# Created December 2017.
# @author Brett Vickers <brett@phillips-vickers.com>
# See LICENSE and README.md for details.
module CSVDecision
  # Methods to assign a matcher to data cells.
  # @api private
  class Matchers
    # Recognise numeric comparison expressions - e.g., +> 100+ or +!= 0+.
    class Numeric < Matcher
      # For example: +>= 100+ or +!= 0+.
      COMPARISON = /\A(?<comparator><=|>=|<|>|!=)\s*(?<value>\S.*)\z/
      private_constant :COMPARISON

      # Coerce the input value to a numeric representation before invoking the comparison.
      # If the coercion fails, it will produce a nil value which always fails to match.
      COMPARATORS = {
        '>'  => proc { |numeric_cell, value| Matchers.numeric(value)&.>  numeric_cell },
        '>=' => proc { |numeric_cell, value| Matchers.numeric(value)&.>= numeric_cell },
        '<'  => proc { |numeric_cell, value| Matchers.numeric(value)&.<  numeric_cell },
        '<=' => proc { |numeric_cell, value| Matchers.numeric(value)&.<= numeric_cell },
        '!=' => proc { |numeric_cell, value| Matchers.numeric(value)&.!= numeric_cell }
      }.freeze
      private_constant :COMPARATORS

      # (see Matcher#matches?)
      def self.matches?(cell)
        match = COMPARISON.match(cell)
        return false unless match

        numeric_cell = Matchers.to_numeric(match['value'])
        return false unless numeric_cell

        comparator = match['comparator']
        Matchers::Proc.new(type: :proc,
                           function: COMPARATORS[comparator].curry[numeric_cell].freeze)
      end

      # (see Matcher#matches?)
      def matches?(cell)
        Numeric.matches?(cell)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
csv_decision-0.4.0 lib/csv_decision/matchers/numeric.rb
csv_decision-0.3.2 lib/csv_decision/matchers/numeric.rb
csv_decision-0.3.1 lib/csv_decision/matchers/numeric.rb
csv_decision-0.3.0 lib/csv_decision/matchers/numeric.rb
csv_decision-0.2.0 lib/csv_decision/matchers/numeric.rb