pebbles-uid 0.0.9 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
data/lib/pebbles-uid.rb CHANGED
@@ -6,6 +6,7 @@ require 'pebbles-uid/query'
6
6
  require "pebbles-uid/conditions"
7
7
  require "pebbles-uid/labels"
8
8
  require "pebbles-uid/oid"
9
+ require "pebbles-uid/roots"
9
10
 
10
11
  module Pebbles
11
12
  class Uid
@@ -4,7 +4,7 @@ module Pebbles
4
4
 
5
5
  def parse(s)
6
6
  /^(?<species>.*):(?<path>[^\$]*)\$?(?<oid>.*)$/ =~ s
7
- [species, path, oid.empty? ? nil : oid]
7
+ [species, path, oid && oid.empty? ? nil : oid]
8
8
  end
9
9
 
10
10
  def build(species, path, oid)
@@ -1,6 +1,7 @@
1
1
  module Pebbles
2
2
  class Uid
3
3
  class Labels
4
+ include Enumerable
4
5
 
5
6
  NO_MARKER = Class.new
6
7
 
@@ -15,14 +16,22 @@ module Pebbles
15
16
  @max_depth = options[:max_depth]
16
17
  end
17
18
 
18
- def first
19
- values.first
19
+ def each(&block)
20
+ values.each {|value| yield value}
20
21
  end
21
22
 
22
23
  def tail
23
24
  values[1..-1]
24
25
  end
25
26
 
27
+ def parent_of?(other)
28
+ parent = true
29
+ values.each_with_index do |value, i|
30
+ parent = false unless value == other.values[i]
31
+ end
32
+ parent
33
+ end
34
+
26
35
  def ambiguous?
27
36
  value == '*' || values.empty? || wildcard?
28
37
  end
@@ -35,10 +44,6 @@ module Pebbles
35
44
  values.size
36
45
  end
37
46
 
38
- def to_a
39
- values
40
- end
41
-
42
47
  def to_s
43
48
  values.join('.')
44
49
  end
@@ -19,7 +19,9 @@ module Pebbles
19
19
  @terms = extract_terms
20
20
  end
21
21
 
22
- if !list?
22
+ if list?
23
+ @species, @path, _ = Pebbles::Uid.parse(terms.first)
24
+ else
23
25
  @species, @path, @oid = Pebbles::Uid.parse(term)
24
26
  end
25
27
  end
@@ -55,6 +57,10 @@ module Pebbles
55
57
  !species_wrapper.tail.empty?
56
58
  end
57
59
 
60
+ def genus
61
+ species.split('.').first
62
+ end
63
+
58
64
  def epiteth
59
65
  species_wrapper.tail.join('.')
60
66
  end
@@ -0,0 +1,14 @@
1
+ module Pebbles
2
+ class Uid
3
+ def self.root_paths(uids)
4
+ paths = uids.map { |uid| Labels.new(Pebbles::Uid.path(uid)) }
5
+ roots = [paths.first]
6
+ paths[1..-1].each { |path|
7
+ roots.each { |root|
8
+ roots << path unless root.parent_of?(path)
9
+ }
10
+ }
11
+ roots.map(&:to_s)
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  module Pebbles
2
2
  class Uid
3
- VERSION = "0.0.9"
3
+ VERSION = "0.0.11"
4
4
  end
5
5
  end
data/spec/labels_spec.rb CHANGED
@@ -44,4 +44,15 @@ describe Pebbles::Uid::Labels do
44
44
  it { subject.valid_with?(/[a-z]/).should == true }
45
45
  end
46
46
 
47
+ context "parent_of?" do
48
+ let(:a) { Pebbles::Uid::Labels.new('a') }
49
+ let(:a_b_c) { Pebbles::Uid::Labels.new('a.b.c') }
50
+ let(:a_b_d) { Pebbles::Uid::Labels.new('a.b.d') }
51
+
52
+ it "works" do
53
+ a.parent_of?(a_b_c).should == true
54
+ a_b_c.parent_of?(a_b_d).should == false
55
+ end
56
+ end
57
+
47
58
  end
data/spec/query_spec.rb CHANGED
@@ -3,7 +3,7 @@ require 'pebbles-uid'
3
3
  describe Pebbles::Uid::Query do
4
4
 
5
5
  context "for a single resource" do
6
- let(:term) { "post:area51$abc" }
6
+ let(:term) { "post.doc:area51$abc" }
7
7
  subject { Pebbles::Uid::Query.new(term, :suffix => '') }
8
8
 
9
9
  its(:term) { should == term }
@@ -11,8 +11,12 @@ describe Pebbles::Uid::Query do
11
11
  its(:for_one?) { should == true }
12
12
  its(:list?) { should == false }
13
13
  its(:collection?) { should == false }
14
- its(:cache_keys) { should eq(['post:area51.*$abc']) }
15
- its(:to_hash) { should eq(:species_0_ => 'post', :path_0_ => 'area51', :oid_ => 'abc') }
14
+ its(:cache_keys) { should eq(['post.doc:area51.*$abc']) }
15
+ its(:to_hash) { should eq(:species_0_ => 'post', :species_1_ => 'doc', :path_0_ => 'area51', :oid_ => 'abc') }
16
+ its(:genus) { should == 'post' }
17
+ its(:species) { should == 'post.doc' }
18
+ its(:path) { should == 'area51' }
19
+ its(:oid) { should == 'abc' }
16
20
 
