pebbles-uid 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -194,13 +194,28 @@ Or install it yourself as:
194
194
  => 'post.card:tourism.norway.fjords$1234'
195
195
 
196
196
  uid.to_hash
197
- => {'genus' => 'post.card', 'path_0' => 'tourism', 'path_1' => 'norway', 'path_2' => 'fjords', 'oid' => '1234'}
197
+ => {'genus_0' => 'post', 'genus_1' => 'card', 'path_0' => 'tourism', 'path_1' => 'norway', 'path_2' => 'fjords', 'oid' => '1234'}
198
198
 
199
- uid.to_hash(:verbose => true, :suffix => '')
200
- => {'genus_0_' => 'post', 'genus_1_' => 'card', 'path_0_' => 'tourism', 'path_1_' => 'norway', 'path_2_' => 'fjords', 'oid_' => '1234'}
199
+ ## TODO
201
200
 
202
- uid.to_hash(:verbose => true, :suffix => 'xyz', :genus => 'klass', :path => 'label' => :oid => 'id')
203
- => {'klass_0_xyz' => 'post', 'klass_1_xyz' => 'card', 'label_0_xyz' => 'tourism', 'label_1_xyz' => 'norway', 'label_2_xyz' => 'fjords', 'id_xyz' => '1234'}
201
+ [ ] handle caching for path-specific queries(*)
202
+
203
+ (*) Caching and path-specific queries.
204
+
205
+ A realm has multiple publication channels, e.g. nuz.paper, nuz.magazine, and nuz.tv.
206
+ An article in the realm can be present in one or more of these paths. Assume that in
207
+ this instance, the article is only in the paper and tv. e.g:
208
+
209
+ ```
210
+ post.article:nuz.paper$123
211
+ post.article:nuz.tv$123
212
+ ```
213
+
214
+ If you search for post.article:nuz.*$123 then you will find it, but assume for a second
215
+ that you only want to find things that are in the `nuz.magazine` path. If you search on
216
+ the cache key, then you will find the `$123` article, but it should not have been returned.
217
+
218
+ I don't know what the solution to this is.
204
219
 
205
220
  ## Contributing
206
221
 
data/lib/pebbles-uid.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  require "pebbles-uid/version"
2
2
 
3
+ require 'pebbles-uid/parse'
4
+ require 'pebbles-uid/cache_key'
3
5
  require 'pebbles-uid/query'
4
6
  require "pebbles-uid/conditions"
5
7
  require "pebbles-uid/labels"
6
- require "pebbles-uid/genus"
7
- require "pebbles-uid/path"
8
8
  require "pebbles-uid/oid"
9
9
 
10
10
  module Pebbles
@@ -12,15 +12,15 @@ module Pebbles
12
12
 
13
13
  class << self
14
14
 
15
- def query(s)
16
- Pebbles::Uid::Query.new(s)
17
- end
18
-
19
15
  def parse(s)
20
16
  /^(?<genus>.*):(?<path>[^\$]*)\$?(?<oid>.*)$/ =~ s
21
17
  [genus, path, oid.empty? ? nil : oid]
22
18
  end
23
19
 
20
+ def query(s)
21
+ Pebbles::Uid::Query.new(s)
22
+ end
23
+
24
24
  def genus(s)
25
25
  parse(s)[0]
26
26
  end
@@ -35,23 +35,18 @@ module Pebbles
35
35
 
36
36
  def valid_path?(path)
37
37
  return false if path.empty?
38
- Path.new(path).valid_with?(/^[a-z0-9_-]+$/)
38
+ Labels.new(path).valid_with?(/^[a-z0-9_-]+$/)
39
39
  end
40
40
 
41
41
  def valid_genus?(genus)
42
42
  return false if genus.empty?
43
- Genus.new(genus).valid_with?(/^[a-z0-9_-]+$/)
43
+ Labels.new(genus).valid_with?(/^[a-z0-9_-]+$/)
44
44
  end
45
45
 
46
46
  def valid_oid?(oid)
47
47
  return true if !oid || oid.empty?
48
48
  !!(oid =~ /^[^,|]+$/)
49
49
  end
