machine_tag 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -92,21 +92,45 @@ module MachineTag
92
92
  # @return [::Set<Tag>] the machines tags that have the given namespace or namespace and predicate
93
93
  #
94
94
  def [](namespace_or_namespace_and_predicate, predicate = nil)
95
- if namespace_or_namespace_and_predicate =~ /^#{PREFIX}$/
96
- namespace = namespace_or_namespace_and_predicate
95
+ case namespace_or_namespace_and_predicate
96
+ when Regexp
97
+ tags = @machine_tags.select do |machine_tag|
98
+ machine_tag.namespace =~ namespace_or_namespace_and_predicate ||
99
+ machine_tag.namespace_and_predicate =~ namespace_or_namespace_and_predicate
100
+ end
97
101
 
98
- unless predicate
99
- @tags_by_namespace[namespace]
102
+ case predicate
103
+ when nil
104
+ when Regexp
105
+ tags.select! { |machine_tag| machine_tag.predicate =~ predicate }
100
106
  else
101
107
  raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/
102
- @tags_by_namespace_and_predicate["#{namespace}:#{predicate}"]
108
+ tags.select! { |machine_tag| machine_tag.predicate == predicate }
103
109
  end
104
- elsif namespace_or_namespace_and_predicate =~ /^#{NAMESPACE_AND_PREDICATE}$/
105
- namespace_and_predicate = namespace_or_namespace_and_predicate
106
- raise ArgumentError, "Separate predicate passed with namespace and predicate: #{namespace_and_predicate.inspect}, #{predicate.inspect}" if predicate
107
- @tags_by_namespace_and_predicate[namespace_and_predicate]
110
+
111
+ ::Set.new tags
108
112
  else
109
- raise ArgumentError, "Invalid machine tag namespace and/or predicate: #{namespace_or_namespace_and_predicate.inspect}, #{predicate.inspect}"
113
+ if namespace_or_namespace_and_predicate =~ /^#{PREFIX}$/
114
+ namespace = namespace_or_namespace_and_predicate
115
+
116
+ unless predicate
117
+ @tags_by_namespace[namespace]
118
+ else
119
+ case predicate
120
+ when Regexp
121
+ ::Set.new @tags_by_namespace[namespace].select { |machine_tag| machine_tag.predicate =~ predicate }
122
+ else
123
+ raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/
124
+ @tags_by_namespace_and_predicate["#{namespace}:#{predicate}"]
125
+ end
126
+ end
127
+ elsif namespace_or_namespace_and_predicate =~ /^#{NAMESPACE_AND_PREDICATE}$/
128
+ namespace_and_predicate = namespace_or_namespace_and_predicate
129
+ raise ArgumentError, "Separate predicate passed with namespace and predicate: #{namespace_and_predicate.inspect}, #{predicate.inspect}" if predicate
130
+ @tags_by_namespace_and_predicate[namespace_and_predicate]
131
+ else
132
+ raise ArgumentError, "Invalid machine tag namespace and/or predicate: #{namespace_or_namespace_and_predicate.inspect}, #{predicate.inspect}"
133
+ end
110
134
  end
111
135
  end
112
136
  end
@@ -26,5 +26,5 @@
26
26
  module MachineTag
27
27
  # The version of this library.
28
28
  #
29
- VERSION = "1.0.0"
29
+ VERSION = "1.1.0"
30
30
  end
@@ -44,12 +44,45 @@ describe MachineTag::Set do
44
44
  end
45
45
 
46
46
  describe '#[]' do
47
- let(:tags) { MachineTag::Set.new }
47
+ let(:tags) do
48
+ MachineTag::Set[
49
+ 'aa:cc=1',
50
+ 'ab:cd=2',
51
+ 'bb:cc=3',
52
+ 'ba:dd=4',
53
+ 'cc:ee=5',
54
+ 'cc:ef=6',
55
+ 'cc:ff=7',
56
+ ]
57
+ end
58
+
59
+ it 'should retrieve with a Regexp for namespace and no predicate' do
60
+ tags[/^a/].should eq Set['aa:cc=1', 'ab:cd=2']
61
+ end
62
+
63
+ it 'should retrieve with a Regexp for namespace and predicate as one argument' do
64
+ tags[/^.b:c.$/].should eq Set['ab:cd=2', 'bb:cc=3']
65
+ end
66
+
67
+ it 'should retrieve with a Regexp for namespace and a String for predicate' do
68
+ tags[/^[ab]/, 'cc'].should eq Set['aa:cc=1', 'bb:cc=3']
69
+ end
70
+
71
+ it 'should retrieve with a String for namespace and Regexp for predicate' do
72
+ tags['cc', /^e/].should eq Set['cc:ee=5', 'cc:ef=6']
73
+ end
74
+
75
+ it 'should retrieve with a Regexp for namespace and predicate' do
76
+ tags[/^[abc]/, /^(cc|ee)$/].should eq Set['aa:cc=1', 'bb:cc=3', 'cc:ee=5']
77
+ end
48
78
 
49
79
  it 'should not retrieve with an invalid predicate' do
50
80
  expect do
51
81
  tags['a', '!']
52
82
  end.to raise_error(ArgumentError, 'Invalid machine tag predicate: "!"')
83
+ expect do
84
+ tags[/a/, '!']
85
+ end.to raise_error(ArgumentError, 'Invalid machine tag predicate: "!"')
53
86
  end
54
87
 
55
88
  it 'should not retrieve with a combined namespace and predicate and a predicate' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: machine_tag
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-16 00:00:00.000000000 Z
12
+ date: 2013-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler