Class: OctocatalogDiff::CatalogDiff::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/catalog-diff/filter.rb,
lib/octocatalog-diff/catalog-diff/filter/json.rb,
lib/octocatalog-diff/catalog-diff/filter/yaml.rb,
lib/octocatalog-diff/catalog-diff/filter/absent_file.rb,
lib/octocatalog-diff/catalog-diff/filter/compilation_dir.rb,
lib/octocatalog-diff/catalog-diff/filter/single_item_array.rb,
lib/octocatalog-diff/catalog-diff/filter/equivalent_array_no_datatypes.rb

Overview

Filtering of diffs, and parent class for inheritance.

Defined Under Namespace

Classes: AbsentFile, CompilationDir, EquivalentArrayNoDatatypes, JSON, SingleItemArray, YAML

Constant Summary collapse

AVAILABLE_FILTERS =

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

%w(AbsentFile CompilationDir EquivalentArrayNoDatatypes JSON SingleItemArray YAML).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_diff_array = [], logger = Logger.new(StringIO.new)) ⇒ Filter

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.



69
70
71
# File 'lib/octocatalog-diff/catalog-diff/filter.rb', line 69

def initialize(_diff_array = [], logger = Logger.new(StringIO.new))
  @logger = logger
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



15
16
17
# File 'lib/octocatalog-diff/catalog-diff/filter.rb', line 15

def logger
  @logger
end

Class Method Details

.apply_filters(result, filter_names, options = {}) ⇒ Object

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

Parameters:

  • result (Array)

    Difference array (mutated)

  • filter_names (Array)

    Filters to run

  • options (Hash) (defaults to: {})

    Options for each filter



41
42
43
44
# File 'lib/octocatalog-diff/catalog-diff/filter.rb', line 41

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) ⇒ Object

Public: Assert that a filter exists, and raise an error if it does not.

Parameters:

  • filter_name (String)

    Proposed filter name

Raises:

  • (ArgumentError)


30
31
32
33
# File 'lib/octocatalog-diff/catalog-diff/filter.rb', line 30

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 = {}) ⇒ Object

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

Parameters:

  • result (Array)

    Difference array (mutated)

  • filter_class_name (String)

    Filter class name (from ‘filter` subdirectory)

  • options (Hash) (defaults to: {})

    Additional options (optional) to pass to filtered? method



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/octocatalog-diff/catalog-diff/filter.rb', line 53

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) ⇒ Boolean

Public: Determine whether a particular filter exists. This can be used to validate a user-submitted filter.

Parameters:

  • filter_name (String)

    Proposed filter name

Returns:

  • (Boolean)

    True if filter is valid; false otherwise



24
25
26
# File 'lib/octocatalog-diff/catalog-diff/filter.rb', line 24

def self.filter?(filter_name)
  AVAILABLE_FILTERS.include?(filter_name)
end

Instance Method Details

#filtered?(_item, _options = {}) ⇒ Boolean

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

Returns:

  • (Boolean)


75
76
77
# File 'lib/octocatalog-diff/catalog-diff/filter.rb', line 75

def filtered?(_item, _options = {})
  raise "No `filtered?` method is implemented in #{self.class}"
end