50
-
51
- def cache_key(uid)
52
- genus, path, oid = parse(uid)
53
- "#{genus}:#{Path.new(path).realm}.*$#{oid}"
54
- end
55
50
  end
56
51
 
57
52
  attr_reader :genus, :path, :oid
@@ -81,15 +76,15 @@ module Pebbles
81
76
  end
82
77
 
83
78
  def species
84
- @species ||= genus_labels.species
79
+ @species ||= genus_labels.tail.join('.')
85
80
  end
86
81
 
87
82
  def path_labels
88
- @path_labels ||= Path.new(path)
83
+ @path_labels ||= Labels.new(path, :name => 'path')
89
84
  end
90
85
 
91
86
  def genus_labels
92
- @genus_labels ||= Genus.new(genus)
87
+ @genus_labels ||= Labels.new(genus, :name => 'genus')
93
88
  end
94
89
 
95
90
  def valid?
@@ -122,8 +117,15 @@ module Pebbles
122
117
  [genus, path, oid].compact
123
118
  end
124
119
 
120
+ def to_hash
121
+ hash = genus_labels.to_hash.merge(path_labels.to_hash)
122
+ hash = hash.merge(:oid => oid) if oid?
123
+ hash
124
+ end
125
+
125
126
  def cache_key
126
127
  "#{genus}:#{realm}.*$#{oid}"
127
128
  end
129
+
128
130
  end
129
131
  end
@@ -0,0 +1,13 @@
1
+ module Pebbles
2
+ class Uid
3
+
4
+ class << self
5
+ def cache_key(uid)
6
+ genus, path, oid = parse(uid)
7
+ "#{genus}:#{Labels.new(path).first}.*$#{oid}"
8
+ end
9
+ end
10
+
11
+ end
12
+ end
13
+
@@ -7,13 +7,12 @@ module Pebbles
7
7
 
8
8
  NO_MARKER = Class.new
9
9
 
10
- attr_reader :values, :name, :suffix, :stop, :verbose
10
+ attr_reader :values, :name, :suffix, :stop
11
11
 
12
12
  def initialize(values, options = {})
13
13
  @values = values
14
14
  @name = options.fetch(:name) { 'label' }
15
15
  @suffix = options.fetch(:suffix) { nil }
16
- @verbose = options.fetch(:verbose) { true }
17
16
  @stop = options.fetch(:stop) { NO_MARKER }
18
17
  if values.last == '*'
19
18
  @stop = NO_MARKER
@@ -21,15 +20,11 @@ module Pebbles
21
20
  end
22
21
  end
23
22
 
24
- alias :verbose? :verbose
25
-
26
23
  def next
27
24
  label(values.length)
28
25
  end
29
26
 
30
27
  def to_hash
31
- return {label => values.join('.')} unless verbose?
32
-
33
28
  collection = labelize
34
29
  collection.merge!(stop_label) if use_stop_marker?
35
30
  collection
@@ -57,7 +52,7 @@ module Pebbles
57
52
  end
58
53
 
59
54
  def label(i = nil)
60
- [name, i, suffix].compact.join('_')
55
+ [name, i, suffix].compact.join('_').to_sym
61
56
  end
62
57
 
63
58
  def stop_label
@@ -2,15 +2,26 @@ module Pebbles
2
2
  class Uid
3
3
  class Labels
4
4
 
5
- attr_reader :values, :prefix
5
+ attr_reader :values, :name
6
6
  def initialize(*values)
7
+ options = values.pop if values.last.is_a?(Hash)
8
+ options ||= {}
7
9
  @values = values.flatten.compact.map {|v| v.split('.') }.flatten
10
+ @name = options[:name]
11
+ end
12
+
13
+ def first
14
+ values.first
8
15
  end
9
16
 
10
17
  def tail
11
18
  values[1..-1]
12
19
  end
13
20
 
21
+ def ambiguous?
22
+ value == '*' || values.empty? || wildcard?
23
+ end
24
+
14
25
  def valid_with?(pattern)
15
26
  !empty? && values.all? {|value| value[pattern] }
16
27
  end
@@ -37,7 +48,7 @@ module Pebbles
37
48
  end
38
49
 
39
50
  def to_hash(options = {})
