Class: Pvectl::Selectors::Base
- Inherits:
-
Object
- Object
- Pvectl::Selectors::Base
- Defined in:
- lib/pvectl/selectors/base.rb
Overview
Base class for parsing and applying selectors.
Selectors use kubectl-style syntax to filter resources:
-l key=value # equality
-l key!=value # inequality
-l key=~pattern # wildcard pattern
-l key in (a,b,c) # one of many
Instance Attribute Summary collapse
-
#conditions ⇒ Array<Hash>
readonly
Parsed conditions.
Class Method Summary collapse
-
.parse(selector_string) ⇒ Base
Parses selector string into Base instance.
-
.parse_all(selector_strings) ⇒ Base
Parses multiple selector strings (from multiple -l flags).
Instance Method Summary collapse
-
#apply(collection) ⇒ Array
Applies selector to collection (subclass responsibility).
-
#empty? ⇒ Boolean
Checks if selector is empty (no conditions).
-
#initialize(conditions = []) ⇒ Base
constructor
Creates selector with parsed conditions.
-
#matches?(item) ⇒ Boolean
Checks if a single item matches all conditions.
Constructor Details
#initialize(conditions = []) ⇒ Base
Creates selector with parsed conditions.
48 49 50 |
# File 'lib/pvectl/selectors/base.rb', line 48 def initialize(conditions = []) @conditions = conditions end |
Instance Attribute Details
#conditions ⇒ Array<Hash> (readonly)
Parsed conditions
26 27 28 |
# File 'lib/pvectl/selectors/base.rb', line 26 def conditions @conditions end |
Class Method Details
.parse(selector_string) ⇒ Base
Parses selector string into Base instance.
32 33 34 |
# File 'lib/pvectl/selectors/base.rb', line 32 def self.parse(selector_string) new(parse_conditions(selector_string)) end |
.parse_all(selector_strings) ⇒ Base
Parses multiple selector strings (from multiple -l flags).
40 41 42 43 |
# File 'lib/pvectl/selectors/base.rb', line 40 def self.parse_all(selector_strings) conditions = selector_strings.flat_map { |s| parse_conditions(s) } new(conditions) end |
Instance Method Details
#apply(collection) ⇒ Array
Applies selector to collection (subclass responsibility).
64 65 66 |
# File 'lib/pvectl/selectors/base.rb', line 64 def apply(collection) raise NotImplementedError, "#{self.class}#apply must be implemented" end |
#empty? ⇒ Boolean
Checks if selector is empty (no conditions).
55 56 57 |
# File 'lib/pvectl/selectors/base.rb', line 55 def empty? @conditions.empty? end |
#matches?(item) ⇒ Boolean
Checks if a single item matches all conditions.
72 73 74 |
# File 'lib/pvectl/selectors/base.rb', line 72 def matches?(item) @conditions.all? { |cond| match_condition?(item, cond) } end |