pebble_path 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +46 -0
- data/Rakefile +2 -0
- data/lib/pebble_path/positions.rb +82 -0
- data/lib/pebble_path.rb +50 -0
- data/pebble_path.gemspec +19 -0
- data/spec/pebble_path_spec.rb +98 -0
- data/spec/positions_spec.rb +45 -0
- metadata +80 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Katrina Owen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# PebblePath
|
2
|
+
|
3
|
+
Provides searchable, parseable pebbles-compliant UID paths, e.g. (such as `a.b.*`) for Active Record models.
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
Requires ActiveModel. The target class (the one that will contain the `path` property) needs to have fields in the DB for:
|
8
|
+
|
9
|
+
```
|
10
|
+
label_0
|
11
|
+
label_1
|
12
|
+
label_2
|
13
|
+
label_3
|
14
|
+
label_4
|
15
|
+
label_5
|
16
|
+
label_6
|
17
|
+
label_7
|
18
|
+
label_8
|
19
|
+
label_9
|
20
|
+
```
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Add this line to your application's Gemfile:
|
25
|
+
|
26
|
+
gem 'pebble_path'
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
|
30
|
+
$ bundle
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
$ gem install pebble_path
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
TODO: Write usage instructions here
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'pebblebed'
|
2
|
+
|
3
|
+
module PebblePath
|
4
|
+
class Positions
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
MAX_DEPTH = 10
|
8
|
+
|
9
|
+
attr_reader :labels, :all
|
10
|
+
def initialize(positions)
|
11
|
+
@all = resolve positions
|
12
|
+
@labels = all.compact
|
13
|
+
end
|
14
|
+
|
15
|
+
def each
|
16
|
+
labels.each {|label| yield label}
|
17
|
+
end
|
18
|
+
|
19
|
+
def [](index)
|
20
|
+
labels[index]
|
21
|
+
end
|
22
|
+
|
23
|
+
def []=(index, value)
|
24
|
+
labels[index] = value
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
labels.join('.')
|
29
|
+
end
|
30
|
+
|
31
|
+
def resolve(positions)
|
32
|
+
positions = positions.split('.') if positions.is_a?(String)
|
33
|
+
positions = truncate_invalid(positions)
|
34
|
+
(0...MAX_DEPTH).map do |i|
|
35
|
+
positions[i]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def truncate_invalid(positions)
|
40
|
+
labels = []
|
41
|
+
(0...MAX_DEPTH).each do |i|
|
42
|
+
break if positions[i].nil?
|
43
|
+
labels << positions[i]
|
44
|
+
end
|
45
|
+
labels
|
46
|
+
end
|
47
|
+
|
48
|
+
class << self
|
49
|
+
|
50
|
+
def detect(path)
|
51
|
+
unless Pebblebed::Uid.valid_path?(path)
|
52
|
+
raise ArgumentError.new("Wildcards terminate the path. Invalid path: #{path}")
|
53
|
+
end
|
54
|
+
|
55
|
+
labels = path.split('.')
|
56
|
+
# In a Pebblebed::Uid::WildcardPath, anything after '^' is optional.
|
57
|
+
optional_part = false
|
58
|
+
|
59
|
+
labels.map! do |label|
|
60
|
+
if label =~ /^\^/
|
61
|
+
label.gsub!(/^\^/, '')
|
62
|
+
optional_part = true
|
63
|
+
end
|
64
|
+
|
65
|
+
result = label.include?('|') ? label.split('|') : label
|
66
|
+
result = [label, nil].flatten if optional_part
|
67
|
+
result
|
68
|
+
end
|
69
|
+
|
70
|
+
result = {}
|
71
|
+
(0...MAX_DEPTH).map do |index|
|
72
|
+
break if labels[index] == '*'
|
73
|
+
result[:"label_#{index}"] = labels[index]
|
74
|
+
break if labels[index].nil?
|
75
|
+
end
|
76
|
+
result
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
data/lib/pebble_path.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'pebble_path/positions'
|
2
|
+
|
3
|
+
module PebblePath
|
4
|
+
|
5
|
+
def self.max_depth
|
6
|
+
Positions::MAX_DEPTH
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.detect(paths)
|
10
|
+
Positions.detect(paths)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.included(base)
|
14
|
+
base.class_eval do
|
15
|
+
scope :by_path, lambda { |path|
|
16
|
+
where Positions.detect(path)
|
17
|
+
}
|
18
|
+
|
19
|
+
validates_presence_of :label_0
|
20
|
+
validate :must_be_valid_uid_path
|
21
|
+
|
22
|
+
after_initialize :initialize_path
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
attr_reader :path
|
27
|
+
def path=(positions)
|
28
|
+
@path = Positions.new(positions)
|
29
|
+
|
30
|
+
path.all.each_with_index do |value, i|
|
31
|
+
send(:"label_#{i}=", value)
|
32
|
+
end
|
33
|
+
path
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize_path
|
37
|
+
positions = []
|
38
|
+
[:label_0, :label_1, :label_2, :label_3, :label_4, :label_5, :label_6, :label_7, :label_8, :label_9].each do |attribute|
|
39
|
+
positions << send(attribute)
|
40
|
+
end
|
41
|
+
self.path = positions
|
42
|
+
end
|
43
|
+
|
44
|
+
def must_be_valid_uid_path
|
45
|
+
unless Pebblebed::Uid.valid_path?(self.path.to_s)
|
46
|
+
errors.add(:base, "Location path '#{self.path.to_s}' is invalid.")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
data/pebble_path.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.authors = ["Katrina Owen"]
|
5
|
+
gem.email = ["katrina.owen@gmail.com"]
|
6
|
+
gem.description = %q{Provide pebble-compliant paths for ActiveRecord models.}
|
7
|
+
gem.summary = %q{Provides searchable, parseable pebble-compliant UID paths, e.g. (such as a.b.*) for Active Record models.}
|
8
|
+
gem.homepage = ""
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "pebble_path"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = "0.0.1"
|
16
|
+
|
17
|
+
gem.add_development_dependency 'rspec'
|
18
|
+
gem.add_dependency 'pebblebed', '>=0.0.15'
|
19
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'pebble_path'
|
2
|
+
|
3
|
+
class Thing
|
4
|
+
def self.scope(*attrs)
|
5
|
+
end
|
6
|
+
def self.after_initialize(*attrs)
|
7
|
+
end
|
8
|
+
def self.validates_presence_of(*attrs)
|
9
|
+
end
|
10
|
+
def self.validate(*attrs)
|
11
|
+
end
|
12
|
+
|
13
|
+
(0..9).each do |i|
|
14
|
+
attr_accessor "label_#{i}".to_sym
|
15
|
+
end
|
16
|
+
|
17
|
+
include PebblePath
|
18
|
+
end
|
19
|
+
|
20
|
+
describe PebblePath do
|
21
|
+
subject do
|
22
|
+
thing = Thing.new
|
23
|
+
thing.path = "a.b.c.d.e"
|
24
|
+
thing
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "path" do
|
28
|
+
|
29
|
+
it "accepts an array" do
|
30
|
+
subject.path = %w(a b c)
|
31
|
+
"#{subject.path}".should eq("a.b.c")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "accepts a string" do
|
35
|
+
subject.path = "a.b.c"
|
36
|
+
"#{subject.path}".should eq("a.b.c")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "doesn't do weird stuff" do
|
40
|
+
subject.path = [nil, 'a']
|
41
|
+
"#{subject.path}".should eq("")
|
42
|
+
end
|
43
|
+
|
44
|
+
specify { subject.path.map{|label| label + label}.should eq %w(aa bb cc dd ee) }
|
45
|
+
|
46
|
+
specify { subject.path[1].should eq('b') }
|
47
|
+
|
48
|
+
specify do
|
49
|
+
subject.path[2] = 'x'
|
50
|
+
"#{subject.path}".should eq("a.b.x.d.e")
|
51
|
+
end
|
52
|
+
|
53
|
+
its(:label_0) { should eq('a') }
|
54
|
+
its(:label_1) { should eq('b') }
|
55
|
+
its(:label_2) { should eq('c') }
|
56
|
+
its(:label_3) { should eq('d') }
|
57
|
+
its(:label_4) { should eq('e') }
|
58
|
+
|
59
|
+
[:label_5, :label_6, :label_7, :label_8, :label_9].each do |attribute|
|
60
|
+
it "hasn't filled #{attribute}" do
|
61
|
+
subject.send(attribute).should be_nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "overwrites labels" do
|
67
|
+
before(:each) do
|
68
|
+
subject.path = 'a.b.c'
|
69
|
+
end
|
70
|
+
|
71
|
+
its(:label_0) { should eq('a') }
|
72
|
+
its(:label_1) { should eq('b') }
|
73
|
+
its(:label_2) { should eq('c') }
|
74
|
+
|
75
|
+
[:label_3, :label_4, :label_5, :label_6, :label_7, :label_8, :label_9].each do |attribute|
|
76
|
+
it "overwrites #{attribute}" do
|
77
|
+
subject.send(attribute).should be_nil
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it "initializes path from label" do
|
83
|
+
subject.path = []
|
84
|
+
subject.label_0 = 'a'
|
85
|
+
subject.label_1 = 'b'
|
86
|
+
subject.label_2 = 'c'
|
87
|
+
subject.path.to_s.should eq('')
|
88
|
+
subject.initialize_path
|
89
|
+
subject.path.to_s.should eq('a.b.c')
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "no stray labels" do
|
93
|
+
it "stops setting labels when it reaches a nil" do
|
94
|
+
subject.path = ['a', 'b', nil, 'd']
|
95
|
+
subject.path.to_s.should eq('a.b')
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'pebble_path/positions'
|
2
|
+
|
3
|
+
describe PebblePath::Positions do
|
4
|
+
subject { PebblePath::Positions }
|
5
|
+
|
6
|
+
let(:full_path) do
|
7
|
+
{
|
8
|
+
:label_0 => "a",
|
9
|
+
:label_1 => "b",
|
10
|
+
:label_2 => "c",
|
11
|
+
:label_3 => "d",
|
12
|
+
:label_4 => "e",
|
13
|
+
:label_5 => "f",
|
14
|
+
:label_6 => "g",
|
15
|
+
:label_7 => "h",
|
16
|
+
:label_8 => "i",
|
17
|
+
:label_9 => "j",
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#detect" do
|
22
|
+
|
23
|
+
specify do
|
24
|
+
subject.detect("a.b.c.d.e.f.g.h.i.j").should eq(full_path)
|
25
|
+
end
|
26
|
+
|
27
|
+
specify do
|
28
|
+
subject.detect("a.b.c").should eq({:label_0=>"a", :label_1=>"b", :label_2=>"c", :label_3=>nil})
|
29
|
+
end
|
30
|
+
|
31
|
+
specify do
|
32
|
+
subject.detect("a.b.*").should eq({:label_0=>"a", :label_1=>"b"})
|
33
|
+
end
|
34
|
+
|
35
|
+
specify do
|
36
|
+
subject.detect("a.^b.c").should eq({:label_0=>"a", :label_1=>["b", nil], :label_2=>["c", nil], :label_3=>nil})
|
37
|
+
end
|
38
|
+
|
39
|
+
specify do
|
40
|
+
subject.detect("a.b|c.d").should eq({:label_0=>"a", :label_1=>["b", "c"], :label_2=>"d", :label_3=>nil})
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pebble_path
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Katrina Owen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-14 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70303532184640 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70303532184640
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: pebblebed
|
27
|
+
requirement: &70303532183560 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.0.15
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70303532183560
|
36
|
+
description: Provide pebble-compliant paths for ActiveRecord models.
|
37
|
+
email:
|
38
|
+
- katrina.owen@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- LICENSE
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- lib/pebble_path.rb
|
49
|
+
- lib/pebble_path/positions.rb
|
50
|
+
- pebble_path.gemspec
|
51
|
+
- spec/pebble_path_spec.rb
|
52
|
+
- spec/positions_spec.rb
|
53
|
+
homepage: ''
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.10
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Provides searchable, parseable pebble-compliant UID paths, e.g. (such as
|
77
|
+
a.b.*) for Active Record models.
|
78
|
+
test_files:
|
79
|
+
- spec/pebble_path_spec.rb
|
80
|
+
- spec/positions_spec.rb
|