40
- Conditions.new(values, options).to_hash
51
+ Conditions.new(values, {:name => name}.merge(options)).to_hash
41
52
  end
42
53
 
43
54
  end
@@ -0,0 +1,14 @@
1
+ module Pebbles
2
+ class Uid
3
+
4
+ class << self
5
+
6
+ def parse(s)
7
+ /^(?<genus>.*):(?<path>[^\$]*)\$?(?<oid>.*)$/ =~ s
8
+ [genus, path, oid.empty? ? nil : oid]
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+
@@ -35,11 +35,11 @@ module Pebbles
35
35
  end
36
36
 
37
37
  def species?
38
- !!Genus.new(genus).species
38
+ !genus_wrapper.tail.empty?
39
39
  end
40
40
 
41
41
  def species
42
- Genus.new(genus).species
42
+ genus_wrapper.tail.join('.')
43
43
  end
44
44
 
45
45
  def oid?
@@ -50,33 +50,51 @@ module Pebbles
50
50
  terms.map { |t| Pebbles::Uid.cache_key(t) }
51
51
  end
52
52
 
53
+ def to_hash
54
+ if list?
55
+ raise RuntimeError.new('Cannot compute a conditions hash for a list of uids')
56
+ end
57
+
58
+ hash = genus_wrapper.to_hash.merge(path_wrapper.to_hash)
59
+ hash = hash.merge(:oid => oid) if oid?
60
+ hash
61
+ end
62
+
53
63
  private
54
64
 
65
+ def genus_wrapper
66
+ @genus_wrapper ||= Labels.new(genus, :name => 'genus')
67
+ end
68
+
69
+ def path_wrapper
70
+ @path_wrapper ||= Labels.new(path, :name => 'path')
71
+ end
72
+
55
73
  def wildcard_query?
56
74
  return false if term.include?(',')
57
- species, _, oid = Pebbles::Uid.parse(term)
58
- return true if Genus.new(species).wildcard?
75
+ genus, _, oid = Pebbles::Uid.parse(term)
76
+ return true if Labels.new(genus).wildcard?
59
77
  return true if oid.nil? || oid.empty? || oid == '*'
60
78
  false
61
79
  end
62
80
 
63
81
  def extract_terms
64
82
  term.split(',').map do |uid|
65
- species, path, oid = Pebbles::Uid.parse(uid)
66
- species_labels = Genus.new(species)
67
- path_labels = Path.new(path)
68
- oid_box = Oid.new(oid)
83
+ _genus, _path, _oid = Pebbles::Uid.parse(uid)
84
+ genus_labels = Labels.new(_genus)
85
+ path_labels = Labels.new(_path)
86
+ oid_box = Oid.new(_oid)
69
87
 
70
- raise ArgumentError.new('Realm must be specified') unless path_labels.realm?
71
- raise ArgumentError.new('Genus must unambiguous') if species_labels.ambiguous?
88
+ raise ArgumentError.new('Realm must be specified') if path_labels.empty? || path_labels.first == '*'
89
+ raise ArgumentError.new('Genus must unambiguous') if genus_labels.ambiguous?
72
90
  raise ArgumentError.new('Oid must be unambiguous') if oid_box.ambiguous?
73
91
 
74
- @realm ||= path_labels.realm
75
- raise ArgumentError.new('One realm at a time, please') if @realm != path_labels.realm
92
+ @realm ||= path_labels.first
93
+ raise ArgumentError.new('One realm at a time, please') if @realm != path_labels.first
76
94
 
77
95
  if oid_box.multiple?
78
- oid.split('|').map do |s|
79
- "#{species}:#{path}$#{s}"
96
+ _oid.split('|').map do |s|
97
+ "#{_genus}:#{_path}$#{s}"
80
98
  end
81
99
  else
82
100
  uid
@@ -1,5 +1,5 @@
1
1
  module Pebbles
2
2
  class Uid
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -4,52 +4,42 @@ describe Pebbles::Uid::Conditions do
4
4
 
5
5
  subject { Pebbles::Uid::Conditions.new(%w(a b c)) }
6
6
 
7
- its(:to_hash) { should eq('label_0' => "a", 'label_1' => "b", 'label_2' => "c") }
7
+ its(:to_hash) { should eq(:label_0 => "a", :label_1 => "b", :label_2 => "c") }
8
8
 
9
9
  describe "customized labels" do
10
10
  subject { Pebbles::Uid::Conditions.new(%w(p r q), :name => 'dot', :suffix => '') }
11
- its(:to_hash) { should eq('dot_0_' => 'p', 'dot_1_' => 'r', 'dot_2_' => 'q') }
12
- end
13
-
14
- it "has a non-verbose mode" do
15
- uid = Pebbles::Uid::Conditions.new(%w(k l m), :name => 'stuff', :verbose => false)
16
- uid.to_hash.should eq({'stuff' => 'k.l.m'})
17
- end
18
-
19
- specify "non-verbose still takes a suffix" do
20
- uid = Pebbles::Uid::Conditions.new(%w(k l m), :name => 'stuff', :verbose => false, :suffix => 'xyz')
21
- uid.to_hash.should eq({'stuff_xyz' => 'k.l.m'})
11
+ its(:to_hash) { should eq(:dot_0_ => 'p', :dot_1_ => 'r', :dot_2_ => 'q') }
22
12
  end
23
13
 
24
14
  describe "with a stop label" do
25
15
  subject { Pebbles::Uid::Conditions.new(%w(x y z), :stop => nil) }
26
- its(:to_hash) { should eq('label_0' => "x", 'label_1' => "y", 'label_2' => "z", 'label_3' => nil) }
16
+ its(:to_hash) { should eq(:label_0 => "x", :label_1 => "y", :label_2 => "z", :label_3 => nil) }
27
17
  end
28
18
 
29
19
  describe "next label" do
30
20
  subject { Pebbles::Uid::Conditions.new(%w(h j k l), :name => 'vim') }
31
- its(:next) { should eq('vim_4') }
21
+ its(:next) { should eq(:vim_4) }
32
22
  end
33
23
 
34
24
  describe "with pipes" do
35
25
  subject { Pebbles::Uid::Conditions.new(%w(a b|c d), :stop => nil) }
36
- its(:to_hash) { should eq('label_0' => "a", 'label_1' => ['b', 'c'], 'label_2' => "d", 'label_3' => nil) }
26
+ its(:to_hash) { should eq(:label_0 => "a", :label_1 => ['b', 'c'], :label_2 => "d", :label_3 => nil) }
37
27
  end
38
28
 
39
29
  describe "with an asterisk" do
40
30
  # ignores stop marker if it is terminated by an asterisk
41
31
  subject { Pebbles::Uid::Conditions.new(%w(a b *), :stop => nil) }
42
- its(:to_hash) { should eq('label_0' => "a", 'label_1' => 'b') }
32
+ its(:to_hash) { should eq(:label_0 => "a", :label_1 => 'b') }
43
33
  end
44
34
 
45
35
  describe "with an asterisk" do
46
36
  subject { Pebbles::Uid::Conditions.new(%w(a ^b c), :stop => nil) }
47
- its(:to_hash) { should eq('label_0' => "a", 'label_1' => ['b', nil], 'label_2' => ['c', nil], 'label_3' => nil) }
37
+ its(:to_hash) { should eq(:label_0 => "a", :label_1 => ['b', nil], :label_2 => ['c', nil], :label_3 => nil) }
48
38
  end
49
39
 
50
40
  describe "complicated stuff" do
51
41
  subject { Pebbles::Uid::Conditions.new(%w(a ^b|c d *), :stop => nil) }
52
- its(:to_hash) { should eq('label_0' => "a", 'label_1' => ['b', 'c', nil], 'label_2' => ['d', nil]) }
42
+ its(:to_hash) { should eq(:label_0 => "a", :label_1 => ['b', 'c', nil], :label_2 => ['d', nil]) }
53
43
  end
54
44
 
55
45
  end
data/spec/labels_spec.rb CHANGED
@@ -3,16 +3,12 @@ require 'pebbles-uid/labels'
3
3
 
4
4
  describe Pebbles::Uid::Labels do
5
5
 
6
- subject { Pebbles::Uid::Labels.new('a.b.c') }
6
+ subject { Pebbles::Uid::Labels.new('a.b.c', :name => 'thing') }
7
7
  its(:to_s) { should eq('a.b.c') }
8
8
  its(:to_a) { should eq(%w(a b c)) }
9
- its(:to_hash) { should eq('label_0' => "a", 'label_1' => "b", 'label_2' => "c") }
9
+ its(:to_hash) { should eq(:thing_0 => "a", :thing_1 => "b", :thing_2 => "c") }
10
10
  its(:tail) { should eq(%w(b c)) }
11
11
 
12
- it "delegates to conditions" do
13
- subject.to_hash(:name => 'thing', :verbose => false).should eq({'thing' => 'a.b.c'})
14
- end
15
-
16
12
  it "has a size" do
17
13
  subject.size.should eq(3)
18
14
  end
data/spec/query_spec.rb CHANGED
@@ -1,4 +1,9 @@
1
- require 'pebbles-uid'
1
+ require 'pebbles-uid/parse'
2
+ require 'pebbles-uid/cache_key'
3
+ require 'pebbles-uid/labels'
4
+ require 'pebbles-uid/conditions'
5
+ require 'pebbles-uid/oid'
6
+ require 'pebbles-uid/query'
2
7
 
3
8
  describe Pebbles::Uid::Query do
4
9
 
@@ -39,9 +44,14 @@ describe Pebbles::Uid::Query do
39
44
 
40
45
  its(:cache_keys) { should eq(['post:area51.*$abc', 'post:area51.*$xyz']) }
41
46
 
42
- it "handles a pipe-delimited list of oids" do
43
- query = Pebbles::Uid::Query.new("post:area51$abc|xyz")
44
- query.terms.should eq(['post:area51$abc', 'post:area51$xyz'])
47
+ context "by oid" do
48
+ subject { Pebbles::Uid::Query.new("post:area51$abc|xyz") }
49
+
50
+ its(:terms) { should eq(['post:area51$abc', 'post:area51$xyz']) }
51
+
52
+ it "can't do hashes under these circumstances" do
53
+ ->{ subject.to_hash }.should raise_error(RuntimeError)
54
+ end
45
55
  end
46
56
 
47
57
  it "ignores crazy wildcard stuff in the path" do
@@ -89,6 +99,8 @@ describe Pebbles::Uid::Query do
89
99
  its(:genus?) { should == false }
90
100
  its(:path?) { should == false }
91
101
  its(:oid?) { should == false }
102
+
103
+ its(:to_hash) { should == {} }
92
104
  end
93
105
 
94
106
  context "everything, with any oid" do
@@ -97,6 +109,7 @@ describe Pebbles::Uid::Query do
97
109
  its(:genus?) { should == false }
98
110
  its(:path?) { should == false }
99
111
  its(:oid?) { should == false }
112
+ its(:to_hash) { should == {} }
100
113
  end
101
114
 
102
115
  context "a genus" do
@@ -104,23 +117,27 @@ describe Pebbles::Uid::Query do
104
117
  its(:genus?) { should == true }
105
118
  its(:genus) { should eq('beast') }
106
119
  its(:species?) { should == false }
120
+ its(:to_hash) { should == {:genus_0 => 'beast'} }
107
121
  end
108
122
 
109
123
  context "a species" do
110
124
  subject { Pebbles::Uid::Query.new('beast.mythical.hairy:*$*') }
111
125
  its(:species?) { should == true }
112
126
  its(:species) { should eq('mythical.hairy') }
127
+ its(:to_hash) { should == {:genus_0 => 'beast', :genus_1 => 'mythical', :genus_2 => 'hairy'} }
113
128
  end
114
129
 
115
130
  context "a path" do
116
131
  subject { Pebbles::Uid::Query.new('*:area51.*') }
117
132
  its(:path?) { should == true }
133
+ its(:to_hash) { should == {:path_0 => 'area51'} }
118
134
  end
119
135
 
120
136
  context "one oid" do
121
137
  subject { Pebbles::Uid::Query.new('*:*$yak') }
122
138
  its(:oid?) { should == true }
123
139
  its(:oid) { should == 'yak' }
