Class: OctocatalogDiff::CatalogDiff::Filter
- Inherits:
-
Object
- Object
- OctocatalogDiff::CatalogDiff::Filter
- 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.
Direct Known Subclasses
AbsentFile, CompilationDir, EquivalentArrayNoDatatypes, JSON, SingleItemArray, YAML
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
-
#logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
-
.apply_filters(result, filter_names, options = {}) ⇒ Object
Public: Apply multiple filters by repeatedly calling the
filtermethod for each filter in an array. -
.assert_that_filter_exists(filter_name) ⇒ Object
Public: Assert that a filter exists, and raise an error if it does not.
-
.filter(result, filter_class_name, options = {}) ⇒ Object
Public: Perform a filter on
resultusing the specified filter class. -
.filter?(filter_name) ⇒ Boolean
Public: Determine whether a particular filter exists.
Instance Method Summary collapse
-
#filtered?(_item, _options = {}) ⇒ Boolean
Inherited: Construct a default
filtered?method for the subclass via inheritance. -
#initialize(_diff_array = [], logger = Logger.new(StringIO.new)) ⇒ Filter
constructor
Inherited: Constructor.
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
#logger ⇒ Object
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.
41 42 43 44 |
# File 'lib/octocatalog-diff/catalog-diff/filter.rb', line 41 def self.apply_filters(result, filter_names, = {}) return unless filter_names.is_a?(Array) filter_names.each { |x| filter(result, x, || {}) } end |
.assert_that_filter_exists(filter_name) ⇒ Object
Public: Assert that a filter exists, and raise an error if it does not.
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.
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, = {}) 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, [:logger]) result.reject! { |item| obj.filtered?(result_hash[item], ) } end |
.filter?(filter_name) ⇒ Boolean
Public: Determine whether a particular filter exists. This can be used to validate a user-submitted filter.
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.
75 76 77 |
# File 'lib/octocatalog-diff/catalog-diff/filter.rb', line 75 def filtered?(_item, = {}) raise "No `filtered?` method is implemented in #{self.class}" end |