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[] @machine_tags = ::Set[] @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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/machine_tag/set.rb', line 96 def [](namespace_or_namespace_and_predicate, predicate = nil) case namespace_or_namespace_and_predicate when Regexp = @machine_tags.select do |machine_tag| machine_tag.namespace =~ namespace_or_namespace_and_predicate || machine_tag.namespace_and_predicate =~ namespace_or_namespace_and_predicate end case predicate when nil when Regexp .select! { |machine_tag| machine_tag.predicate =~ predicate } else raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/ .select! { |machine_tag| machine_tag.predicate == predicate } end ::Set.new else if namespace_or_namespace_and_predicate =~ /^#{PREFIX}$/ namespace = namespace_or_namespace_and_predicate unless predicate @tags_by_namespace[namespace] || Set[] else case predicate when Regexp ::Set.new @tags_by_namespace[namespace].select { |machine_tag| machine_tag.predicate =~ predicate } else raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/ @tags_by_namespace_and_predicate["#{namespace}:#{predicate}"] || Set[] end 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] || Set[] else raise ArgumentError, "Invalid machine tag namespace and/or predicate: #{namespace_or_namespace_and_predicate.inspect}, #{predicate.inspect}" end 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[] @tags_by_namespace[tag.namespace] << tag @tags_by_namespace_and_predicate[tag.namespace_and_predicate] ||= ::Set[] @tags_by_namespace_and_predicate[tag.namespace_and_predicate] << tag else @plain_tags << tag end self end |