140
+ its(:to_hash) { should == {:oid => 'yak'} }
124
141
  end
125
142
 
126
143
  end
data/spec/uid_spec.rb CHANGED
@@ -4,16 +4,6 @@ describe Pebbles::Uid do
4
4
 
5
5
  let(:uid) { 'post.card:tourism.norway.fjords$1234' }
6
6
 
7
- describe "extracting single elements" do
8
- specify { Pebbles::Uid.oid(uid).should eq('1234') }
9
- specify { Pebbles::Uid.path(uid).should eq('tourism.norway.fjords') }
10
- specify { Pebbles::Uid.genus(uid).should eq('post.card') }
11
-
12
- describe "with missing oid" do
13
- specify { Pebbles::Uid.oid('post:a.b.c').should eq(nil) }
14
- end
15
- end
16
-
17
7
  describe "query" do
18
8
  it "returns a query object" do
19
9
  s = 'post:tourism.*$*'
@@ -22,58 +12,58 @@ describe Pebbles::Uid do
22
12
  end
23
13
  end
24
14
 
25
- describe "requirements" do
26
- it "must have a genus" do
15
+ describe "extracts elements" do
16
+ specify "genus" do
17
+ Pebbles::Uid.genus(uid).should eq('post.card')
18
+ end
19
+
20
+ specify "path" do
21
+ Pebbles::Uid.path(uid).should eq('tourism.norway.fjords')
22
+ end
23
+
24
+ specify "oid" do
25
+ Pebbles::Uid.oid(uid).should eq('1234')
26
+ end
27
+
28
+ specify "non-existant oid" do
29
+ Pebbles::Uid.oid('post:a.b.c').should be_nil
30
+ end
31
+ end
32
+
33
+ describe "must" do
34
+ specify "have a genus" do
27
35
  ->{ Pebbles::Uid.new(':tourism.norway$1') }.should raise_error(ArgumentError)
28
36
  end
29
37
 
30
- it "must have a realm" do
38
+ specify "have a realm" do
31
39
  ->{ Pebbles::Uid.new('post:$1') }.should raise_error(ArgumentError)
32
40
  end
41
+ end
33
42
 
34
- it "can skip the oid" do
43
+ describe "may" do
44
+ specify "have no oid" do
35
45
  ->{ Pebbles::Uid.new('post:tourism') }.should_not raise_error
36
46
  end
47
+ end
37
48
 
38
- it "doesn't accept wildcard genus" do
49
+ describe "rejects" do
50
+ it "wildcard genus" do
39
51
  ->{ Pebbles::Uid.new('post.*:tourism$1') }.should raise_error(ArgumentError)
40
52
  ->{ Pebbles::Uid.new('post|card:tourism$1') }.should raise_error(ArgumentError)
41
53
  ->{ Pebbles::Uid.new('post.^b.c:tourism$1') }.should raise_error(ArgumentError)
42
54
  end
43
55
 
44
- it "doesn't accept wildcard paths" do
56
+ it "wildcard paths" do
45
57
  ->{ Pebbles::Uid.new('post:tourism.*$1') }.should raise_error(ArgumentError)
46
58
  ->{ Pebbles::Uid.new('post:tourism|blogging$1') }.should raise_error(ArgumentError)
47
59
  ->{ Pebbles::Uid.new('post:tourism.^b.c$1') }.should raise_error(ArgumentError)
48
60
  end
49
61
 
50
- it "doesn't accept wildcard oid" do
62
+ it "wildcard oid" do
51
63
  ->{ Pebbles::Uid.new('post:tourism$*') }.should raise_error(ArgumentError)
52
64
  end
53
65
  end
54
66
 
55
- subject { Pebbles::Uid.new(uid) }
56
-
57
- its(:to_s) { should eq(uid) }
58
- its(:realm) { should eq('tourism') }
59
- its(:genus) { should eq('post.card') }
60
- its(:species) { should eq('card') }
61
- its(:path) { should eq('tourism.norway.fjords') }
62
- its(:oid) { should eq('1234') }
63
- its(:oid?) { should == true }
64
-
65
- its(:cache_key) { should eq('post.card:tourism.*$1234') }
66
-
67
- context "when pending creation" do
68
-
69
- let(:uid) { 'post.doc:universities.europe.norway' }
70
- subject { Pebbles::Uid.new(uid) }
71
-
72
- its(:to_s) { should eq(uid) }
73
- its(:oid) { should be_nil }
74
-
75
- end
76
-
77
67
  context "paths" do
