Class: Machinery::Filter
Constant Summary collapse
- OPERATOR_EQUALS =
"=".freeze
- OPERATOR_EQUALS_NOT =
"!=".freeze
Instance Attribute Summary collapse
-
#element_filters ⇒ Object
Returns the value of attribute element_filters.
Class Method Summary collapse
Instance Method Summary collapse
- #add_element_filter_from_definition(filter_definition) ⇒ Object
- #add_element_filters(element_filters) ⇒ Object
- #apply!(system_description) ⇒ Object
- #element_filter_for(path) ⇒ Object
- #element_filters_for_scope(scope) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(definitions = []) ⇒ Filter
constructor
A new instance of Filter.
- #matches?(path, value) ⇒ Boolean
- #set_element_filters_for_scope(scope, element_filters) ⇒ Object
- #to_array ⇒ Object
Constructor Details
#initialize(definitions = []) ⇒ Filter
Returns a new instance of Filter.
91 92 93 |
# File 'lib/filter.rb', line 91 def initialize(definitions = []) @element_filters = Filter.parse_filter_definitions(definitions) end |
Instance Attribute Details
#element_filters ⇒ Object
Returns the value of attribute element_filters.
48 49 50 |
# File 'lib/filter.rb', line 48 def element_filters @element_filters end |
Class Method Details
.from_default_definition(command) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/filter.rb', line 75 def self.from_default_definition(command) filter = Filter.new default_filters_file = File.join(Machinery::ROOT, "filters", "default_filters.json") if File.exist?(default_filters_file) default_filters = JSON.parse(File.read(default_filters_file)) if default_filters[command] default_filters[command].each do |definition| filter.add_element_filter_from_definition(definition) end end end filter end |
.parse_filter_definitions(filter_definitions) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/filter.rb', line 53 def self.parse_filter_definitions(filter_definitions) element_filters = {} Array(filter_definitions).each do |definition| path, operator, matcher_definition = definition.scan(/([a-zA-Z_\/]+)(.*=)(.*)/)[0] raise Machinery::Errors::InvalidFilter.new( "Invalid filter: '#{definition}'" ) unless operator element_filters[path] ||= ElementFilter.new(path) if matcher_definition.index(",") matchers = matcher_definition.split(/(?<!\\),/) matchers.map! { |matcher| matcher.gsub("\\,", ",") } # Unescape escaped commas element_filters[path].add_matchers(operator, [matchers]) else element_filters[path].add_matchers(operator, matcher_definition) end end element_filters end |
Instance Method Details
#add_element_filter_from_definition(filter_definition) ⇒ Object
95 96 97 |
# File 'lib/filter.rb', line 95 def add_element_filter_from_definition(filter_definition) add_element_filters(Filter.parse_filter_definitions(filter_definition).values) end |
#add_element_filters(element_filters) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/filter.rb', line 99 def add_element_filters(element_filters) Array(element_filters).each do |element_filter| path = element_filter.path @element_filters[path] ||= ElementFilter.new(path) element_filter.matchers.each do |operator, matchers| @element_filters[path].add_matchers(operator, matchers) end end end |
#apply!(system_description) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/filter.rb', line 145 def apply!(system_description) element_filters.each do |path, element_filter| steps = path.split("/").reject(&:empty?) target = steps.pop pointer = system_description container = nil steps.each do |step| break unless pointer begin pointer = pointer[step] rescue TypeError raise Machinery::Errors::WrongFilterPath.new( "Error: Check if the path: '#{path}' is correct." ) end container ||= pointer if pointer.is_a?(Machinery::Array) end next unless pointer begin pointer.delete_if do |element| element_filter.matches?(element[target]) end rescue Machinery::Errors::ElementFilterTypeMismatch => e Machinery::Ui.warn("Warning: Filter '#{e.failed_matcher}' tries to match an array, " \ "but the according element is not an array.") end end end |
#element_filter_for(path) ⇒ Object
110 111 112 |
# File 'lib/filter.rb', line 110 def element_filter_for(path) element_filters[path] end |
#element_filters_for_scope(scope) ⇒ Object
114 115 116 117 118 |
# File 'lib/filter.rb', line 114 def element_filters_for_scope(scope) @element_filters.values.select do |element_filter| element_filter.filters_scope?(scope) end end |
#empty? ⇒ Boolean
177 178 179 |
# File 'lib/filter.rb', line 177 def empty? element_filters.empty? end |
#matches?(path, value) ⇒ Boolean
138 139 140 141 142 143 |
# File 'lib/filter.rb', line 138 def matches?(path, value) filter = element_filter_for(path) return false unless filter filter.matches?(value) end |
#set_element_filters_for_scope(scope, element_filters) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/filter.rb', line 120 def set_element_filters_for_scope(scope, element_filters) @element_filters.reject! do |_path, element_filter| element_filter.filters_scope?(scope) end add_element_filters(element_filters) end |