Class: MachineTag::Set
- Inherits:
-
Set
- Object
- Set
- MachineTag::Set
- Defined in:
- lib/machine_tag/set.rb
Overview
Set of tags which can be machine tags.
Instance Attribute Summary collapse
-
#machine_tags ⇒ ::Set<Tag>
readonly
The tags in the set which are machine tags.
-
#plain_tags ⇒ ::Set<Tag>
readonly
The tags in the set which are not machine tags.
Instance Method Summary collapse
-
#[](namespace_or_namespace_and_predicate, predicate = nil) ⇒ ::Set<Tag>
Retrieves machine tags in the Set with a matching namespace or namespace and predicate.
-
#add(tag) ⇒ Set
(also: #<<)
Adds a tag to the set of tags.
-
#initialize(enum = nil, &block) ⇒ Set
constructor
Creates a set of tags which can be machine tags.
Constructor Details
#initialize(enum = nil, &block) ⇒ Set
Creates a set of tags which can be machine tags. If String objects are passed in they will be converted to Tag.
48 49 50 51 52 53 54 |
# File 'lib/machine_tag/set.rb', line 48 def initialize(enum = nil, &block) @plain_tags = ::Set.new @machine_tags = ::Set.new @tags_by_namespace = {} @tags_by_namespace_and_predicate = {} super end |
Instance Attribute Details
#machine_tags ⇒ ::Set<Tag> (readonly)
The tags in the set which are machine tags
40 41 42 |
# File 'lib/machine_tag/set.rb', line 40 def @machine_tags end |
#plain_tags ⇒ ::Set<Tag> (readonly)
The tags in the set which are not machine tags
34 35 36 |
# File 'lib/machine_tag/set.rb', line 34 def @plain_tags end |
Instance Method Details
#[](namespace_or_namespace_and_predicate, predicate = nil) ⇒ ::Set<Tag>
Retrieves machine tags in the Set with a matching namespace or namespace and predicate.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/machine_tag/set.rb', line 94 def [](namespace_or_namespace_and_predicate, predicate = nil) if namespace_or_namespace_and_predicate =~ /^#{PREFIX}$/ namespace = namespace_or_namespace_and_predicate unless predicate @tags_by_namespace[namespace] else raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/ @tags_by_namespace_and_predicate["#{namespace}:#{predicate}"] end elsif namespace_or_namespace_and_predicate =~ /^#{NAMESPACE_AND_PREDICATE}$/ namespace_and_predicate = namespace_or_namespace_and_predicate raise ArgumentError, "Separate predicate passed with namespace and predicate: #{namespace_and_predicate.inspect}, #{predicate.inspect}" if predicate @tags_by_namespace_and_predicate[namespace_and_predicate] else raise ArgumentError, "Invalid machine tag namespace and/or predicate: #{namespace_or_namespace_and_predicate.inspect}, #{predicate.inspect}" end end |
#add(tag) ⇒ Set Also known as: <<
Adds a tag to the set of tags. If a String object is passed in it will be converted to Tag.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/machine_tag/set.rb', line 62 def add(tag) tag = Tag.new(tag) unless tag.is_a? Tag super(tag) if tag.machine_tag? @machine_tags << tag @tags_by_namespace[tag.namespace] ||= ::Set.new @tags_by_namespace[tag.namespace] << tag @tags_by_namespace_and_predicate[tag.namespace_and_predicate] ||= ::Set.new @tags_by_namespace_and_predicate[tag.namespace_and_predicate] << tag else @plain_tags << tag end self end |