Class: CSVDecision2::Matchers Private

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_decision2.rb,
lib/csv_decision2/matchers.rb,
lib/csv_decision2/matchers/guard.rb,
lib/csv_decision2/matchers/range.rb,
lib/csv_decision2/matchers/symbol.rb,
lib/csv_decision2/matchers/numeric.rb,
lib/csv_decision2/matchers/pattern.rb,
lib/csv_decision2/matchers/constant.rb,
lib/csv_decision2/matchers/function.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Methods to assign a matcher to data cells

Defined Under Namespace

Classes: Constant, Function, Guard, Matcher, Numeric, Pattern, Proc, Range, Symbol

Constant Summary collapse

NEGATE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Negation sign prefixed to ranges and functions.

'!'
INEQUALITY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Symbols used for inequality

'!=|!'
INEQUALITY_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Match Regexp for inequality

regexp(INEQUALITY)
EQUALS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Equality, cell constants and functions specified by prefixing the value with one of these 3 symbols.

'==|:=|='
EQUALS_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Match Regexp for equality

regexp(EQUALS)
METHOD_NAME_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Method names are stricter than CSV column names.

/\A[_a-z][_a-z0-9]*[?!=]?\z/
NUMERIC =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regular expression used to recognise a numeric string with or without a decimal point.

'[-+]?\d*(?<decimal>\.?)\d*'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Matchers

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Matchers.

Parameters:

  • options (Hash{Symbol=>Object})

    Options hash controlling how the table is parsed and interpreted.



167
168
169
170
171
# File 'lib/csv_decision2/matchers.rb', line 167

def initialize(options)
  matchers = options[:matchers].collect { |klass| klass.new(options) }
  @ins = matchers.select(&:ins?)
  @outs = matchers.select(&:outs?)
end

Instance Attribute Details

#insArray<Matchers::Matcher> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Matchers for the input columns.

Returns:



161
162
163
# File 'lib/csv_decision2/matchers.rb', line 161

def ins
  @ins
end

#outsArray<Matchers::Matcher> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Matchers for the output columns.

Returns:



164
165
166
# File 'lib/csv_decision2/matchers.rb', line 164

def outs
  @outs
end

Class Method Details

.compare?(lhs:, compare:, rhs:) ⇒ nil, Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compare one object with another if they both respond to the compare method.

Parameters:

  • lhs (Object)
  • compare (Object)
  • rhs (Object)

Returns:

  • (nil, Boolean)


133
134
135
136
137
138
139
# File 'lib/csv_decision2/matchers.rb', line 133

def self.compare?(lhs:, compare:, rhs:)
  # Is the rhs the same class or a superclass of lhs, and does rhs respond to the
  # compare method?
  return lhs.send(compare, rhs) if lhs.is_a?(rhs.class) && rhs.respond_to?(compare)

  nil
end

.normalize_operator(operator) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Normalize the operators which are a variation on equals/assignment.

Parameters:

  • operator (String)

Returns:

  • (String)


95
96
97
# File 'lib/csv_decision2/matchers.rb', line 95

def self.normalize_operator(operator)
  EQUALS_RE.match?(operator) ? '==' : operator
end

.numeric(value) ⇒ nil, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validate a numeric value and convert it to an Integer or BigDecimal if a valid numeric string.

Parameters:

  • value (nil, String, Integer, BigDecimal)

Returns:

  • (nil, Integer, BigDecimal)


109
110
111
112
113
114
# File 'lib/csv_decision2/matchers.rb', line 109

def self.numeric(value)
  return value if value.is_a?(Integer) || value.is_a?(BigDecimal)
  return unless value.is_a?(String)

  to_numeric(value)
end

.parse(columns:, matchers:, row:) ⇒ Array<(Array, ScanRow)>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse the supplied input columns for the row supplied using an array of matchers.

Parameters:

  • columns (Hash{Integer=>Columns::Entry})

    Input columns hash.

  • matchers (Array<Matchers::Matcher>)
  • row (Array<String>)

    Data row being parsed.

Returns:

  • (Array<(Array, ScanRow)>)

    Used to scan a table row against an input hash for matches.



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/csv_decision2/matchers.rb', line 147

def self.parse(columns:, matchers:, row:)
  # Build an array of column indexes requiring simple constant matches,
  # and a second array of columns requiring special matchers.
  scan_row = ScanRow.new

  # Scan the columns in the data row, and build an object to scan this row against
  # an input hash.
  # Convert values in the data row if not just a simple constant.
  row = scan_row.scan_columns(columns: columns, matchers: matchers, row: row)

  [row, scan_row]
end

.regexp(value) ⇒ Regexp

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

All regular expressions used for matching are anchored inside their own non-capturing group.

Parameters:

  • value (String)

    String used to form an anchored regular expression.

Returns:

  • (Regexp)

    Anchored, frozen regular expression.



71
72
73
# File 'lib/csv_decision2/matchers.rb', line 71

def self.regexp(value)
  Regexp.new("\\A(?:#{value})\\z").freeze
end

.to_numeric(value) ⇒ nil, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert a numeric string into an Integer or BigDecimal, otherwise return nil.

Parameters:

  • value (String)

Returns:

  • (nil, Integer, BigDecimal)


120
121
122
123
124
125
# File 'lib/csv_decision2/matchers.rb', line 120

def self.to_numeric(value)
  return unless (match = NUMERIC_RE.match(value))

  return value.to_i if match['decimal'] == ''
  BigDecimal(value.chomp('.'))
end

Instance Method Details

#parse_ins(columns:, row:) ⇒ Array<(Array, ScanRow)>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse the row’s input columns using the input matchers.

Parameters:

  • columns (Hash{Integer=>Columns::Entry})

    Input columns hash.

  • row (Array<String>)

    Data row being parsed.

Returns:

  • (Array<(Array, ScanRow)>)

    Used to scan a table row against an input hash for matches.



178
179
180
# File 'lib/csv_decision2/matchers.rb', line 178

def parse_ins(columns:, row:)
  Matchers.parse(columns: columns, matchers: @ins, row: row)
end

#parse_outs(columns:, row:) ⇒ Array<(Array, ScanRow)>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse the row’s output columns using the output matchers.

Parameters:

  • columns (Hash{Integer=>Columns::Entry})

    Input columns hash.

  • row (Array<String>)

    Data row being parsed.

Returns:

  • (Array<(Array, ScanRow)>)

    Used to scan a table row against an input hash for matches.



187
188
189
# File 'lib/csv_decision2/matchers.rb', line 187

def parse_outs(columns:, row:)
  Matchers.parse(columns: columns, matchers: @outs, row: row)
end