module AlmaCourseLoader::FilterParser::Private
Private
helpers
Public Class Methods
Returns true if the value parameter is a collection @param value [Object] the value to test @return [Boolean] true if the value is a collection, false otherwise
# File lib/alma_course_loader/filter.rb, line 91 def self.collection?(value) value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(Set) end
Returns a Proc implementing the filter match @param values [Object] the filter values @param method [Symbol] the method to call against the filter values @param negate [Boolean] if true, negate the result of the method @return [Proc] the Proc implementing the filter
# File lib/alma_course_loader/filter.rb, line 100 def self.matcher(values, method = nil, negate = false) method, negate = method_symbol(method, values) if method.nil? proc do |value| result = values.send(method, value) negate ? !result : result end end
Returns the method symbol or appropriate default if not specified If no method is specified in the filter string, the default is :include? for Arrays, Hashes and Sets, :match for regular expressions and :== for everything else. @param match [MatchData] the parsed filter string @param values [Object] the filter value(s) @return [Array] the method symbol and negation flag (false|true)
# File lib/alma_course_loader/filter.rb, line 115 def self.method_symbol(match, values) method = match[:method] # Return the default if no method is specified return method_symbol_default(values) if method.nil? || method.empty? method = method.to_sym # Return the method as specified unless it's mapped return method, false unless FILTER_MAP.key?(method) # Otherwise return the mapped filter method, negate = FILTER_MAP[method] [method, negate.nil? ? false : negate] end
Returns a default method symbol based on the filter value type @param values [Object] the filter value(s) @return [Array] the default method symbol and negation flag (false|true)
# File lib/alma_course_loader/filter.rb, line 130 def self.method_symbol_default(values) return :include?, false if collection?(values) return :match, false if values.is_a?(Regexp) [:==, false] end
Returns the actual filter negate flag based on the requested filter negate flag and the compare method's negate flag @param filter_negate [Boolean] the requested filter negate flag @param method_negate [Boolean] the compare method's negate flag @return [Boolean] the effective negate flag
# File lib/alma_course_loader/filter.rb, line 141 def self.negate_flag(filter_negate = false, method_negate = false) # The effective negate flag is true only if exactly one of the arguments # is true (filter_flag xor method_negate) since double negation cancels filter_negate != method_negate end
Returns the extractor Proc specified by a parsed filter string @param match [MatchData] the parsed filter string @param extractors [Hash<Symbol, Proc|Method>] the extractors @raise [ArgumentError] if a specified extractor is invalid or there is
no default extractor in extractors when required
# File lib/alma_course_loader/filter.rb, line 152 def self.parse_extractor(match, extractors = nil) raise ArgumentError, 'extractors required' \ if extractors.nil? || extractors.empty? e = match[:extractor] extractor = extractors[e ? e.to_sym : nil] return extractor unless extractor.nil? raise ArgumentError, "invalid extractor: #{e}" if e raise ArgumentError, 'no default extractor' end
Returns a parsed regular expression @param regexp [String] the regular expression from the value string @return [Regexp] the parsed regular expression @raise [ArgumentError] if the regular expression cannot be parsed
# File lib/alma_course_loader/filter.rb, line 166 def self.parse_regexp(regexp) # Remove enclosing / if present first = regexp.start_with?('/') ? 1 : 0 last = regexp.end_with?('/') ? -2 : -1 Regexp.new(regexp[first..last]) rescue RegexpError raise ArgumentError, "invalid regular expression: #{regexp}" end
Returns the parsed value string @param match [MatchData] the parsed filter string @return [Object] the filter value(s) @raise [ArgumentError] if the value string cannot be parsed
# File lib/alma_course_loader/filter.rb, line 179 def self.parse_value(match) value = match[:value] return nil if value.nil? # Parse strings starting with / as regular expressions return parse_regexp(value) if value.start_with?('/') # Parse all other strings as JSON JSON.parse(value) rescue JSON::ParserError raise ArgumentError, "invalid value: #{value}" end