class Artifactory::Cleaner::ArtifactFilter
Filter a list of artifacts based on a series of include/exclude rules
Artifactory::Cleaner::ArtifactFilter is used to filter a list of artifacts based on rules. It is both a whitelist and a blacklist: it maintains a list of rules in sorted priority order, and the first rule which matches a given artifact determines the action for that artifact (include or exclude)
Rules are stored in ascending priority order with lower numbers being greater priority. (Think “Priority 1” or process queue scheduling via `nice` value under Linux.
Attributes
Public Class Methods
ArtifactFilter constructor
# File lib/artifactory/cleaner/artifact_filter.rb, line 18 def initialize() @rules = [] @sorted = false @default_action = :include end
Public Instance Methods
Access a rule by index
# File lib/artifactory/cleaner/artifact_filter.rb, line 30 def [](*args) sort_if_needed @rules[*args] end
Update a given rule
# File lib/artifactory/cleaner/artifact_filter.rb, line 37 def []=(key, rule) raise TypeError, "expected Artifactory::Cleaner::ArtifactFilterRule, got #{rule.class.name}" unless rule.is_a? Artifactory::Cleaner::ArtifactFilterRule @rules[key] = rule @sorted = false end
Filter a given Artifactory::Resource::Artifact and return the action which should be taken
Returns a symbol from the rule which matches this artifact, or the default action (:include) if no rules matched
# File lib/artifactory/cleaner/artifact_filter.rb, line 114 def action_for(artifact) sort_if_needed @rules.each do |rule| action = rule.action_for artifact return action if action end @default_action end
Search for a rule
# File lib/artifactory/cleaner/artifact_filter.rb, line 52 def bsearch(*args, &block) sort_if_needed @rules.bsearch(*args, &block) end
Iterate over all rules (See Enumerable#each)
# File lib/artifactory/cleaner/artifact_filter.rb, line 73 def each(&block) sort_if_needed @rules.each(&block) end
Filter a collection of Artifactory::Resource::Artifact instances, returning the ones for which the action matches
Takes an Enumerable filled with Artifactory::Resource::Artifact instances and returns a filtered enumerable for which all artifacts matched the desired action after applying the filter rules to each item. `action` defaults to :include but can be changed if desired
Unlike Array#filter +this method does not take a block+
# File lib/artifactory/cleaner/artifact_filter.rb, line 132 def filter(artifacts, action = :include) artifacts.filter {|artifact| action_for(artifact) == action} end
Get the first (numerically first priority) rule
# File lib/artifactory/cleaner/artifact_filter.rb, line 59 def first sort_if_needed @rules.first end
Get the last (numerically last priority) rule
# File lib/artifactory/cleaner/artifact_filter.rb, line 66 def last sort_if_needed @rules.last end
Add rules to this filter
Like Array#push this method adds a rule to the end of the array, however the array will be sorted in priority order before usage so addition at the end or the beginning is somewhat meaningless
# File lib/artifactory/cleaner/artifact_filter.rb, line 83 def push(rule) raise TypeError, "expected Artifactory::Cleaner::ArtifactFilterRule, got #{rule.class.name}" unless rule.is_a? Artifactory::Cleaner::ArtifactFilterRule @rules.push rule @sorted = false self end
Slice the filter rules, see Array#slice
# File lib/artifactory/cleaner/artifact_filter.rb, line 45 def slice(*args, &block) sort_if_needed @rules.slice(*args, &block) end
Ensure the filterset is sorted properly. Should not need to be called manually
# File lib/artifactory/cleaner/artifact_filter.rb, line 105 def sort! sort_if_needed self end
Add a rule to this filter
Like Array#unshift this method adds a rule to the beginning of the array, however the array will be sorted in priority order before usage so addition at the end or the beginning is somewhat meaningless
# File lib/artifactory/cleaner/artifact_filter.rb, line 96 def unshift(rule) raise TypeError, "expected Artifactory::Cleaner::ArtifactFilterRule, got #{rule.class.name}" unless rule.is_a? Artifactory::Cleaner::ArtifactFilterRule @rules.unshift rule @sorted = false self end