Class: Pvectl::Selectors::Container
- Defined in:
- lib/pvectl/selectors/container.rb,
sig/pvectl/selectors/container.rbs
Overview
Selector for filtering containers.
Extends Base with container-specific field extraction. Supports: status, tags, pool, name, template.
Constant Summary collapse
- SUPPORTED_FIELDS =
%w[status tags pool name template].freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#apply(containers) ⇒ Array<Models::Container>
Applies selector to container collection.
-
#extract_value(container, field) ⇒ String?
Extracts field value from Container model.
-
#match_condition?(container, condition) ⇒ Boolean
Override to handle tags specially.
-
#match_tags_condition?(container, condition) ⇒ Boolean
Special matching for tags field.
Methods inherited from Base
#compare_value, #empty?, #initialize, #matches?, parse, parse_all, #wildcard_match?
Constructor Details
This class inherits a constructor from Pvectl::Selectors::Base
Instance Method Details
#apply(containers) ⇒ Array<Models::Container>
Applies selector to container collection.
29 30 31 32 33 |
# File 'lib/pvectl/selectors/container.rb', line 29 def apply(containers) return containers if empty? containers.select { |ct| matches?(ct) } end |
#extract_value(container, field) ⇒ String?
Extracts field value from Container model.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pvectl/selectors/container.rb', line 43 def extract_value(container, field) case field when "status" container.status when "tags" container. when "pool" container.pool when "name" container.name when "template" container.template? ? "yes" : "no" else raise ArgumentError, "Unknown field: #{field}. Supported: #{SUPPORTED_FIELDS.join(', ')}" end end |
#match_condition?(container, condition) ⇒ Boolean
Override to handle tags specially. Tags in Proxmox are semicolon-separated: "tag1;tag2;tag3" Selector "tags=prod" should match if "prod" is one of the tags.
67 68 69 70 71 72 |
# File 'lib/pvectl/selectors/container.rb', line 67 def match_condition?(container, condition) return (container, condition) if condition[:field] == "tags" return super(container, normalize_boolean_condition(condition)) if condition[:field] == "template" super end |
#match_tags_condition?(container, condition) ⇒ Boolean
Special matching for tags field. Proxmox tags are semicolon-separated, so we check if the value is contained in the tag list.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/pvectl/selectors/container.rb', line 97 def (container, condition) = container. || "" tag_list = .split(";").map(&:strip) case condition[:operator] when :eq tag_list.include?(condition[:value]) when :neq !tag_list.include?(condition[:value]) when :match tag_list.any? { |tag| wildcard_match?(tag, condition[:value]) } when :in (tag_list & condition[:value]).any? else false end end |