namaste 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "i18n"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "shoulda", ">= 0"
12
+ gem "bundler", "~> 1.0.0"
13
+ gem "jeweler", "~> 1.5.1"
14
+ gem "rcov", ">= 0"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ i18n (0.4.1)
6
+ jeweler (1.5.1)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ shoulda (2.11.3)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.0.0)
19
+ i18n
20
+ jeweler (~> 1.5.1)
21
+ rcov
22
+ shoulda
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
+ = namaste
2
+
3
+ A ruby client implementation of the [[Namaste|http://www.cdlib.org/inside/diglib/namaste/namastespec.html]] specification for directory description with filename-based tags.
4
+
5
+ == Contributing to namaste
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,47 @@
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.name = "namaste"
15
+ gem.homepage = "http://github.com/cbeer/namaste"
16
+ gem.license = "MIT"
17
+ gem.summary = "A ruby client implementation of the Namaste specification for directory description with filename-based tags."
18
+ gem.email = "chris@cbeer.info"
19
+ gem.authors = ["Chris Beer"]
20
+ end
21
+ Jeweler::RubygemsDotOrgTasks.new
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ task :default => :test
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "namaste #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/namaste ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/ruby
2
+ require 'optparse'
3
+
4
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
5
+ require 'namaste'
6
+
7
+ options = {}
8
+
9
+ optparse = OptionParser.new do |opts|
10
+ opts.banner = "Usage: namaste [options] [dir]"
11
+
12
+ options[:verbose] = false
13
+ opts.on( '-v', '--verbose', 'Output more information') do
14
+ options[:verbose] = true
15
+ end
16
+
17
+ options[:filter] = nil
18
+ opts.on( '-f', '--filter FILTER', 'Filter specific Dublin Kernel tags') do |v|
19
+ options[:filter] = v
20
+ end
21
+
22
+ # This displays the help screen, all programs are
23
+ # assumed to have this option.
24
+ opts.on( '-h', '--help', 'Display this screen' ) do
25
+ puts opts
26
+ exit
27
+ end
28
+ end
29
+
30
+ optparse.parse!
31
+
32
+ options[:dir] = ARGV.shift
33
+ options[:dir] ||= Dir.pwd
34
+
35
+ dir = Namaste::Dir.new options[:dir]
36
+
37
+ args = {}
38
+ args[:filter] = options[:filter] if options[:filter]
39
+
40
+ print dir.namaste(args).inspect
41
+
42
+ print "\n"
data/lib/namaste.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'i18n'
2
+ module Namaste
3
+ DUBLIN_KERNEL = { :type => 0, :who => 1, :what => 2, :when => 3, :where => 4 }
4
+ PATTERN = Hash[*Namaste::DUBLIN_KERNEL.map { |k, v| [k, Regexp.new("^#{v}=.*")]}.flatten]
5
+ PATTERN_CORE = /^\d=.*/
6
+ PATTERN_EXTENDED = /=.*/
7
+ module Mixin
8
+ def self.included base
9
+ Namaste::DUBLIN_KERNEL.each do |k,v|
10
+ base.class_eval do
11
+ define_method(k.to_s) do |*args|
12
+ namaste(:filter => k)
13
+ end
14
+
15
+ define_method(k.to_s+'=') do |*args|
16
+ set_namaste v, *args
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ def namaste_tags args = {}
23
+ tags = []
24
+ rgx = nil
25
+
26
+ if args[:filter]
27
+ if Namaste::PATTERN.key? args[:filter].to_sym
28
+ rgx = Namaste::PATTERN[args[:filter].to_sym]
29
+ else
30
+ rgx = Regexp.new("^#{args[:filter]}=.*")
31
+ end
32
+ else
33
+ rgx = Namaste::PATTERN_CORE
34
+ rgx = Regexp(rgx, Namaste::PATTERN_EXTENDED) if args[:extended]
35
+ end
36
+
37
+ rgx = args[:regex] if args[:regex]
38
+
39
+ tags |= self.select { |x| x =~ rgx }
40
+ end
41
+
42
+ def namaste args = {}
43
+ namaste_tags(args).map { |x| get_namaste x }
44
+ end
45
+
46
+ def dirtype
47
+ namaste(:filter => :type).map do |nam|
48
+ matches = /([^_]+)_(\d+)\.(\d+)/.match(nam[:value])
49
+ { :type => nam[:value], :name => matches[1], :major => matches[2], :minor => matches[3] } if matches
50
+ end
51
+ end
52
+
53
+ private
54
+ def set_namaste tag, value
55
+ File.open(File.join(self.path, make_namaste(tag, value)), 'w') do |f|
56
+ f.write(value)
57
+ end
58
+ end
59
+
60
+ def get_namaste namaste_tag
61
+ n = {}
62
+ name, tvalue = namaste_tag.split '='
63
+ n[:file] = namaste_tag
64
+ n[:name] = name
65
+ n[:value] = open(File.join(self.path, namaste_tag)).read.strip
66
+ n
67
+ end
68
+
69
+ def make_namaste tag, value
70
+ value = I18n.transliterate value
71
+ value.gsub!(/[^A-Za-z0-9\-\._]+/, '_')
72
+ value.gsub!(/_{2,}/, '_')
73
+ value.gsub!(/^_|_$/, '_')
74
+ encoded_value = value.downcase
75
+
76
+ n = "%s=%s" % [tag, encoded_value]
77
+ n = n.slice(0...252) + "..." if n.length > 255
78
+ n
79
+ end
80
+
81
+ end
82
+
83
+ class Dir < ::Dir
84
+ include Namaste::Mixin
85
+ end
86
+ 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 'namaste'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+
3
+ class TestNamasteSurrogate
4
+ include Namaste::Mixin
5
+
6
+ def test_make_namaste tag, value
7
+ make_namaste tag, value
8
+ end
9
+ end
10
+ class TestNamaste < Test::Unit::TestCase
11
+ def test_make_simple
12
+ t = TestNamasteSurrogate.new
13
+ assert_equal(t.test_make_namaste(0, 'abcdef'), '0=abcdef')
14
+ end
15
+
16
+ def test_make_dflat
17
+ t = TestNamasteSurrogate.new
18
+ assert_equal(t.test_make_namaste(0, 'Dflat/0.19'), '0=dflat_0.19')
19
+ end
20
+
21
+ def test_make_complex
22
+ t = TestNamasteSurrogate.new
23
+ assert_equal(t.test_make_namaste(0, 'rQ@f2!éüAsd!'), '0=rq_f2_euasd_')
24
+ end
25
+
26
+ def test_make_extended
27
+ t = TestNamasteSurrogate.new
28
+ assert_equal(t.test_make_namaste('x_123', 'extended'), 'x_123=extended')
29
+ end
30
+
31
+ def test_truncate
32
+ t = TestNamasteSurrogate.new
33
+ assert_equal(t.test_make_namaste(0, 'lfvtshfasfogfzjgqokuwicivlnyluqlgfcsfmhtdbmrizvzqkiyaxqtlclkgxpgkmxtwwylepsorbdnddgrdgzpcyojqbwuxkqkfzmfbkxrfpaaymgygbpjgqxyklkfblqekgtrpdxvjsmodvkrlwcfrqswdknngervsjivehotqeiowigfpwymunrccgjhakdwpugwwtpqcpkwqvwlhcqccwqovlwaldwfuoalscdvzccgnpooedbrnttzmno'), '0=lfvtshfasfogfzjgqokuwicivlnyluqlgfcsfmhtdbmrizvzqkiyaxqtlclkgxpgkmxtwwylepsorbdnddgrdgzpcyojqbwuxkqkfzmfbkxrfpaaymgygbpjgqxyklkfblqekgtrpdxvjsmodvkrlwcfrqswdknngervsjivehotqeiowigfpwymunrccgjhakdwpugwwtpqcpkwqvwlhcqccwqovlwaldwfuoalscdvzccgnpooedbrnt...')
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: namaste
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Chris Beer
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-26 00:00:00 -05:00
19
+ default_executable: namaste
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ prerelease: false
24
+ name: i18n
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: shoulda
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ requirement: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ type: :development
51
+ prerelease: false
52
+ name: bundler
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 23
59
+ segments:
60
+ - 1
61
+ - 0
62
+ - 0
63
+ version: 1.0.0
64
+ requirement: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ type: :development
67
+ prerelease: false
68
+ name: jeweler
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 1
75
+ segments:
76
+ - 1
77
+ - 5
78
+ - 1
79
+ version: 1.5.1
80
+ requirement: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ type: :development
83
+ prerelease: false
84
+ name: rcov
85
+ version_requirements: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirement: *id005
95
+ description:
96
+ email: chris@cbeer.info
97
+ executables:
98
+ - namaste
99
+ extensions: []
100
+
101
+ extra_rdoc_files:
102
+ - LICENSE.txt
103
+ - README.rdoc
104
+ files:
105
+ - .document
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE.txt
109
+ - README.rdoc
110
+ - Rakefile
111
+ - VERSION
112
+ - bin/namaste
113
+ - lib/namaste.rb
114
+ - test/helper.rb
115
+ - test/test_namaste.rb
116
+ has_rdoc: true
117
+ homepage: http://github.com/cbeer/namaste
118
+ licenses:
119
+ - MIT
120
+ post_install_message:
121
+ rdoc_options: []
122
+
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ hash: 3
131
+ segments:
132
+ - 0
133
+ version: "0"
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ hash: 3
140
+ segments:
141
+ - 0
142
+ version: "0"
143
+ requirements: []
144
+
145
+ rubyforge_project:
146
+ rubygems_version: 1.3.7
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: A ruby client implementation of the Namaste specification for directory description with filename-based tags.
150
+ test_files:
151
+ - test/helper.rb
152
+ - test/test_namaste.rb