pebbles-uid 0.0.11 → 0.0.12

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.
@@ -2,6 +2,10 @@ module Pebbles
2
2
  class Uid
3
3
  class << self
4
4
 
5
+ def roots(uids)
6
+ Pebbles::Uid::Roots.new(uids).unique
7
+ end
8
+
5
9
  def parse(s)
6
10
  /^(?<species>.*):(?<path>[^\$]*)\$?(?<oid>.*)$/ =~ s
7
11
  [species, path, oid && oid.empty? ? nil : oid]
@@ -25,6 +25,7 @@ module Pebbles
25
25
  end
26
26
 
27
27
  def parent_of?(other)
28
+ return false if values == other.values
28
29
  parent = true
29
30
  values.each_with_index do |value, i|
30
31
  parent = false unless value == other.values[i]
@@ -32,6 +33,16 @@ module Pebbles
32
33
  parent
33
34
  end
34
35
 
36
+ def child_of?(other)
37
+ child = true
38
+ other.values.each_with_index do |label, i|
39
+ if label != values[i]
40
+ child = false
41
+ end
42
+ end
43
+ child && other.size != size
44
+ end
45
+
35
46
  def ambiguous?
36
47
  value == '*' || values.empty? || wildcard?
37
48
  end
@@ -1,14 +1,27 @@
1
1
  module Pebbles
2
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)
3
+ class Roots
4
+
5
+ attr_reader :paths
6
+ def initialize(uids)
7
+ @paths = {}
8
+ uids.each { |uid|
9
+ @paths[uid] = Labels.new(Pebbles::Uid.path(uid))
9
10
  }
10
- }
11
- roots.map(&:to_s)
11
+ end
12
+
13
+ def unique
14
+ unless @unique
15
+ @unique = []
16
+ paths.each do |uid, path|
17
+ unless paths.any? { |_, other| path.child_of?(other) }
18
+ @unique << uid
19
+ end
20
+ end
21
+ end
22
+ @unique
23
+ end
24
+
12
25
  end
13
26
  end
14
27
  end
@@ -1,5 +1,5 @@
1
1
  module Pebbles
2
2
  class Uid
3
- VERSION = "0.0.11"
3
+ VERSION = "0.0.12"
4
4
  end
5
5
  end
data/spec/labels_spec.rb CHANGED
@@ -49,10 +49,21 @@ describe Pebbles::Uid::Labels do
49
49
  let(:a_b_c) { Pebbles::Uid::Labels.new('a.b.c') }
50
50
  let(:a_b_d) { Pebbles::Uid::Labels.new('a.b.d') }
51
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
52
+ specify { a.parent_of?(a).should == false }
53
+ specify { a_b_c.parent_of?(a).should == false }
54
+ specify { a.parent_of?(a_b_c).should == true }
55
+ specify { a_b_c.parent_of?(a_b_d).should == false }
56
+ end
57
+
58
+ context "#child_of?" do
59
+ let(:a) { Pebbles::Uid::Labels.new('a') }
60
+ let(:b) { Pebbles::Uid::Labels.new('b') }
61
+ let(:a_b) { Pebbles::Uid::Labels.new('a.b') }
62
+
63
+ specify { a.child_of?(a).should == false }
64
+ specify { a_b.child_of?(a).should == true }
65
+ specify { a.child_of?(b).should == false }
66
+ specify { a.child_of?(a_b).should == false }
56
67
  end
57
68
 
58
69
  end
data/spec/roots_spec.rb CHANGED
@@ -1,21 +1,33 @@
1
1
  require 'pebbles-uid'
2
2
 
3
- describe Pebbles::Uid do
3
+ describe Pebbles::Uid::Roots do
4
4
 
5
- specify "single node" do
6
- Pebbles::Uid.root_paths(['person:a$1']).should eq(['a'])
5
+ specify "empty list is empty :)" do
6
+ Pebbles::Uid::Roots.new([]).unique.should eq([])
7
7
  end
8
8
 
9
- specify "occluded node" do
10
- Pebbles::Uid.root_paths(['person:a$1', 'person:a.b$2']).should eq(['a'])
9
+ specify "with one, has a single root" do
10
+ Pebbles::Uid::Roots.new(['boy:a$1']).unique.should eq(['boy:a$1'])
11
11
  end
12
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'])
13
+ specify "does not keep duplicates" do
14
+ Pebbles::Uid::Roots.new(['girl:a$1', 'girl:a$1']).unique.should eq(['girl:a$1'])
15
15
  end
16
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'])
17
+ specify "with two, returns just the parent" do
18
+ Pebbles::Uid::Roots.new(['kid:a$1', 'kid:a.b$2']).unique.should eq(['kid:a$1'])
19
+ end
20
+
21
+ specify "with two, returns just the parent, regardless of sort order" do
22
+ Pebbles::Uid::Roots.new(['baby:a.b$1', 'baby:a$2']).unique.should eq(['baby:a$2'])
23
+ end
24
+
25
+ specify "with non-intersecting nodes, returns both" do
26
+ Pebbles::Uid::Roots.new(['goat:a.b$1', 'goat:a.c$2']).unique.should eq(['goat:a.b$1', 'goat:a.c$2'])
27
+ end
28
+
29
+ specify "with similar, but non-intersecting nodes, returns both" do
30
+ Pebbles::Uid::Roots.new(['pig:a.b.c$1', 'pig:a.c.d$2']).unique.should eq(['pig:a.b.c$1', 'pig:a.c.d$2'])
19
31
  end
20
32
 
21
33
  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.11
4
+ version: 0.0.12
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-11-19 00:00:00.000000000 Z
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70189435613420 !ruby/object:Gem::Requirement
16
+ requirement: &70359179077380 !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: *70189435613420
24
+ version_requirements: *70359179077380
25
25
  description: Handle pebble UIDs conveniently.
26
26
  email:
27
27
  - katrina.owen@gmail.com