module AlmaCourseLoader::FilterParser

Methods for parsing filter specification strings The primary method is parse_filter(str, extractors)

Constants

FILTER

The regular expression describing a filter string: [!][extractor]value

FILTER_MAP

Filter operators mapped to method names

The operators in filter expressions are converted to methods called on the filter value. Note that this means that the < and > operators are inverted ('field < filter-value' is equivalent to 'filter-value.>(field)') Operators not specified explicitly in FILTER_MAP are called directly as methods.

The hash value is either:

  • a Symbol representing the method name to call on the filter value;

  • an array [method-name-symbol, negated] containing the method name symbol and a Boolean indicating whether the method is implicitly negated, e.g. !~ is !match(); the default is no implicit negation.

Public Instance Methods

parse_filter(str = nil, extractors = nil) click to toggle source

Parses a filter string and returns the parsed filter string components @param str [String] the filter string: [extractor]value

where value is a JSON string specifying the value(s) to match,
      extractor is the optional name of an entry in the extractors hash,

@param extractors [Hash<Symbol, Proc|Method>] a hash of named field

extractors referenced by the filter string; this may include a nil
key which specifies the default extractor if not given in the filter
string

@return [Array] the parsed filter string components:

values, method, extractor, negate: [Object, Symbol, Proc, Boolean]

@raise [ArgumentError] if the filter string is invalid

# File lib/alma_course_loader/filter.rb, line 54
def parse_filter(str = nil, extractors = nil)
  # Parse the filter string
  raise ArgumentError, 'expected filter' if str.nil? || str.empty?
  match = FILTER.match(str)
  raise ArgumentError, "invalid filter: #{str}" if match.nil?
  # Get the filter value(s)
  value = Private.parse_value(match)
  # Get the method used to compare the filter value(s)
  method, method_negate = parse_method(match, value)
  # Get the effective negate flag
  negate = Private.negate_flag(match[:negate] == '!', method_negate)
  # Get the named extractor from the hash or block
  extractor = Private.parse_extractor(match, extractors)
  # Return the parsed filter string components
  [value, method, extractor, negate]
end
parse_method(match, values) click to toggle source

Validates and returns the method symbol and negation flag @param match [MatchData] the parsed filter string @param values [Object] the filter value(s) @return [Array] the method symbol and negate flag @raise [ArgumentError] if the specified method is not supported by the

filter values
# File lib/alma_course_loader/filter.rb, line 77
def parse_method(match, values)
  method_info = Private.method_symbol(match, values)
  method = method_info[0]
  unless values.respond_to?(method)
    raise ArgumentError, "invalid method: #{values.class.name}\##{method}"
  end
  method_info
end