ruby-nuggets 0.9.6 → 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README +2 -2
- data/lib/nuggets/array/hashify.rb +5 -0
- data/lib/nuggets/array/hashify_mixin.rb +48 -0
- data/lib/nuggets/array/histogram_mixin.rb +1 -1
- data/lib/nuggets/cli.rb +5 -1
- data/lib/nuggets/hash/unroll_mixin.rb +2 -2
- data/lib/nuggets/midos.rb +14 -167
- data/lib/nuggets/midos/base.rb +81 -0
- data/lib/nuggets/midos/reader.rb +117 -0
- data/lib/nuggets/midos/writer.rb +252 -0
- data/lib/nuggets/rdf/compression.rb +92 -0
- data/lib/nuggets/rdf/prefix.rb +32 -0
- data/lib/nuggets/rdf/turtle.rb +124 -0
- data/lib/nuggets/rdf/turtle/reader.rb +71 -0
- data/lib/nuggets/rdf/uri.rb +38 -0
- data/lib/nuggets/version.rb +1 -1
- data/spec/nuggets/array/hashify_spec.rb +43 -0
- metadata +20 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0085d350ab71ac8d9eb89f589930da057d40769e
|
4
|
+
data.tar.gz: 14ad33894ecf8bfde16551bd44ec760a5f37bf64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1075bd40b0f1e876d6363833bddcb9dc928d7171544ea674d1c1dca21f73ec4160c7e923b5d5cfbe057d402a84c9b395e5755f9858cd4a58eda3b69c089fff0
|
7
|
+
data.tar.gz: 472db2f820fa47e20ba1b9fcfc9518df4ea73f7d080182d0ec084205236b8353cd17fe82041320e4d9d04c5a726d959eef5d1471730fdbc8d43e5f24da819b2b
|
data/README
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
== VERSION
|
4
4
|
|
5
|
-
This documentation refers to ruby-nuggets version 0.9.
|
5
|
+
This documentation refers to ruby-nuggets version 0.9.7
|
6
6
|
|
7
7
|
|
8
8
|
== DESCRIPTION
|
@@ -46,7 +46,7 @@ RubyGem:: http://rubygems.org/gems/ruby-nuggets
|
|
46
46
|
|
47
47
|
== LICENSE AND COPYRIGHT
|
48
48
|
|
49
|
-
Copyright (C) 2007-
|
49
|
+
Copyright (C) 2007-2014 Jens Wille
|
50
50
|
|
51
51
|
ruby-nuggets is free software: you can redistribute it and/or modify it under
|
52
52
|
the terms of the GNU Affero General Public License as published by the Free
|
@@ -0,0 +1,48 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2014 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU Affero General Public License as published by #
|
14
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
+
# option) any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
+
# for more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU Affero General Public License #
|
23
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
module Nuggets
|
29
|
+
class Array
|
30
|
+
module HashifyMixin
|
31
|
+
|
32
|
+
def hashify(value = nil, &block)
|
33
|
+
block ||= lambda { |key| [key, key] }
|
34
|
+
|
35
|
+
hash = case value
|
36
|
+
when ::Hash then value
|
37
|
+
when ::Proc then ::Hash.new(&value)
|
38
|
+
else ::Hash.new(value)
|
39
|
+
end
|
40
|
+
|
41
|
+
each { |key| hash.store(*block[key]) }
|
42
|
+
|
43
|
+
hash
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -141,7 +141,7 @@ module Nuggets
|
|
141
141
|
#
|
142
142
|
# See HistogramItem for further details on the individual arguments.
|
143
143
|
def formatted_histogram(format = :default, indicator = '=')
|
144
|
-
format = FORMATS[format] if FORMATS.
|
144
|
+
format = FORMATS[format] if FORMATS.key?(format)
|
145
145
|
raise ::TypeError, "String expected, got #{format.class}" unless format.is_a?(::String)
|
146
146
|
|
147
147
|
include_percentage = format.include?('%%')
|
data/lib/nuggets/cli.rb
CHANGED
@@ -104,6 +104,10 @@ module Nuggets
|
|
104
104
|
}
|
105
105
|
end
|
106
106
|
|
107
|
+
def run(arguments)
|
108
|
+
raise ::NotImplementedError, 'must be implemented by subclass'
|
109
|
+
end
|
110
|
+
|
107
111
|
def reset(stdin = ::STDIN, stdout = ::STDOUT, stderr = ::STDERR)
|
108
112
|
@stdin, @stdout, @stderr = stdin, stdout, stderr
|
109
113
|
@options, @config = {}, {}
|
@@ -177,7 +181,7 @@ module Nuggets
|
|
177
181
|
|
178
182
|
def merge_config(args = [config, defaults])
|
179
183
|
args.each { |hash| hash && hash.each { |key, value|
|
180
|
-
options[key] = value unless options.
|
184
|
+
options[key] = value unless options.key?(key)
|
181
185
|
} }
|
182
186
|
end
|
183
187
|
|
@@ -59,9 +59,9 @@ module Nuggets
|
|
59
59
|
args = value_keys.dup
|
60
60
|
options = value_keys.last.is_a?(::Hash) ? value_keys.pop : {}
|
61
61
|
|
62
|
-
sort_proc = if options.
|
62
|
+
sort_proc = if options.key?(:sort_by)
|
63
63
|
lambda { sort_by(&options[:sort_by]) }
|
64
|
-
elsif options.
|
64
|
+
elsif options.key?(:sort)
|
65
65
|
options[:sort] == true ? lambda { sort } : lambda { sort(&options[:sort]) }
|
66
66
|
end
|
67
67
|
|
data/lib/nuggets/midos.rb
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
7
7
|
# language. #
|
8
8
|
# #
|
9
|
-
# Copyright (C) 2007-
|
9
|
+
# Copyright (C) 2007-2014 Jens Wille #
|
10
10
|
# #
|
11
11
|
# Authors: #
|
12
12
|
# Jens Wille <jens.wille@gmail.com> #
|
@@ -27,6 +27,10 @@
|
|
27
27
|
###############################################################################
|
28
28
|
#++
|
29
29
|
|
30
|
+
require 'nuggets/midos/base'
|
31
|
+
require 'nuggets/midos/reader'
|
32
|
+
require 'nuggets/midos/writer'
|
33
|
+
|
30
34
|
module Nuggets
|
31
35
|
module Midos
|
32
36
|
|
@@ -51,23 +55,18 @@ module Nuggets
|
|
51
55
|
class << self
|
52
56
|
|
53
57
|
def filter(source, target, source_options = {}, target_options = source_options)
|
54
|
-
|
58
|
+
writer, size = Writer.new(target_options.merge(:io => target)), 0
|
55
59
|
|
56
|
-
|
57
|
-
|
60
|
+
Reader.parse(source, source_options) { |*args|
|
61
|
+
writer << args and size += 1 if yield(*args)
|
58
62
|
}
|
59
63
|
|
60
|
-
|
61
|
-
|
62
|
-
records
|
64
|
+
size
|
63
65
|
end
|
64
66
|
|
65
67
|
def filter_file(source_file, target_file, source_options = {}, target_options = source_options, &block)
|
66
|
-
source_options
|
67
|
-
|
68
|
-
|
69
|
-
::File.open(source_file, :encoding => source_options[:encoding]) { |source|
|
70
|
-
::File.open(target_file, 'w', :encoding => target_options[:encoding]) { |target|
|
68
|
+
open_file(source_file, source_options) { |source|
|
69
|
+
open_file(target_file, target_options, 'w') { |target|
|
71
70
|
filter(source, target, source_options, target_options, &block)
|
72
71
|
}
|
73
72
|
}
|
@@ -81,161 +80,9 @@ module Nuggets
|
|
81
80
|
filter_file(*args) { |*| true }
|
82
81
|
end
|
83
82
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
def initialize(options = {}, &block)
|
89
|
-
self.key = options[:key]
|
90
|
-
|
91
|
-
self.rs = options[:rs] || DEFAULT_RS
|
92
|
-
self.fs = options[:fs] || DEFAULT_FS
|
93
|
-
self.vs = options[:vs] || DEFAULT_VS
|
94
|
-
self.nl = options[:nl] || DEFAULT_NL
|
95
|
-
self.le = options[:le] || DEFAULT_LE
|
96
|
-
|
97
|
-
@auto_id_block = block
|
98
|
-
reset
|
99
|
-
end
|
100
|
-
|
101
|
-
attr_accessor :key, :rs, :fs, :nl, :le, :auto_id
|
102
|
-
|
103
|
-
attr_reader :vs, :records
|
104
|
-
|
105
|
-
def vs=(vs)
|
106
|
-
@vs = vs.is_a?(::Regexp) ? vs : %r{\s*#{::Regexp.escape(vs)}\s*}
|
107
|
-
end
|
108
|
-
|
109
|
-
def reset
|
110
|
-
@records = {}
|
111
|
-
@auto_id = @auto_id_block ? @auto_id_block.call : default_auto_id
|
112
|
-
end
|
113
|
-
|
114
|
-
private
|
115
|
-
|
116
|
-
def default_auto_id(n = 0)
|
117
|
-
lambda { n += 1 }
|
118
|
-
end
|
119
|
-
|
120
|
-
end
|
121
|
-
|
122
|
-
class Parser < Base
|
123
|
-
|
124
|
-
class << self
|
125
|
-
|
126
|
-
def parse(source, options = {}, &block)
|
127
|
-
parser = new(options).parse(source, &block)
|
128
|
-
block_given? ? parser : parser.records
|
129
|
-
end
|
130
|
-
|
131
|
-
def parse_file(file, options = {}, &block)
|
132
|
-
::File.open(file, :encoding => options[:encoding] ||= DEFAULT_ENCODING) { |source|
|
133
|
-
parse(source, options, &block)
|
134
|
-
}
|
135
|
-
end
|
136
|
-
|
137
|
-
end
|
138
|
-
|
139
|
-
def initialize(options = {})
|
140
|
-
super
|
141
|
-
@vs = /\s*#{::Regexp.escape(@vs)}\s*/ unless @vs.is_a?(::Regexp)
|
142
|
-
end
|
143
|
-
|
144
|
-
def parse(source, &block)
|
145
|
-
unless block
|
146
|
-
records, block = @records, amend_block { |id, record|
|
147
|
-
records[id] = record
|
148
|
-
}
|
149
|
-
end
|
150
|
-
|
151
|
-
rs, fs, vs, nl, le, key, auto_id, id, record =
|
152
|
-
@rs, @fs, @vs, @nl, @le, @key, @auto_id, nil, {}
|
153
|
-
|
154
|
-
source.each { |line|
|
155
|
-
line = line.chomp(le)
|
156
|
-
|
157
|
-
if line == rs
|
158
|
-
block[key ? id : auto_id.call, record]
|
159
|
-
id, record = nil, {}
|
160
|
-
else
|
161
|
-
k, v = line.split(fs, 2)
|
162
|
-
|
163
|
-
if k && v
|
164
|
-
if k == key
|
165
|
-
id = v
|
166
|
-
else
|
167
|
-
v.gsub!(nl, "\n")
|
168
|
-
v = v.split(vs) if v.index(vs)
|
169
|
-
end
|
170
|
-
|
171
|
-
record[k] = v
|
172
|
-
end
|
173
|
-
end
|
174
|
-
}
|
175
|
-
|
176
|
-
self
|
177
|
-
end
|
178
|
-
|
179
|
-
private
|
180
|
-
|
181
|
-
def amend_block(&block)
|
182
|
-
return block unless $VERBOSE && k = @key
|
183
|
-
|
184
|
-
r, i = block.binding.eval('_ = records, source')
|
185
|
-
|
186
|
-
l = i.respond_to?(:lineno)
|
187
|
-
s = i.respond_to?(:path) ? i.path :
|
188
|
-
::Object.instance_method(:inspect).bind(i).call
|
189
|
-
|
190
|
-
lambda { |id, *args|
|
191
|
-
if (r ||= block.binding.eval('records')).has_key?(id)
|
192
|
-
warn "Duplicate record in #{s}#{":#{i.lineno}" if l}: »#{k}:#{id}«"
|
193
|
-
end
|
194
|
-
|
195
|
-
block[id, *args]
|
196
|
-
}
|
197
|
-
end
|
198
|
-
|
199
|
-
end
|
200
|
-
|
201
|
-
class Writer < Base
|
202
|
-
|
203
|
-
class << self
|
204
|
-
|
205
|
-
def write(target, records, options = {})
|
206
|
-
new(options).write(target, records)
|
207
|
-
end
|
208
|
-
|
209
|
-
def write_file(file, records, options = {})
|
210
|
-
::File.open(file, 'w', :encoding => options[:encoding] ||= DEFAULT_ENCODING) { |target|
|
211
|
-
write(target, records, options)
|
212
|
-
}
|
213
|
-
end
|
214
|
-
|
215
|
-
end
|
216
|
-
|
217
|
-
def write(target, records = {})
|
218
|
-
rs, fs, vs, nl, le, key, auto_id =
|
219
|
-
@rs, @fs, @vs, @nl, @le, @key, @auto_id
|
220
|
-
|
221
|
-
@records.update(records).each { |id, record|
|
222
|
-
record, id = id, nil unless record
|
223
|
-
|
224
|
-
if key && !record.has_key?(key)
|
225
|
-
record[key] = id || auto_id.call
|
226
|
-
end
|
227
|
-
|
228
|
-
record.each { |k, v|
|
229
|
-
if v
|
230
|
-
v = v.is_a?(::Array) ? v.join(vs) : v.to_s
|
231
|
-
target << k << fs << v.gsub("\n", nl) << le
|
232
|
-
end
|
233
|
-
}
|
234
|
-
|
235
|
-
target << rs << le << le
|
236
|
-
}
|
237
|
-
|
238
|
-
self
|
83
|
+
def open_file(file, options = {}, mode = 'r', &block)
|
84
|
+
encoding = options[:encoding] ||= DEFAULT_ENCODING
|
85
|
+
::File.open(file, mode, :encoding => encoding, &block)
|
239
86
|
end
|
240
87
|
|
241
88
|
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
7
|
+
# language. #
|
8
|
+
# #
|
9
|
+
# Copyright (C) 2007-2014 Jens Wille #
|
10
|
+
# #
|
11
|
+
# Authors: #
|
12
|
+
# Jens Wille <jens.wille@gmail.com> #
|
13
|
+
# #
|
14
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
15
|
+
# under the terms of the GNU Affero General Public License as published by #
|
16
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
17
|
+
# option) any later version. #
|
18
|
+
# #
|
19
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
20
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
22
|
+
# for more details. #
|
23
|
+
# #
|
24
|
+
# You should have received a copy of the GNU Affero General Public License #
|
25
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
26
|
+
# #
|
27
|
+
###############################################################################
|
28
|
+
#++
|
29
|
+
|
30
|
+
module Nuggets
|
31
|
+
module Midos
|
32
|
+
class Base
|
33
|
+
|
34
|
+
class << self
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def file_method(method, mode, file, options = {}, *args, &block)
|
39
|
+
Midos.open_file(file, options, mode) { |io|
|
40
|
+
args.unshift(options.merge(:io => io))
|
41
|
+
method ? send(method, *args, &block) : block[new(*args)]
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def extract_options!(args)
|
46
|
+
args.last.is_a?(::Hash) ? args.pop : {}
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
def initialize(options = {}, &block)
|
52
|
+
self.key = options[:key]
|
53
|
+
|
54
|
+
self.rs = options[:rs] || DEFAULT_RS
|
55
|
+
self.fs = options[:fs] || DEFAULT_FS
|
56
|
+
self.vs = options[:vs] || DEFAULT_VS
|
57
|
+
self.nl = options[:nl] || DEFAULT_NL
|
58
|
+
self.le = options[:le] || DEFAULT_LE
|
59
|
+
self.io = options[:io] || self.class::DEFAULT_IO
|
60
|
+
|
61
|
+
@auto_id_block = options[:auto_id] || block
|
62
|
+
reset
|
63
|
+
end
|
64
|
+
|
65
|
+
attr_accessor :key, :rs, :fs, :nl, :le, :io, :auto_id
|
66
|
+
|
67
|
+
attr_reader :vs
|
68
|
+
|
69
|
+
def reset
|
70
|
+
@auto_id = @auto_id_block ? @auto_id_block.call : default_auto_id
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def default_auto_id(n = 0)
|
76
|
+
lambda { n += 1 }
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
7
|
+
# language. #
|
8
|
+
# #
|
9
|
+
# Copyright (C) 2007-2014 Jens Wille #
|
10
|
+
# #
|
11
|
+
# Authors: #
|
12
|
+
# Jens Wille <jens.wille@gmail.com> #
|
13
|
+
# #
|
14
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
15
|
+
# under the terms of the GNU Affero General Public License as published by #
|
16
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
17
|
+
# option) any later version. #
|
18
|
+
# #
|
19
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
20
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
22
|
+
# for more details. #
|
23
|
+
# #
|
24
|
+
# You should have received a copy of the GNU Affero General Public License #
|
25
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
26
|
+
# #
|
27
|
+
###############################################################################
|
28
|
+
#++
|
29
|
+
|
30
|
+
module Nuggets
|
31
|
+
module Midos
|
32
|
+
class Reader < Base
|
33
|
+
|
34
|
+
DEFAULT_IO = $stdin
|
35
|
+
|
36
|
+
class << self
|
37
|
+
|
38
|
+
def parse(*args, &block)
|
39
|
+
reader = new(extract_options!(args)).parse(*args, &block)
|
40
|
+
block ? reader : reader.records
|
41
|
+
end
|
42
|
+
|
43
|
+
def parse_file(*args, &block)
|
44
|
+
file_method(:parse, 'r', *args, &block)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
attr_reader :records
|
50
|
+
|
51
|
+
def reset
|
52
|
+
super
|
53
|
+
@records = {}
|
54
|
+
end
|
55
|
+
|
56
|
+
def vs=(vs)
|
57
|
+
@vs = vs.is_a?(::Regexp) ? vs : %r{\s*#{::Regexp.escape(vs)}\s*}
|
58
|
+
end
|
59
|
+
|
60
|
+
def parse(io = io, &block)
|
61
|
+
unless block
|
62
|
+
records, block = @records, amend_block { |id, record|
|
63
|
+
records[id] = record
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
rs, fs, vs, nl, le, key, auto_id, id, record =
|
68
|
+
@rs, @fs, @vs, @nl, @le, @key, @auto_id, nil, {}
|
69
|
+
|
70
|
+
io.each { |line|
|
71
|
+
line = line.chomp(le)
|
72
|
+
|
73
|
+
if line == rs
|
74
|
+
block[key ? id : auto_id.call, record]
|
75
|
+
id, record = nil, {}
|
76
|
+
else
|
77
|
+
k, v = line.split(fs, 2)
|
78
|
+
|
79
|
+
if k && v
|
80
|
+
if k == key
|
81
|
+
id = v
|
82
|
+
else
|
83
|
+
v.gsub!(nl, "\n")
|
84
|
+
v = v.split(vs) if v.index(vs)
|
85
|
+
end
|
86
|
+
|
87
|
+
record[k] = v
|
88
|
+
end
|
89
|
+
end
|
90
|
+
}
|
91
|
+
|
92
|
+
self
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def amend_block(&block)
|
98
|
+
return block unless $VERBOSE && k = @key
|
99
|
+
|
100
|
+
r, i = block.binding.eval('_ = records, io')
|
101
|
+
|
102
|
+
l = i.respond_to?(:lineno)
|
103
|
+
s = i.respond_to?(:path) ? i.path :
|
104
|
+
::Object.instance_method(:inspect).bind(i).call
|
105
|
+
|
106
|
+
lambda { |id, *args|
|
107
|
+
if (r ||= block.binding.eval('records')).key?(id)
|
108
|
+
warn "Duplicate record in #{s}#{":#{i.lineno}" if l}: »#{k}:#{id}«"
|
109
|
+
end
|
110
|
+
|
111
|
+
block[id, *args]
|
112
|
+
}
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,252 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
7
|
+
# language. #
|
8
|
+
# #
|
9
|
+
# Copyright (C) 2007-2014 Jens Wille #
|
10
|
+
# #
|
11
|
+
# Authors: #
|
12
|
+
# Jens Wille <jens.wille@gmail.com> #
|
13
|
+
# #
|
14
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
15
|
+
# under the terms of the GNU Affero General Public License as published by #
|
16
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
17
|
+
# option) any later version. #
|
18
|
+
# #
|
19
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
20
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
22
|
+
# for more details. #
|
23
|
+
# #
|
24
|
+
# You should have received a copy of the GNU Affero General Public License #
|
25
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
26
|
+
# #
|
27
|
+
###############################################################################
|
28
|
+
#++
|
29
|
+
|
30
|
+
require 'nuggets/hash/idmap'
|
31
|
+
|
32
|
+
module Nuggets
|
33
|
+
module Midos
|
34
|
+
class Writer < Base
|
35
|
+
|
36
|
+
DEFAULT_IO = $stdout
|
37
|
+
|
38
|
+
class << self
|
39
|
+
|
40
|
+
def write(*args, &block)
|
41
|
+
new(extract_options!(args), &block).write(*args)
|
42
|
+
end
|
43
|
+
|
44
|
+
def write_file(*args, &block)
|
45
|
+
file_method(:write, 'w', *args, &block)
|
46
|
+
end
|
47
|
+
|
48
|
+
def open(*args, &block)
|
49
|
+
file_method(nil, 'w', *args, &block)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
def vs=(vs)
|
55
|
+
vs.is_a?(::String) ? @vs = vs : raise(::TypeError,
|
56
|
+
"wrong argument type #{vs.class} (expected String)")
|
57
|
+
end
|
58
|
+
|
59
|
+
def write(records, *args)
|
60
|
+
if records.is_a?(::Hash)
|
61
|
+
records.each { |id, record| write_i(id, record, *args) }
|
62
|
+
else
|
63
|
+
records.each { |record| write_i(nil, record, *args) }
|
64
|
+
end
|
65
|
+
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
def put(record, *args)
|
70
|
+
if record.is_a?(::Hash)
|
71
|
+
write_i(nil, record, *args)
|
72
|
+
else
|
73
|
+
write_i(*args.unshift(*record))
|
74
|
+
end
|
75
|
+
|
76
|
+
self
|
77
|
+
end
|
78
|
+
|
79
|
+
alias_method :<<, :put
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def write_i(id, record, io = io)
|
84
|
+
return if record.empty?
|
85
|
+
|
86
|
+
if @key && !record.key?(@key)
|
87
|
+
record[@key] = id || @auto_id.call
|
88
|
+
end
|
89
|
+
|
90
|
+
record.each { |k, v|
|
91
|
+
if v
|
92
|
+
if k
|
93
|
+
v = v.is_a?(::Array) ? v.join(@vs) : v.to_s
|
94
|
+
io << k << @fs << v.gsub("\n", @nl) << @le
|
95
|
+
else
|
96
|
+
Array(v).each { |w| io << w.to_s << @le }
|
97
|
+
end
|
98
|
+
end
|
99
|
+
}
|
100
|
+
|
101
|
+
io << @rs << @le << @le
|
102
|
+
end
|
103
|
+
|
104
|
+
class Thesaurus < self
|
105
|
+
|
106
|
+
PROLOGUE = {
|
107
|
+
:PAR => '1011111111110000000010001000000000000010',
|
108
|
+
:DAT => '00000000',
|
109
|
+
:DES => 'DE',
|
110
|
+
:TOP => 'TP~TP',
|
111
|
+
:KLA => 'CC~CC',
|
112
|
+
:OBR => 'BT~BT',
|
113
|
+
:UTR => 'NT~NT',
|
114
|
+
:SYN => 'UF~USE',
|
115
|
+
:FRU => 'PT~PT für',
|
116
|
+
:VER => 'RT~RT',
|
117
|
+
:SP1 => 'ENG~ENG für',
|
118
|
+
:SP2 => 'FRA~FRA für',
|
119
|
+
:SP3 => 'SPA~SPA für',
|
120
|
+
:SP4 => 'ITA~ITA für',
|
121
|
+
:SP5 => 'GRI~GRI für',
|
122
|
+
:SP6 => 'RUS~RUS für',
|
123
|
+
:SP7 => 'POL~POL für',
|
124
|
+
:SP8 => 'UNG~UNG für',
|
125
|
+
:SP9 => 'TSC~TSC für',
|
126
|
+
:SN1 => 'SN1',
|
127
|
+
:SN2 => 'SN2',
|
128
|
+
:SN3 => 'SN3',
|
129
|
+
:SN4 => 'SN4',
|
130
|
+
:SN5 => 'SN5',
|
131
|
+
:DA1 => 'DATE1',
|
132
|
+
:DA2 => 'DATE2',
|
133
|
+
:DA3 => 'DATE3',
|
134
|
+
:DA4 => 'DATE4',
|
135
|
+
:KLD => 'MIDOS Thesaurus',
|
136
|
+
:KOM => ' / ',
|
137
|
+
:KO1 => 'UF',
|
138
|
+
:KO2 => 'USE',
|
139
|
+
:TLE => ' 32000 Zeichen',
|
140
|
+
:PAW => '',
|
141
|
+
:ART => '00000',
|
142
|
+
:REL => ' 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25'
|
143
|
+
}
|
144
|
+
|
145
|
+
EPILOGUE = {
|
146
|
+
:DE => '*****NICHTDESKRIPTORRELATIONEN*****'
|
147
|
+
}
|
148
|
+
|
149
|
+
RESOLVE_FROM = [:OBR, :UTR, :VER]
|
150
|
+
|
151
|
+
RESOLVE_TO = :DES
|
152
|
+
|
153
|
+
NAME = :KLD
|
154
|
+
|
155
|
+
class << self
|
156
|
+
|
157
|
+
def write(*args, &block)
|
158
|
+
new(extract_options!(args), &block).instruct! { |mth| mth.write(*args) }
|
159
|
+
end
|
160
|
+
|
161
|
+
def open(*args, &block)
|
162
|
+
super { |mth| mth.instruct!(&block) }
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
def initialize(options = {}, prologue = {}, epilogue = {}, &block)
|
168
|
+
super(options, &block)
|
169
|
+
|
170
|
+
prologue[self.class::NAME] ||= options[:name]
|
171
|
+
|
172
|
+
@prologue = self.class::PROLOGUE.merge(prologue)
|
173
|
+
@epilogue = self.class::EPILOGUE.merge(epilogue)
|
174
|
+
end
|
175
|
+
|
176
|
+
attr_reader :prologue, :epilogue
|
177
|
+
|
178
|
+
def instruct!(*args)
|
179
|
+
put(prologue, *args)
|
180
|
+
yield self
|
181
|
+
put(epilogue, *args)
|
182
|
+
end
|
183
|
+
|
184
|
+
private
|
185
|
+
|
186
|
+
def merge_records(hash, records, *args)
|
187
|
+
args = [hash, records, *resolve_from_to(*args)]
|
188
|
+
|
189
|
+
records.each { |id, record|
|
190
|
+
new_record = hash[id] = {}
|
191
|
+
record.each { |key, value| new_record[key] = resolve(key, value, *args) }
|
192
|
+
}
|
193
|
+
end
|
194
|
+
|
195
|
+
def resolve_from_to(from = nil, to = prologue[RESOLVE_TO])
|
196
|
+
if from.nil? || from == true
|
197
|
+
from = prologue.values_at(*RESOLVE_FROM).map { |v| v.split('~').first }
|
198
|
+
end
|
199
|
+
|
200
|
+
[from, to]
|
201
|
+
end
|
202
|
+
|
203
|
+
def resolve(key, value, hash, records, from = nil, to = nil)
|
204
|
+
from && from.include?(key) ? value.map { |id| records[id][to] } : value
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
class ThesaurusX < Thesaurus
|
210
|
+
|
211
|
+
PROLOGUE = {
|
212
|
+
'MTX-PARAMETER' => '',
|
213
|
+
:BEZ => 'MIDOS Thesaurus',
|
214
|
+
:KOM => ' / ',
|
215
|
+
:TXL => 0,
|
216
|
+
:REL => '',
|
217
|
+
nil => %w[
|
218
|
+
TT1|Topterm|TT1||||||
|
219
|
+
BT1|Oberbegriff|BT1||||||
|
220
|
+
NT1|Unterbegriff|NT1||||||
|
221
|
+
RT1|Verwandter\ Begriff|RT1||||||
|
222
|
+
SY1|Synonym1|SY1|SY1FOR|||||
|
223
|
+
]
|
224
|
+
}
|
225
|
+
|
226
|
+
EPILOGUE = {}
|
227
|
+
|
228
|
+
NAME = :BEZ
|
229
|
+
|
230
|
+
private
|
231
|
+
|
232
|
+
def merge_records(hash, *)
|
233
|
+
idmap = hash[:__list__] = ::Hash.idmap
|
234
|
+
|
235
|
+
super
|
236
|
+
|
237
|
+
idmap.replace(nil => idmap.map { |key, id| "#{key}|DE|#{id}" })
|
238
|
+
end
|
239
|
+
|
240
|
+
def resolve_from_to(*)
|
241
|
+
# nothing to do
|
242
|
+
end
|
243
|
+
|
244
|
+
def resolve(key, value, hash, *)
|
245
|
+
value.map { |id| hash[:__list__][id] }
|
246
|
+
end
|
247
|
+
|
248
|
+
end
|
249
|
+
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
7
|
+
# language. #
|
8
|
+
# #
|
9
|
+
# Copyright (C) 2007-2014 Jens Wille #
|
10
|
+
# #
|
11
|
+
# Authors: #
|
12
|
+
# Jens Wille <jens.wille@gmail.com> #
|
13
|
+
# #
|
14
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
15
|
+
# under the terms of the GNU Affero General Public License as published by #
|
16
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
17
|
+
# option) any later version. #
|
18
|
+
# #
|
19
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
20
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
22
|
+
# for more details. #
|
23
|
+
# #
|
24
|
+
# You should have received a copy of the GNU Affero General Public License #
|
25
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
26
|
+
# #
|
27
|
+
###############################################################################
|
28
|
+
#++
|
29
|
+
|
30
|
+
require 'rdf/util'
|
31
|
+
|
32
|
+
begin
|
33
|
+
require 'zlib'
|
34
|
+
rescue LoadError => err
|
35
|
+
warn err if $VERBOSE
|
36
|
+
end
|
37
|
+
|
38
|
+
begin
|
39
|
+
require 'rbzip2'
|
40
|
+
|
41
|
+
class RBzip2::Decompressor
|
42
|
+
|
43
|
+
def eof?
|
44
|
+
@current_state == EOF
|
45
|
+
end
|
46
|
+
|
47
|
+
def gets(sep = $/)
|
48
|
+
r = ''
|
49
|
+
|
50
|
+
loop {
|
51
|
+
b = read0
|
52
|
+
break if b < 0
|
53
|
+
|
54
|
+
count(1)
|
55
|
+
r << b
|
56
|
+
|
57
|
+
break if r.end_with?(sep)
|
58
|
+
}
|
59
|
+
|
60
|
+
r
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
rescue LoadError => err
|
65
|
+
warn err if $VERBOSE
|
66
|
+
end
|
67
|
+
|
68
|
+
class << RDF::Util::File
|
69
|
+
|
70
|
+
alias_method :_nuggets_original_open_file, :open_file
|
71
|
+
|
72
|
+
def open_file(filename_or_url, options = {}, &block)
|
73
|
+
klass = begin
|
74
|
+
case File.extname(filename_or_url).downcase.sub(/\A\./, '')
|
75
|
+
when /\Abz(?:ip)?2?\z/ then RBzip2::Decompressor
|
76
|
+
when /\Agz(?:ip)?\z/ then Zlib::GzipReader
|
77
|
+
end
|
78
|
+
rescue NameError => err
|
79
|
+
err.message.sub!('Module::', '')
|
80
|
+
raise
|
81
|
+
end
|
82
|
+
|
83
|
+
if klass
|
84
|
+
original_block, block = block, lambda { |file|
|
85
|
+
original_block[file.is_a?(IO) ? klass.new(file) : file]
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
_nuggets_original_open_file(filename_or_url, options, &block)
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
7
|
+
# language. #
|
8
|
+
# #
|
9
|
+
# Copyright (C) 2007-2014 Jens Wille #
|
10
|
+
# #
|
11
|
+
# Authors: #
|
12
|
+
# Jens Wille <jens.wille@gmail.com> #
|
13
|
+
# #
|
14
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
15
|
+
# under the terms of the GNU Affero General Public License as published by #
|
16
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
17
|
+
# option) any later version. #
|
18
|
+
# #
|
19
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
20
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
22
|
+
# for more details. #
|
23
|
+
# #
|
24
|
+
# You should have received a copy of the GNU Affero General Public License #
|
25
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
26
|
+
# #
|
27
|
+
###############################################################################
|
28
|
+
#++
|
29
|
+
|
30
|
+
require 'rdf'
|
31
|
+
|
32
|
+
def RDF.__prefix__; :rdf; end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
7
|
+
# language. #
|
8
|
+
# #
|
9
|
+
# Copyright (C) 2007-2014 Jens Wille #
|
10
|
+
# #
|
11
|
+
# Authors: #
|
12
|
+
# Jens Wille <jens.wille@gmail.com> #
|
13
|
+
# #
|
14
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
15
|
+
# under the terms of the GNU Affero General Public License as published by #
|
16
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
17
|
+
# option) any later version. #
|
18
|
+
# #
|
19
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
20
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
22
|
+
# for more details. #
|
23
|
+
# #
|
24
|
+
# You should have received a copy of the GNU Affero General Public License #
|
25
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
26
|
+
# #
|
27
|
+
###############################################################################
|
28
|
+
#++
|
29
|
+
|
30
|
+
require 'nuggets/rdf/turtle/reader'
|
31
|
+
require 'nuggets/rdf/compression'
|
32
|
+
require 'nuggets/rdf/prefix'
|
33
|
+
require 'nuggets/rdf/uri'
|
34
|
+
|
35
|
+
module Nuggets
|
36
|
+
module RDF
|
37
|
+
class Turtle
|
38
|
+
|
39
|
+
include ::Enumerable
|
40
|
+
|
41
|
+
NS_SEPARATOR = ':'.freeze
|
42
|
+
|
43
|
+
NS = ::RDF::Vocabulary.inject({}) { |h, v| h[v.__prefix__] = v; h }
|
44
|
+
|
45
|
+
class << self
|
46
|
+
|
47
|
+
def open(file, *args)
|
48
|
+
::RDF::Reader.open(file, :format => :ttl) { |reader|
|
49
|
+
turtle = new(reader, *args)
|
50
|
+
turtle.file = ::File.expand_path(file)
|
51
|
+
|
52
|
+
return block_given? ? yield(turtle) : turtle
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def foreach(file, *args, &block)
|
57
|
+
open(file, *args) { |turtle| turtle.each(&block) }
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
def initialize(reader, map = true)
|
63
|
+
@reader, @base, @prefixes = reader, *reader.parse_prologue
|
64
|
+
self.map = map
|
65
|
+
end
|
66
|
+
|
67
|
+
attr_reader :reader, :map, :base, :prefixes
|
68
|
+
|
69
|
+
attr_accessor :file
|
70
|
+
|
71
|
+
def map=(map)
|
72
|
+
unless map.is_a?(::Hash)
|
73
|
+
@map = ::Hash.new(map)
|
74
|
+
else
|
75
|
+
@map = {}
|
76
|
+
|
77
|
+
map.each { |k, v|
|
78
|
+
if k.is_a?(::String)
|
79
|
+
n, s = k.split(NS_SEPARATOR, 2)
|
80
|
+
k = NS.key?(n = n.to_sym) ? NS[n][s] : prefixes[n] / s
|
81
|
+
end
|
82
|
+
|
83
|
+
@map[k] = v
|
84
|
+
}
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def statements
|
89
|
+
each_statement.to_a
|
90
|
+
end
|
91
|
+
|
92
|
+
def each_statement(&block)
|
93
|
+
return enum_for(:each_statement) unless block_given?
|
94
|
+
reader.parse_statements(&block)
|
95
|
+
self
|
96
|
+
end
|
97
|
+
|
98
|
+
def each
|
99
|
+
return enum_for(:each) unless block_given?
|
100
|
+
|
101
|
+
uri, map, base = ::RDF::URI, self.map, self.base
|
102
|
+
|
103
|
+
each_statement { |t|
|
104
|
+
s, p, o = *t
|
105
|
+
|
106
|
+
if s.is_a?(uri) and k = map[p] and s.start_with?(base)
|
107
|
+
yield s, o, k
|
108
|
+
end
|
109
|
+
}
|
110
|
+
end
|
111
|
+
|
112
|
+
def closed?
|
113
|
+
reader.closed?
|
114
|
+
end
|
115
|
+
|
116
|
+
def inspect
|
117
|
+
'#<%s:0x%x @file=%p, @base=%p%s>' % [
|
118
|
+
self.class, object_id, file, base, closed? ? ' (closed)' : ''
|
119
|
+
]
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
7
|
+
# language. #
|
8
|
+
# #
|
9
|
+
# Copyright (C) 2007-2014 Jens Wille #
|
10
|
+
# #
|
11
|
+
# Authors: #
|
12
|
+
# Jens Wille <jens.wille@gmail.com> #
|
13
|
+
# #
|
14
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
15
|
+
# under the terms of the GNU Affero General Public License as published by #
|
16
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
17
|
+
# option) any later version. #
|
18
|
+
# #
|
19
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
20
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
22
|
+
# for more details. #
|
23
|
+
# #
|
24
|
+
# You should have received a copy of the GNU Affero General Public License #
|
25
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
26
|
+
# #
|
27
|
+
###############################################################################
|
28
|
+
#++
|
29
|
+
|
30
|
+
require 'rdf/turtle'
|
31
|
+
|
32
|
+
module RDF
|
33
|
+
class Turtle::Reader
|
34
|
+
|
35
|
+
PARSE_OPTIONS = {
|
36
|
+
:branch => BRANCH,
|
37
|
+
:first => FIRST,
|
38
|
+
:follow => FOLLOW,
|
39
|
+
:reset_on_start => true
|
40
|
+
}
|
41
|
+
|
42
|
+
def closed?
|
43
|
+
@input.closed?
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse_prologue
|
47
|
+
parse_internal { break }
|
48
|
+
rewind
|
49
|
+
[base_uri, prefixes]
|
50
|
+
end
|
51
|
+
|
52
|
+
def parse_statements
|
53
|
+
parse_internal { |context, _, *data|
|
54
|
+
if context == :statement
|
55
|
+
data[3] = { :context => data[3] }
|
56
|
+
yield Statement.new(*data)
|
57
|
+
end
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def parse_internal(&block)
|
64
|
+
parse(@input, START, @options.merge(PARSE_OPTIONS), &block)
|
65
|
+
rescue => err
|
66
|
+
err.message << " (line #{lineno})"
|
67
|
+
raise
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
7
|
+
# language. #
|
8
|
+
# #
|
9
|
+
# Copyright (C) 2007-2014 Jens Wille #
|
10
|
+
# #
|
11
|
+
# Authors: #
|
12
|
+
# Jens Wille <jens.wille@gmail.com> #
|
13
|
+
# #
|
14
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
15
|
+
# under the terms of the GNU Affero General Public License as published by #
|
16
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
17
|
+
# option) any later version. #
|
18
|
+
# #
|
19
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
20
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
22
|
+
# for more details. #
|
23
|
+
# #
|
24
|
+
# You should have received a copy of the GNU Affero General Public License #
|
25
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
26
|
+
# #
|
27
|
+
###############################################################################
|
28
|
+
#++
|
29
|
+
|
30
|
+
require 'rdf'
|
31
|
+
|
32
|
+
class RDF::URI
|
33
|
+
|
34
|
+
def basename
|
35
|
+
File.basename(path).sub(/;[^\/]*$/, '')
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/lib/nuggets/version.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'nuggets/array/hashify'
|
2
|
+
|
3
|
+
describe Array, 'when extended by', Nuggets::Array::HashifyMixin do
|
4
|
+
|
5
|
+
it { Array.ancestors.should include(Nuggets::Array::HashifyMixin) }
|
6
|
+
|
7
|
+
example do
|
8
|
+
hash = [1, 2, 3, 4].hashify
|
9
|
+
hash.should == { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }
|
10
|
+
hash[42].should be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
example do
|
14
|
+
[1, 2, 3, 4].hashify { |i| [i, i ** 2] }.should == { 1 => 1, 2 => 4, 3 => 9, 4 => 16 }
|
15
|
+
end
|
16
|
+
|
17
|
+
example do
|
18
|
+
[1, 2, 3, 4].hashify { |i| [i ** 2, i] }.should == { 1 => 1, 4 => 2, 9 => 3, 16 => 4 }
|
19
|
+
end
|
20
|
+
|
21
|
+
example do
|
22
|
+
lambda { [1, 2, 3, 4].hashify { |i| i } }.should raise_error(ArgumentError)
|
23
|
+
end
|
24
|
+
|
25
|
+
example do
|
26
|
+
hash = [1, 2, 3, 4].hashify(0)
|
27
|
+
hash.should == { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }
|
28
|
+
hash[42].should == 0
|
29
|
+
end
|
30
|
+
|
31
|
+
example do
|
32
|
+
hash = [1, 2, 3, 4].hashify(lambda { |h, k| h[k] = [] })
|
33
|
+
hash.should == { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }
|
34
|
+
hash[42].should == []
|
35
|
+
end
|
36
|
+
|
37
|
+
example do
|
38
|
+
hash = [1, 2, 3, 4].hashify(4 => -1, 23 => 42)
|
39
|
+
hash.should == { 1 => 1, 2 => 2, 3 => 3, 4 => 4, 23 => 42 }
|
40
|
+
hash[42].should be_nil
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-nuggets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Wille
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Some extensions to the Ruby programming language.
|
14
14
|
email: jens.wille@gmail.com
|
@@ -19,6 +19,11 @@ extra_rdoc_files:
|
|
19
19
|
- COPYING
|
20
20
|
- ChangeLog
|
21
21
|
files:
|
22
|
+
- ".rspec"
|
23
|
+
- COPYING
|
24
|
+
- ChangeLog
|
25
|
+
- README
|
26
|
+
- Rakefile
|
22
27
|
- lib/nuggets.rb
|
23
28
|
- lib/nuggets/all.rb
|
24
29
|
- lib/nuggets/all_mixins.rb
|
@@ -34,6 +39,8 @@ files:
|
|
34
39
|
- lib/nuggets/array/flush.rb
|
35
40
|
- lib/nuggets/array/flush_mixin.rb
|
36
41
|
- lib/nuggets/array/format.rb
|
42
|
+
- lib/nuggets/array/hashify.rb
|
43
|
+
- lib/nuggets/array/hashify_mixin.rb
|
37
44
|
- lib/nuggets/array/histogram.rb
|
38
45
|
- lib/nuggets/array/histogram_mixin.rb
|
39
46
|
- lib/nuggets/array/in_order.rb
|
@@ -113,6 +120,9 @@ files:
|
|
113
120
|
- lib/nuggets/log_parser/rails.rb
|
114
121
|
- lib/nuggets/lsi.rb
|
115
122
|
- lib/nuggets/midos.rb
|
123
|
+
- lib/nuggets/midos/base.rb
|
124
|
+
- lib/nuggets/midos/reader.rb
|
125
|
+
- lib/nuggets/midos/writer.rb
|
116
126
|
- lib/nuggets/mysql.rb
|
117
127
|
- lib/nuggets/net/success.rb
|
118
128
|
- lib/nuggets/numeric/between.rb
|
@@ -140,6 +150,11 @@ files:
|
|
140
150
|
- lib/nuggets/proc/bind_mixin.rb
|
141
151
|
- lib/nuggets/range/quantile.rb
|
142
152
|
- lib/nuggets/range/quantile_mixin.rb
|
153
|
+
- lib/nuggets/rdf/compression.rb
|
154
|
+
- lib/nuggets/rdf/prefix.rb
|
155
|
+
- lib/nuggets/rdf/turtle.rb
|
156
|
+
- lib/nuggets/rdf/turtle/reader.rb
|
157
|
+
- lib/nuggets/rdf/uri.rb
|
143
158
|
- lib/nuggets/ruby.rb
|
144
159
|
- lib/nuggets/statistics.rb
|
145
160
|
- lib/nuggets/statistics_mixins.rb
|
@@ -178,13 +193,10 @@ files:
|
|
178
193
|
- lib/nuggets/util/pluggable.rb
|
179
194
|
- lib/nuggets/util/ruby.rb
|
180
195
|
- lib/nuggets/version.rb
|
181
|
-
- COPYING
|
182
|
-
- ChangeLog
|
183
|
-
- README
|
184
|
-
- Rakefile
|
185
196
|
- spec/nuggets/array/boost_spec.rb
|
186
197
|
- spec/nuggets/array/correlation_spec.rb
|
187
198
|
- spec/nuggets/array/flush_spec.rb
|
199
|
+
- spec/nuggets/array/hashify_spec.rb
|
188
200
|
- spec/nuggets/array/histogram_spec.rb
|
189
201
|
- spec/nuggets/array/limit_spec.rb
|
190
202
|
- spec/nuggets/array/mean_spec.rb
|
@@ -221,7 +233,6 @@ files:
|
|
221
233
|
- spec/nuggets/uri/content_type_spec.rb
|
222
234
|
- spec/nuggets/uri/exist_spec.rb
|
223
235
|
- spec/spec_helper.rb
|
224
|
-
- ".rspec"
|
225
236
|
homepage: http://github.com/blackwinter/ruby-nuggets
|
226
237
|
licenses:
|
227
238
|
- AGPL-3.0
|
@@ -229,7 +240,7 @@ metadata: {}
|
|
229
240
|
post_install_message:
|
230
241
|
rdoc_options:
|
231
242
|
- "--title"
|
232
|
-
- ruby-nuggets Application documentation (v0.9.
|
243
|
+
- ruby-nuggets Application documentation (v0.9.7)
|
233
244
|
- "--charset"
|
234
245
|
- UTF-8
|
235
246
|
- "--line-numbers"
|
@@ -250,7 +261,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
250
261
|
version: '0'
|
251
262
|
requirements: []
|
252
263
|
rubyforge_project:
|
253
|
-
rubygems_version: 2.
|
264
|
+
rubygems_version: 2.2.2
|
254
265
|
signing_key:
|
255
266
|
specification_version: 4
|
256
267
|
summary: Some extensions to the Ruby programming language.
|