class AlmaCourseLoader::Filter

Implements a course filter

Attributes

extractor[RW]

@!attribute [rw] extractor

@return [Proc, Method] a block accepting a year, course and cohort and
  returning the value used in filter matching
method[RW]

@!attribute [rw] method

@return [Symbol] the method to call against the values
negate[RW]

@!attribute [rw] negate

@return [Boolean] if true, the filter result is negated
values[RW]

@!attribute [rw] values

@return [Object] the value or collection to match against

Public Class Methods

new(values = nil, method = nil, extractor = nil, negate = false, &block) click to toggle source

Initialises a new Filter instance @param values [Object] the value or collection to match against @param method [Symbol] the method symbol applied to the filter values @param extractor [Proc, Method] a block accepting a year, course and

cohort and returning the value used in filter matching.
This may be also passed as a code block.

@param negate [Boolean] if true, negate the result of the filter @raise [ArgumentError] @return [void]

# File lib/alma_course_loader/filter.rb, line 235
def initialize(values = nil, method = nil, extractor = nil, negate = false,
               &block)
  self.method = method
  self.negate = negate
  self.extractor = extractor || block
  self.values = values
  @matcher = matcher_proc(method)
end
parse(str = nil, extractors = nil) click to toggle source

Parses a filter string and returns a Filter instance @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

@raise [ArgumentError] if the filter string is invalid

# File lib/alma_course_loader/filter.rb, line 222
def self.parse(str = nil, extractors = nil)
  new(*parse_filter(str, extractors))
end

Public Instance Methods

call(year = nil, course = nil, cohort = nil) click to toggle source

Applies the filter to a course/cohort @param year [Object] the course year @param course [Object] the course @param cohort [Object] the course cohort @return [Boolean] true if the course passes the filter, false otherwise

# File lib/alma_course_loader/filter.rb, line 249
def call(year = nil, course = nil, cohort = nil)
  # Get the value for matching
  value = extractor.call(year, course, cohort)
  # Test against the filter values (true => match, false => no match)
  result = @matcher.call(value)
  # Return the result with appropriate negation
  negate ? !result : result
end

Private Instance Methods

matcher_proc(method) click to toggle source

Returns a Proc instance which calls the specified method on the filter value object @param method [Method, Proc, Symbol] the method (Method/Proc arguments are

returned as-is)

@return [Proc] the Proc instance

# File lib/alma_course_loader/filter.rb, line 265
def matcher_proc(method)
  # Return Method/Proc arguments unchanged
  return method if method.is_a?(Method) || method.is_a?(Proc)
  # Otherwise return a Proc which invokes method on the filter values
  proc { |value| values.send(method, value) }
end