78
68
  ["abc123", "abc.123", "abc.de-f.123"].each do |path|
79
69
  specify "#{path} is a valid path" do
@@ -103,9 +93,6 @@ describe Pebbles::Uid do
103
93
  end
104
94
 
105
95
  context "oids" do
106
- it "can be empty" do
107
- Pebbles::Uid.new("beast:mythical").oid?.should == false
108
- end
109
96
 
110
97
  it "cannot contain pipes" do
111
98
  Pebbles::Uid.valid_oid?("abc|xyz").should == false
@@ -127,4 +114,38 @@ describe Pebbles::Uid do
127
114
  Pebbles::Uid.valid_oid?("holy+%^&*s!").should == true
128
115
  end
129
116
  end
117
+
118
+ describe "A Uid" do
119
+ subject { Pebbles::Uid.new(uid) }
120
+
121
+ its(:to_s) { should eq(uid) }
122
+ its(:realm) { should eq('tourism') }
123
+ its(:genus) { should eq('post.card') }
124
+ its(:species) { should eq('card') }
125
+ its(:path) { should eq('tourism.norway.fjords') }
126
+ its(:oid) { should eq('1234') }
127
+ its(:oid?) { should == true }
128
+
129
+ its(:cache_key) { should eq('post.card:tourism.*$1234') }
130
+
131
+ its(:to_hash) do
132
+ should eq(:genus_0 => 'post', :genus_1 => 'card', :path_0 => 'tourism', :path_1 => 'norway', :path_2 => 'fjords', :oid => '1234')
133
+ end
134
+
135
+ context "without an oid" do
136
+ it "excludes the oid key from the hash" do
137
+ Pebbles::Uid.new('post.doc:a.b.c').to_hash.should eq(:genus_0 => 'post', :genus_1 => 'doc', :path_0 => 'a', :path_1 => 'b', :path_2 => 'c')
138
+ end
139
+ end
140
+
141
+ context "when pending creation" do
142
+
143
+ let(:uid) { 'post.doc:universities.europe.norway' }
144
+ subject { Pebbles::Uid.new(uid) }
145
+
146
+ its(:to_s) { should eq(uid) }
147
+ its(:oid) { should be_nil }
148
+
149
+ end
150
+ end
130
151
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pebbles-uid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-01 00:00:00.000000000 Z
12
+ date: 2012-10-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70278976164860 !ruby/object:Gem::Requirement
16
+ requirement: &70108731034840 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70278976164860
24
+ version_requirements: *70108731034840
25
25
  description: Handle pebble UIDs conveniently.
26
26
  email:
27
27
  - katrina.owen@gmail.com
@@ -35,21 +35,19 @@ files:
35
35
  - README.md
36
36
  - Rakefile
37
37
  - lib/pebbles-uid.rb
38
+ - lib/pebbles-uid/cache_key.rb
38
39
  - lib/pebbles-uid/conditions.rb
39
- - lib/pebbles-uid/genus.rb
40
40
  - lib/pebbles-uid/labels.rb
41
41
  - lib/pebbles-uid/oid.rb
42
- - lib/pebbles-uid/path.rb
42
+ - lib/pebbles-uid/parse.rb
43
43
  - lib/pebbles-uid/query.rb
44
44
  - lib/pebbles-uid/version.rb
45
45
  - lib/pebbles-uid/wildcard.rb
46
46
  - lib/pebbles/uid.rb
47
47
  - pebbles-uid.gemspec
48
48
  - spec/conditions_spec.rb
49
- - spec/genus_spec.rb
50
49
  - spec/labels_spec.rb
51
50
  - spec/oid_spec.rb
52
- - spec/path_spec.rb
53
51
  - spec/query_spec.rb
54
52
  - spec/uid_spec.rb
55
53
  - spec/wildcard_spec.rb
