class OctocatalogDiff::CatalogDiff::Filter

Filtering of diffs, and parent class for inheritance.

Constants

AVAILABLE_FILTERS

List the available filters here (by class name) for use in the validator method.

Attributes

logger[RW]

Public Class Methods

apply_filters(result, filter_names, options = {}) click to toggle source

Public: Apply multiple filters by repeatedly calling the `filter` method for each filter in an array. This method returns nothing.

@param result [Array] Difference array (mutated) @param filter_names [Array] Filters to run @param options [Hash] Options for each filter

# File lib/octocatalog-diff/catalog-diff/filter.rb, line 40
def self.apply_filters(result, filter_names, options = {})
  return unless filter_names.is_a?(Array)
  filter_names.each { |x| filter(result, x, options || {}) }
end
assert_that_filter_exists(filter_name) click to toggle source

Public: Assert that a filter exists, and raise an error if it does not. @param filter_name [String] Proposed filter name

# File lib/octocatalog-diff/catalog-diff/filter.rb, line 29
def self.assert_that_filter_exists(filter_name)
  return if filter?(filter_name)
  raise ArgumentError, "The filter #{filter_name} is not valid"
end
filter(result, filter_class_name, options = {}) click to toggle source

Public: Perform a filter on `result` using the specified filter class. This mutates `result` by removing items that are ignored. This method returns nothing.

@param result [Array] Difference array (mutated) @param filter_class_name [String] Filter class name (from `filter` subdirectory) @param options [Hash] Additional options (optional) to pass to filtered? method

# File lib/octocatalog-diff/catalog-diff/filter.rb, line 52
def self.filter(result, filter_class_name, options = {})
  assert_that_filter_exists(filter_class_name)
  filter_class_name = [name.to_s, filter_class_name].join('::')

  # Need to convert each of the results array to the OctocatalogDiff::API::V1::Diff object, if
  # it isn't already. The comparison is done on that array which is then applied back to the
  # original array.
  result_hash = {}
  result.each { |x| result_hash[x] = OctocatalogDiff::API::V1::Diff.factory(x) }
  obj = Kernel.const_get(filter_class_name).new(result_hash.values, options[:logger])
  result.reject! { |item| obj.filtered?(result_hash[item], options) }
end
filter?(filter_name) click to toggle source

Public: Determine whether a particular filter exists. This can be used to validate a user-submitted filter. @param filter_name [String] Proposed filter name @return [Boolean] True if filter is valid; false otherwise

# File lib/octocatalog-diff/catalog-diff/filter.rb, line 23
def self.filter?(filter_name)
  AVAILABLE_FILTERS.include?(filter_name)
end
new(_diff_array = [], logger = Logger.new(StringIO.new)) click to toggle source

Inherited: Constructor. Some filters require working on the entire data set and will override this method to perform some pre-processing for efficiency. This also sets up the logger object.

# File lib/octocatalog-diff/catalog-diff/filter.rb, line 68
def initialize(_diff_array = [], logger = Logger.new(StringIO.new))
  @logger = logger
end

Public Instance Methods

filtered?(_item, _options = {}) click to toggle source

Inherited: Construct a default `filtered?` method for the subclass via inheritance. Each subclass must implement this method, so the default method errors.

# File lib/octocatalog-diff/catalog-diff/filter.rb, line 74
def filtered?(_item, _options = {})
  raise "No `filtered?` method is implemented in #{self.class}"
end