Browse Source

Add Regexp support to [] on MachineTag::Set.

Douglas Thrift 10 years ago
parent
commit
d987cb51c3
3 changed files with 69 additions and 12 deletions
  1. 34 10
      lib/machine_tag/set.rb
  2. 1 1
      lib/machine_tag/version.rb
  3. 34 1
      spec/machine_tag_set_spec.rb

+ 34 - 10
lib/machine_tag/set.rb

@@ -92,21 +92,45 @@ module MachineTag
     # @return [::Set<Tag>] the machines tags that have the given namespace or namespace and predicate
     #
     def [](namespace_or_namespace_and_predicate, predicate = nil)
-      if namespace_or_namespace_and_predicate =~ /^#{PREFIX}$/
-        namespace = namespace_or_namespace_and_predicate
+      case namespace_or_namespace_and_predicate
+      when Regexp
+        tags = @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
 
-        unless predicate
-          @tags_by_namespace[namespace]
+        case predicate
+        when nil
+        when Regexp
+          tags.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}"]
+          tags.select! { |machine_tag| machine_tag.predicate == 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]
+
+        ::Set.new tags
       else
-        raise ArgumentError, "Invalid machine tag namespace and/or predicate: #{namespace_or_namespace_and_predicate.inspect}, #{predicate.inspect}"
+        if namespace_or_namespace_and_predicate =~ /^#{PREFIX}$/
+          namespace = namespace_or_namespace_and_predicate
+
+          unless predicate
+            @tags_by_namespace[namespace]
+          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}"]
+            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]
+        else
+          raise ArgumentError, "Invalid machine tag namespace and/or predicate: #{namespace_or_namespace_and_predicate.inspect}, #{predicate.inspect}"
+        end
       end
     end
   end

+ 1 - 1
lib/machine_tag/version.rb

@@ -26,5 +26,5 @@
 module MachineTag
   # The version of this library.
   #
-  VERSION = "1.0.0"
+  VERSION = "1.1.0"
 end

+ 34 - 1
spec/machine_tag_set_spec.rb

@@ -44,12 +44,45 @@ describe MachineTag::Set do
   end
 
   describe '#[]' do
-    let(:tags) { MachineTag::Set.new }
+    let(:tags) do
+      MachineTag::Set[
+        'aa:cc=1',
+        'ab:cd=2',
+        'bb:cc=3',
+        'ba:dd=4',
+        'cc:ee=5',
+        'cc:ef=6',
+        'cc:ff=7',
+      ]
+    end
+
+    it 'should retrieve with a Regexp for namespace and no predicate' do
+      tags[/^a/].should eq Set['aa:cc=1', 'ab:cd=2']
+    end
+
+    it 'should retrieve with a Regexp for namespace and predicate as one argument' do
+      tags[/^.b:c.$/].should eq Set['ab:cd=2', 'bb:cc=3']
+    end
+
+    it 'should retrieve with a Regexp for namespace and a String for predicate' do
+      tags[/^[ab]/, 'cc'].should eq Set['aa:cc=1', 'bb:cc=3']
+    end
+
+    it 'should retrieve with a String for namespace and Regexp for predicate' do
+      tags['cc', /^e/].should eq Set['cc:ee=5', 'cc:ef=6']
+    end
+
+    it 'should retrieve with a Regexp for namespace and predicate' do
+      tags[/^[abc]/, /^(cc|ee)$/].should eq Set['aa:cc=1', 'bb:cc=3', 'cc:ee=5']
+    end
 
     it 'should not retrieve with an invalid predicate' do
       expect do
         tags['a', '!']
       end.to raise_error(ArgumentError, 'Invalid machine tag predicate: "!"')
+      expect do
+        tags[/a/, '!']
+      end.to raise_error(ArgumentError, 'Invalid machine tag predicate: "!"')
     end
 
     it 'should not retrieve with a combined namespace and predicate and a predicate' do