Sha256: c7cf84c45111316b7191a8dfd4e40cb1c6ad51aa3c56f8e0ef78fd8706d981ea
Contents?: true
Size: 1.86 KB
Versions: 3
Compression:
Stored size: 1.86 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 # @param (see Matchers::Matcher#matches?) # @return (see Matchers::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.with(type: :proc, function: COMPARATORS[comparator].curry[numeric_cell].freeze) end # @param (see Matchers::Matcher#matches?) # @return (see Matchers::Matcher#matches?) def matches?(cell) Numeric.matches?(cell) end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
csv_decision-0.0.9 | lib/csv_decision/matchers/numeric.rb |
csv_decision-0.0.8 | lib/csv_decision/matchers/numeric.rb |
csv_decision-0.0.7 | lib/csv_decision/matchers/numeric.rb |