|
| 1 | +require_relative 'filter/yaml' |
| 2 | + |
| 3 | +module OctocatalogDiff |
| 4 | + module CatalogDiff |
| 5 | + # Filtering of diffs, and parent class for inheritance. |
| 6 | + class Filter |
| 7 | + # Public: Apply multiple filters by repeatedly calling the `filter` method for each |
| 8 | + # filter in an array. This method returns nothing. |
| 9 | + # |
| 10 | + # @param result [Array] Difference array (mutated) |
| 11 | + # @param filter_names [Array] Filters to run |
| 12 | + # @param options [Hash] Options for each filter (hashed by name) |
| 13 | + def self.apply_filters(result, filter_names, options = {}) |
| 14 | + return unless filter_names.is_a?(Array) |
| 15 | + filter_names.each { |x| filter(result, x, options[x] || {}) } |
| 16 | + end |
| 17 | + |
| 18 | + # Public: Perform a filter on `result` using the specified filter class. |
| 19 | + # This mutates `result` by removing items that are ignored. This method |
| 20 | + # returns nothing. |
| 21 | + # |
| 22 | + # @param result [Array] Difference array (mutated) |
| 23 | + # @param filter_class_name [String] Filter class name (from `filter` subdirectory) |
| 24 | + # @param options [Hash] Additional options (optional) to pass to filtered? method |
| 25 | + def self.filter(result, filter_class_name, options = {}) |
| 26 | + filter_class_name = [name.to_s, filter_class_name].join('::') |
| 27 | + clazz = Kernel.const_get(filter_class_name) |
| 28 | + result.reject! { |item| clazz.filtered?(item, options) } |
| 29 | + end |
| 30 | + |
| 31 | + # Inherited: Construct a default `filtered?` method for the subclass via inheritance. |
| 32 | + # Each subclass must implement this method, so the default method errors. |
| 33 | + def self.filtered?(_item, _options = {}) |
| 34 | + raise "No `filtered?` method is implemented in #{name}" |
| 35 | + end |
| 36 | + end |
| 37 | + end |
| 38 | +end |
0 commit comments