Class: Artifactory::Cleaner::ArtifactFilter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/artifactory/cleaner/artifact_filter.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArtifactFilter

ArtifactFilter constructor



18
19
20
21
22
# File 'lib/artifactory/cleaner/artifact_filter.rb', line 18

def initialize()
  @rules = []
  @sorted = false
  @default_action = :include
end

Instance Attribute Details

#default_actionObject

Returns the value of attribute default_action.



24
25
26
# File 'lib/artifactory/cleaner/artifact_filter.rb', line 24

def default_action
  @default_action
end

Instance Method Details

#[](*args) ⇒ Object

Access a rule by index



30
31
32
33
# File 'lib/artifactory/cleaner/artifact_filter.rb', line 30

def [](*args)
  sort_if_needed
  @rules[*args]
end

#[]=(key, rule) ⇒ Object

Update a given rule

Raises:

  • (TypeError)


37
38
39
40
41
# 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) ⇒ Object

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



114
115
116
117
118
119
120
121
# 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) ⇒ Object

Search for a rule



52
53
54
55
# File 'lib/artifactory/cleaner/artifact_filter.rb', line 52

def bsearch(*args, &block)
  sort_if_needed
  @rules.bsearch(*args, &block)
end

#each(&block) ⇒ Object

Iterate over all rules (See Enumerable#each)



73
74
75
76
# File 'lib/artifactory/cleaner/artifact_filter.rb', line 73

def each(&block)
  sort_if_needed
  @rules.each(&block)
end

#filter(artifacts, action = :include) ⇒ Object

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



132
133
134
# File 'lib/artifactory/cleaner/artifact_filter.rb', line 132

def filter(artifacts, action = :include)
  artifacts.filter {|artifact| action_for(artifact) == action}
end

#firstObject

Get the first (numerically first priority) rule



59
60
61
62
# File 'lib/artifactory/cleaner/artifact_filter.rb', line 59

def first
  sort_if_needed
  @rules.first
end

#lastObject

Get the last (numerically last priority) rule



66
67
68
69
# File 'lib/artifactory/cleaner/artifact_filter.rb', line 66

def last
  sort_if_needed
  @rules.last
end

#push(rule) ⇒ Object Also known as: <<

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

Raises:

  • (TypeError)


83
84
85
86
87
88
# 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(*args, &block) ⇒ Object

Slice the filter rules, see Array#slice



45
46
47
48
# File 'lib/artifactory/cleaner/artifact_filter.rb', line 45

def slice(*args, &block)
  sort_if_needed
  @rules.slice(*args, &block)
end

#sort!Object

Ensure the filterset is sorted properly. Should not need to be called manually



105
106
107
108
# File 'lib/artifactory/cleaner/artifact_filter.rb', line 105

def sort!
  sort_if_needed
  self
end

#unshift(rule) ⇒ Object

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

Raises:

  • (TypeError)


96
97
98
99
100
101
# 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