17
21
  it "handles a wildcard path if realm is given" do
18
22
  query = Pebbles::Uid::Query.new('post:area51.*$abc')
@@ -35,14 +39,17 @@ describe Pebbles::Uid::Query do
35
39
  end
36
40
 
37
41
  context "for a list of resources" do
38
- let(:term) { "post:area51$abc,post:area51$xyz" }
42
+ let(:term) { "post.doc:area51$abc,post.doc:area51$xyz" }
39
43
  subject { Pebbles::Uid::Query.new(term) }
40
44
 
41
45
  its(:for_one?) { should == false }
42
46
  its(:list?) { should == true }
43
47
  its(:collection?) { should == false }
48
+ its(:genus) { should == 'post' }
44
49
 
45
- its(:cache_keys) { should eq(['post:area51.*$abc', 'post:area51.*$xyz']) }
50
+ its(:cache_keys) { should eq(['post.doc:area51.*$abc', 'post.doc:area51.*$xyz']) }
51
+
52
+ it "only accepts one single genus"
46
53
 
47
54
  context "by oid" do
48
55
  subject { Pebbles::Uid::Query.new("post:area51$abc|xyz") }
@@ -0,0 +1,21 @@
1
+ require 'pebbles-uid'
2
+
3
+ describe Pebbles::Uid do
4
+
5
+ specify "single node" do
6
+ Pebbles::Uid.root_paths(['person:a$1']).should eq(['a'])
7
+ end
8
+
9
+ specify "occluded node" do
10
+ Pebbles::Uid.root_paths(['person:a$1', 'person:a.b$2']).should eq(['a'])
11
+ end
12
+
13
+ specify "non-intersecting nodes" do
14
+ Pebbles::Uid.root_paths(['person:a.b$1', 'person:a.c$2']).should eq(['a.b', 'a.c'])
15
+ end
16
+
17
+ specify "weird non-intersecting nodes" do
18
+ Pebbles::Uid.root_paths(['person:a.b.c$1', 'person:a.c.d$2']).should eq(['a.b.c', 'a.c.d'])
19
+ end
20
+
21
+ end
data/spec/uid_spec.rb CHANGED
@@ -12,7 +12,7 @@ describe Pebbles::Uid do
12
12
  end
13
13
  end
14
14
 
15
- describe "extracts elements" do
15
+ describe "extracts" do
16
16
  specify "genus" do
17
17
  Pebbles::Uid.genus(uid).should eq('post')
18
18
  end
@@ -33,8 +33,22 @@ describe Pebbles::Uid do
33
33
  Pebbles::Uid.oid(uid).should eq('1234')
34
34
  end
35
35
 
36
- specify "non-existant oid" do
37
- Pebbles::Uid.oid('post:a.b.c').should be_nil
36
+ describe "when oid is non-existant" do
37
+ it "returns nil oid" do
38
+ Pebbles::Uid.oid('post:a.b.c').should be_nil
39
+ end
40
+ end
41
+
42
+ describe "when oid is empty" do
43
+ it "returns nil oid" do
44
+ Pebbles::Uid.oid('post:a.b.c$').should be_nil
45
+ end
46
+ end
47
+
48
+ describe "when uid is invalid" do
49
+ it "returns nil oid" do
50
+ Pebbles::Uid.oid('1').should be_nil
51
+ end
38
52
  end
39
53
  end
40
54
 
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.9
4
+ version: 0.0.11
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-16 00:00:00.000000000 Z
12
+ date: 2012-11-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70250797645480 !ruby/object:Gem::Requirement
16
+ requirement: &70189435613420 !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: *70250797645480
24
+ version_requirements: *70189435613420
25
25
  description: Handle pebble UIDs conveniently.
26
26
  email:
27
27
  - katrina.owen@gmail.com
@@ -40,6 +40,7 @@ files:
40
40
  - lib/pebbles-uid/labels.rb
41
41
  - lib/pebbles-uid/oid.rb
42
42
  - lib/pebbles-uid/query.rb
43
+ - lib/pebbles-uid/roots.rb
43
44
  - lib/pebbles-uid/version.rb
44
45
  - lib/pebbles-uid/wildcard.rb
45
46
  - lib/pebbles/uid.rb
@@ -48,6 +49,7 @@ files:
48
49
  - spec/labels_spec.rb
49
50
  - spec/oid_spec.rb
50
51
  - spec/query_spec.rb
52
+ - spec/roots_spec.rb
51
53
  - spec/uid_spec.rb
52
54
  - spec/wildcard_spec.rb
53
55
  homepage: ''
@@ -80,5 +82,7 @@ test_files:
80
82
  - spec/labels_spec.rb
81
83
  - spec/oid_spec.rb
82
84
  - spec/query_spec.rb
85
+ - spec/roots_spec.rb
83
86
  - spec/uid_spec.rb
84
87
  - spec/wildcard_spec.rb
88
+ has_rdoc: