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

default_action[RW]

Public Class Methods

new() click to toggle source

ArtifactFilter constructor

# File lib/artifactory/cleaner/artifact_filter.rb, line 18
def initialize()
  @rules = []
  @sorted = false
  @default_action = :include
end

Public Instance Methods

<<(rule)
Alias for: push
[](*args) click to toggle source

Access a rule by index

# File lib/artifactory/cleaner/artifact_filter.rb, line 30
def [](*args)
  sort_if_needed
  @rules[*args]
end
[]=(key, rule) click to toggle source

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
action_for(artifact) click to toggle source

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
bsearch(*args, &block) click to toggle source

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
each(&block) click to toggle source

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(artifacts, action = :include) click to toggle source

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
first() click to toggle source

Get the first (numerically first priority) rule

# File lib/artifactory/cleaner/artifact_filter.rb, line 59
def first
  sort_if_needed
  @rules.first
end
last() click to toggle source

Get the last (numerically last priority) rule

# File lib/artifactory/cleaner/artifact_filter.rb, line 66
def last
  sort_if_needed
  @rules.last
end
push(rule) click to toggle source

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
Also aliased as: <<
slice(*args, &block) click to toggle source

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
sort!() click to toggle source

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
unshift(rule) click to toggle source

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

Private Instance Methods

sort_if_needed() click to toggle source

Sort the array but avoid needless sorting

# File lib/artifactory/cleaner/artifact_filter.rb, line 140
def sort_if_needed
  @rules.sort! unless @sorted
  @sorted = true
end