pairtree 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/pairtree.rb +7 -0
- data/lib/pairtree/client.rb +18 -0
- data/lib/pairtree/identifier.rb +21 -0
- data/lib/pairtree/obj.rb +5 -0
- data/lib/pairtree/path.rb +11 -0
- data/lib/pairtree/root.rb +57 -0
- data/test/helper.rb +18 -0
- data/test/test_pairtree.rb +91 -0
- metadata +141 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.5.1"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Chris Beer
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= pairtree
|
2
|
+
|
3
|
+
Ruby Pairtre implementation
|
4
|
+
|
5
|
+
== Contributing to pairtree
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Chris Beer. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "pairtree"
|
16
|
+
gem.homepage = "http://github.com/cbeer/pairtree"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Ruby Pairtree implementation}
|
19
|
+
gem.email = "chris@cbeer.info"
|
20
|
+
gem.authors = ["Chris Beer"]
|
21
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
22
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
23
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
24
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
end
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "pairtree #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/lib/pairtree.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Pairtree
|
2
|
+
class Client
|
3
|
+
attr_reader :root
|
4
|
+
attr_reader :prefix
|
5
|
+
def initialize path, args = {}
|
6
|
+
args[:root] ||= {}
|
7
|
+
|
8
|
+
@prefix = open(File.join(path, 'pairtree_prefix')).read.strip if File.exists? File.join(path, 'pairtree_prefix')
|
9
|
+
args[:root][:prefix] ||= @prefix
|
10
|
+
|
11
|
+
@version = nil
|
12
|
+
|
13
|
+
args[:root][:version] ||= @version
|
14
|
+
|
15
|
+
@root = Pairtree::Root.new File.join(path, 'pairtree_root'), args[:root]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Pairtree
|
2
|
+
class Identifier
|
3
|
+
ENCODE_REGEX = Regexp.compile("[\"*+,<=>?\\\\^|]|[^\x21-\x7e]", nil, 'u')
|
4
|
+
DECODE_REGEX = Regexp.compile("\\^(..)", nil, 'u')
|
5
|
+
def self.encode id
|
6
|
+
id.gsub(ENCODE_REGEX) { |c| char2hex(c) }.tr('/:.', '=+,')
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.decode id
|
10
|
+
id.tr('=+,', '/:.').gsub(DECODE_REGEX) { |h| hex2char(h) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.char2hex c
|
14
|
+
c.unpack('H*')[0].scan(/../).map { |x| "^#{x}"}
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.hex2char h
|
18
|
+
'' << h.delete('^').hex
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/pairtree/obj.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'find'
|
2
|
+
require 'fileutils'
|
3
|
+
module Pairtree
|
4
|
+
class Root
|
5
|
+
SHORTY_LENGTH = 2
|
6
|
+
|
7
|
+
attr_reader :root
|
8
|
+
def initialize root, args = {}
|
9
|
+
@root = root
|
10
|
+
|
11
|
+
@shorty_length = args.delete(:shorty_length) || SHORTY_LENGTH
|
12
|
+
@prefix = args.delete(:prefix) || ''
|
13
|
+
|
14
|
+
@options = args
|
15
|
+
end
|
16
|
+
|
17
|
+
def list
|
18
|
+
objects = []
|
19
|
+
return [] unless pairtree_root? @root
|
20
|
+
|
21
|
+
Dir.chdir(@root) do
|
22
|
+
Find.find(*Dir.entries('.').reject { |x| x =~ /^\./ }) do |path|
|
23
|
+
if File.directory? path
|
24
|
+
Find.prune if File.basename(path).length > @shorty_length
|
25
|
+
objects << path if Dir.entries(path).any? { |f| f.length > @shorty_length or File.file? File.join(path, f) }
|
26
|
+
next
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
objects.map { |x| @prefix + Pairtree::Path.path_to_id(x) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def mk id
|
35
|
+
id.sub! @prefix, ''
|
36
|
+
path = File.join(@root, Pairtree::Path.id_to_path(id))
|
37
|
+
FileUtils.mkdir_p path
|
38
|
+
Pairtree::Obj.new path
|
39
|
+
end
|
40
|
+
|
41
|
+
def get id
|
42
|
+
id.sub! @prefix, ''
|
43
|
+
path = File.join(@root, Pairtree::Path.id_to_path(id))
|
44
|
+
Pairtree::Obj.new path if File.directory? path
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def pairtree_root
|
50
|
+
Dir.new @root
|
51
|
+
end
|
52
|
+
|
53
|
+
def pairtree_root? path = @root
|
54
|
+
File.directory? path
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'pairtree'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestPairtree < Test::Unit::TestCase
|
4
|
+
def roundtrip s
|
5
|
+
en = Pairtree::Identifier.encode s
|
6
|
+
de = Pairtree::Identifier.decode en
|
7
|
+
assert_equal(s,de)
|
8
|
+
end
|
9
|
+
def test_a
|
10
|
+
assert_equal(Pairtree::Identifier.encode('a'), 'a')
|
11
|
+
assert_equal(Pairtree::Path.id_to_path('a'), 'a')
|
12
|
+
self.roundtrip('a')
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_ab
|
16
|
+
assert_equal(Pairtree::Identifier.encode('ab'), 'ab')
|
17
|
+
assert_equal(Pairtree::Path.id_to_path('ab'), 'ab')
|
18
|
+
self.roundtrip('ab')
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_abc
|
22
|
+
assert_equal(Pairtree::Identifier.encode('abc'), 'abc')
|
23
|
+
assert_equal(Pairtree::Path.id_to_path('abc'), 'ab/c')
|
24
|
+
self.roundtrip('abc')
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_abcd
|
28
|
+
assert_equal(Pairtree::Identifier.encode('abcd'), 'abcd')
|
29
|
+
assert_equal(Pairtree::Path.id_to_path('abcd'), 'ab/cd')
|
30
|
+
self.roundtrip('abcd')
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_space
|
34
|
+
assert_equal(Pairtree::Identifier.encode('hello world'), 'hello^20world')
|
35
|
+
assert_equal(Pairtree::Path.id_to_path('hello world'), 'he/ll/o^/20/wo/rl/d')
|
36
|
+
self.roundtrip('hello world')
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_slash
|
40
|
+
assert_equal(Pairtree::Identifier.encode('/'), '=')
|
41
|
+
self.roundtrip('/')
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_urn
|
45
|
+
assert_equal(Pairtree::Identifier.encode('http://n2t.info/urn:nbn:se:kb:repos-1'), 'http+==n2t,info=urn+nbn+se+kb+repos-1')
|
46
|
+
self.roundtrip('http://n2t.info/urn:nbn:se:kb:repos-1')
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_wtf
|
50
|
+
assert_equal(Pairtree::Identifier.encode('what-the-*@?#!^!?'), "what-the-^2a@^3f#!^5e!^3f")
|
51
|
+
self.roundtrip('what-the-*@?#!^!?')
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_weird
|
55
|
+
assert_equal(Pairtree::Identifier.encode('\\"*+,<=>?^|'), "^5c^22^2a^2b^2c^3c^3d^3e^3f^5e^7c")
|
56
|
+
assert_equal(Pairtree::Path.id_to_path('\\"*+,<=>?^|'), "^5/c^/22/^2/a^/2b/^2/c^/3c/^3/d^/3e/^3/f^/5e/^7/c")
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_hardcore_unicode
|
60
|
+
self.roundtrip(" 1. Euro Symbol: €.
|
61
|
+
2. Greek: Μπορώ να φάω σπασμένα γυαλιά χωρίς να πάθω τίποτα.
|
62
|
+
3. Íslenska / Icelandic: Ég get etið gler án þess að meiða mig.
|
63
|
+
4. Polish: Mogę jeść szkło, i mi nie szkodzi.
|
64
|
+
5. Romanian: Pot să mănânc sticlă și ea nu mă rănește.
|
65
|
+
6. Ukrainian: Я можу їсти шкло, й воно мені не пошкодить.
|
66
|
+
7. Armenian: Կրնամ ապակի ուտել և ինծի անհանգիստ չըներ։
|
67
|
+
8. Georgian: მინას ვჭამ და არა მტკივა.
|
68
|
+
9. Hindi: मैं काँच खा सकता हूँ, मुझे उस से कोई पीडा नहीं होती.
|
69
|
+
10. Hebrew(2): אני יכול לאכול זכוכית וזה לא מזיק לי.
|
70
|
+
11. Yiddish(2): איך קען עסן גלאָז און עס טוט מיר נישט װײ.
|
71
|
+
12. Arabic(2): أنا قادر على أكل الزجاج و هذا لا يؤلمني.
|
72
|
+
13. Japanese: 私はガラスを食べられます。それは私を傷つけません。
|
73
|
+
14. Thai: ฉันกินกระจกได้ แต่มันไม่ทำให้ฉันเจ็บ ")
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_french
|
77
|
+
self.roundtrip 'Années de Pèlerinage'
|
78
|
+
self.roundtrip "Années de Pèlerinage (Years of Pilgrimage) (S.160, S.161,\n\
|
79
|
+
S.163) is a set of three suites by Franz Liszt for solo piano. Liszt's\n\
|
80
|
+
complete musical style is evident in this masterwork, which ranges from\n\
|
81
|
+
virtuosic fireworks to sincerely moving emotional statements. His musical\n\
|
82
|
+
maturity can be seen evolving through his experience and travel. The\n\
|
83
|
+
third volume is especially notable as an example of his later style: it\n\
|
84
|
+
was composed well after the first two volumes and often displays less\n\
|
85
|
+
showy virtuosity and more harmonic experimentation."
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pairtree
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 0.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Chris Beer
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-23 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
name: shoulda
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
requirement: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
name: bundler
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 23
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 0
|
48
|
+
- 0
|
49
|
+
version: 1.0.0
|
50
|
+
requirement: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
type: :development
|
53
|
+
prerelease: false
|
54
|
+
name: jeweler
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 1
|
61
|
+
segments:
|
62
|
+
- 1
|
63
|
+
- 5
|
64
|
+
- 1
|
65
|
+
version: 1.5.1
|
66
|
+
requirement: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
name: rcov
|
71
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirement: *id004
|
81
|
+
description:
|
82
|
+
email: chris@cbeer.info
|
83
|
+
executables: []
|
84
|
+
|
85
|
+
extensions: []
|
86
|
+
|
87
|
+
extra_rdoc_files:
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.rdoc
|
90
|
+
files:
|
91
|
+
- .document
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.rdoc
|
95
|
+
- Rakefile
|
96
|
+
- VERSION
|
97
|
+
- lib/pairtree.rb
|
98
|
+
- lib/pairtree/client.rb
|
99
|
+
- lib/pairtree/identifier.rb
|
100
|
+
- lib/pairtree/obj.rb
|
101
|
+
- lib/pairtree/path.rb
|
102
|
+
- lib/pairtree/root.rb
|
103
|
+
- test/helper.rb
|
104
|
+
- test/test_pairtree.rb
|
105
|
+
has_rdoc: true
|
106
|
+
homepage: http://github.com/cbeer/pairtree
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
hash: 3
|
120
|
+
segments:
|
121
|
+
- 0
|
122
|
+
version: "0"
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
hash: 3
|
129
|
+
segments:
|
130
|
+
- 0
|
131
|
+
version: "0"
|
132
|
+
requirements: []
|
133
|
+
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 1.3.7
|
136
|
+
signing_key:
|
137
|
+
specification_version: 3
|
138
|
+
summary: Ruby Pairtree implementation
|
139
|
+
test_files:
|
140
|
+
- test/helper.rb
|
141
|
+
- test/test_pairtree.rb
|