@@ -80,10 +78,8 @@ summary: Unique identifiers in the Pebblestack universe, in the format species[.
80
78
  where the path is a dot-delimited set of labels, the first of which (realm) is required.
81
79
  test_files:
82
80
  - spec/conditions_spec.rb
83
- - spec/genus_spec.rb
84
81
  - spec/labels_spec.rb
85
82
  - spec/oid_spec.rb
86
- - spec/path_spec.rb
87
83
  - spec/query_spec.rb
88
84
  - spec/uid_spec.rb
89
85
  - spec/wildcard_spec.rb
@@ -1,25 +0,0 @@
1
- module Pebbles
2
- class Uid
3
- class Genus < Labels
4
-
5
- def ambiguous?
6
- value == '*' || values.empty? || wildcard?
7
- end
8
-
9
- def species
10
- return if size <= 1
11
-
12
- tail.join('.')
13
- end
14
-
15
- def species?
16
- species != '*'
17
- end
18
-
19
- def to_hash(options = {})
20
- super({:verbose => false, :name => 'genus'}.merge(options))
21
- end
22
-
23
- end
24
- end
25
- end
@@ -1,20 +0,0 @@
1
- module Pebbles
2
- class Uid
3
- class Path < Labels
4
-
5
- def realm
6
- values.first
7
- end
8
-
9
- def realm?
10
- return false if realm == '*'
11
- !!realm
12
- end
13
-
14
- def to_hash(options = {})
15
- super({:name => 'path'}.merge(options))
16
- end
17
-
18
- end
19
- end
20
- end
data/spec/genus_spec.rb DELETED
@@ -1,33 +0,0 @@
1
- require 'pebbles-uid/labels'
2
- require 'pebbles-uid/conditions'
3
- require 'pebbles-uid/genus'
4
-
5
- describe Pebbles::Uid::Genus do
6
-
7
- subject { Pebbles::Uid::Genus.new('unicorn') }
8
-
9
- its(:to_s) { should eq('unicorn') }
10
-
11
- its(:species) { should be_nil }
12
- its(:to_hash) { should eq('genus' => 'unicorn') }
13
-
14
- context "subtypes" do
15
-
16
- subject { Pebbles::Uid::Genus.new('unicorn', 'dust', 'sparkles') }
17
- its(:to_s) { should eq('unicorn.dust.sparkles') }
18
- its(:species) { should eq('dust.sparkles') }
19
- its(:to_hash) { should eq('genus' => 'unicorn.dust.sparkles') }
20
-
21
- it "doesn't have a species if there's a wildcard" do
22
- genus = Pebbles::Uid::Genus.new('unicorn.*')
23
- genus.species?.should == false
24
- end
25
-
26
- it "can customize the hash" do
27
- subject.to_hash(:verbose => true).should eq('genus_0' => 'unicorn', 'genus_1' => 'dust', 'genus_2' => 'sparkles')
28
- end
29
- end
30
-
31
- it { Pebbles::Uid::Genus.new('unicorn.horn').species.should eq('horn') }
32
-
33
- end
data/spec/path_spec.rb DELETED
@@ -1,24 +0,0 @@
1
- require 'pebbles-uid/conditions'
2
- require 'pebbles-uid/labels'
3
- require 'pebbles-uid/path'
4
-
5
- describe Pebbles::Uid::Path do
6
-
7
- subject { Pebbles::Uid::Path.new('magical', 'forrest', 'clearing') }
8
-
9
- its(:realm) { should eq('magical') }
10
- its(:realm?) { should == true }
11
- its(:to_s) { should eq('magical.forrest.clearing') }
12
- its(:to_a) { should eq(%w(magical forrest clearing)) }
13
- its(:to_hash) { should eq({'path_0' => 'magical', 'path_1' => 'forrest', 'path_2' => 'clearing'}) }
14
- its(:wildcard?) { should == false }
15
-
16
- it "can customize the hash" do
17
- subject.to_hash(:name => 'label').should eq({'label_0' => 'magical', 'label_1' => 'forrest', 'label_2' => 'clearing'})
18
- end
19
-
20
- specify "wildcard realm" do
21
- Pebbles::Uid::Path.new('*').realm?.should == false
22
- end
23
-
24
- end