ruby-nuggets 0.9.1 → 0.9.2

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.
@@ -1,242 +1,4 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 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
- #++
1
+ require 'nuggets/cli'
2
+ module Util; CLI = ::Nuggets::CLI; end
27
3
 
28
- require 'optparse'
29
- require 'yaml'
30
- require 'zlib'
31
- require 'highline'
32
-
33
- module Util
34
-
35
- class CLI
36
-
37
- class << self
38
-
39
- def usage(prog)
40
- "Usage: #{prog} [-h|--help] [options]"
41
- end
42
-
43
- def version
44
- parent_const_get(:VERSION)
45
- end
46
-
47
- def defaults
48
- {}
49
- end
50
-
51
- def execute(*args)
52
- new.execute(*args)
53
- end
54
-
55
- private
56
-
57
- def parent_const_get(const, range = 0...-1)
58
- name.split('::').inject([::Object]) { |memo, name|
59
- memo << memo.last.const_get(name)
60
- }.reverse[range].each { |mod|
61
- return mod.const_get(const) if mod.const_defined?(const)
62
- }
63
-
64
- raise ::NameError, "uninitialized constant #{self}::#{const}"
65
- end
66
-
67
- end
68
-
69
- attr_reader :options, :config, :defaults
70
- attr_reader :stdin, :stdout, :stderr
71
-
72
- attr_accessor :prog
73
-
74
- def initialize(defaults = nil, *args)
75
- @defaults, @prog = defaults || self.class.defaults, $0
76
-
77
- init(*args)
78
-
79
- # prevent backtrace on ^C
80
- trap(:INT) { exit 130 }
81
- end
82
-
83
- def progname
84
- ::File.basename(prog)
85
- end
86
-
87
- def usage
88
- self.class.usage(prog)
89
- end
90
-
91
- def version
92
- self.class.version
93
- end
94
-
95
- def execute(arguments = ::ARGV, *inouterr)
96
- reset(*inouterr)
97
- parse_options(arguments)
98
- run(arguments)
99
- rescue => err
100
- raise if $VERBOSE
101
- abort "#{err.backtrace.first}: #{err} (#{err.class})"
102
- ensure
103
- options.each_value { |value|
104
- value.close if value.is_a?(::Zlib::GzipWriter)
105
- }
106
- end
107
-
108
- def reset(stdin = ::STDIN, stdout = ::STDOUT, stderr = ::STDERR)
109
- @stdin, @stdout, @stderr = stdin, stdout, stderr
110
- @options, @config = {}, {}
111
- end
112
-
113
- private
114
-
115
- def init(*args)
116
- reset
117
- end
118
-
119
- def ask(question, &block)
120
- ::HighLine.new(stdin, stdout).ask(question, &block)
121
- end
122
-
123
- def puts(*msg)
124
- stdout.puts(*msg)
125
- end
126
-
127
- def warn(*msg)
128
- stderr.puts(*msg)
129
- end
130
-
131
- def quit(msg = nil, include_usage = msg != false)
132
- out = []
133
-
134
- out << "#{progname}: #{msg}" if msg
135
- out << usage if include_usage
136
-
137
- abort out.any? && out.join("\n\n")
138
- end
139
-
140
- def abort(msg = nil, status = 1)
141
- warn(msg) if msg
142
- exit(status)
143
- end
144
-
145
- def shut(msg = nil, status = 0)
146
- puts(msg) if msg
147
- exit(status)
148
- end
149
-
150
- def exit(status = 0)
151
- ::Kernel.exit(status)
152
- end
153
-
154
- def open_file_or_std(file, write = false)
155
- if file == '-'
156
- write ? stdout : stdin
157
- else
158
- gz = file =~ /\.gz\z/i
159
-
160
- if write
161
- gz ? ::Zlib::GzipWriter.open(file) : ::File.open(file, 'w')
162
- else
163
- quit "No such file: #{file}" unless ::File.readable?(file)
164
- (gz ? ::Zlib::GzipReader : ::File).open(file)
165
- end
166
- end
167
- end
168
-
169
- def load_config(file = options[:config] || default = defaults[:config])
170
- return unless file
171
-
172
- if ::File.readable?(file)
173
- @config = ::YAML.load_file(file)
174
- else
175
- quit "No such file: #{file}" unless default
176
- end
177
- end
178
-
179
- def merge_config(args = [config, defaults])
180
- args.each { |hash| hash && hash.each { |key, value|
181
- options[key] = value unless options.has_key?(key)
182
- } }
183
- end
184
-
185
- def parse_options(arguments)
186
- option_parser.parse!(arguments)
187
-
188
- load_config
189
- merge_config
190
- end
191
-
192
- def option_parser
193
- ::OptionParser.new { |opts|
194
- opts.banner = usage
195
-
196
- pre_opts(opts)
197
-
198
- opts.separator ''
199
- opts.separator 'Options:'
200
-
201
- opts(opts)
202
-
203
- opts.separator ''
204
- opts.separator 'Generic options:'
205
-
206
- generic_opts(opts)
207
- post_opts(opts)
208
- }.extend(Util::CLI::OptionParserExtension)
209
- end
210
-
211
- def pre_opts(opts)
212
- end
213
-
214
- def opts(opts)
215
- end
216
-
217
- def generic_opts(opts)
218
- opts.on('-h', '--help', 'Print this help message and exit') {
219
- shut opts
220
- }
221
-
222
- opts.on('--version', 'Print program version and exit') {
223
- shut "#{progname} v#{version}"
224
- }
225
- end
226
-
227
- def post_opts(opts)
228
- end
229
-
230
- module OptionParserExtension
231
-
232
- KEY_POOL = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
233
-
234
- def keys
235
- { :used => keys = top.short.keys, :free => KEY_POOL - keys }
236
- end
237
-
238
- end
239
-
240
- end
241
-
242
- end
4
+ warn "#{__FILE__}: Util::CLI is deprecated, use Nuggets::CLI instead."
@@ -1,105 +1,4 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 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
- #++
1
+ require 'nuggets/content_type'
2
+ module Util; ContentType = ::Nuggets::ContentType; end
27
3
 
28
- begin
29
- require 'rubygems'
30
- rescue ::LoadError
31
- end
32
-
33
- begin
34
- require 'filemagic/ext'
35
- rescue ::LoadError
36
- def File.content_type(path) # :nodoc:
37
- nil
38
- end
39
- end
40
-
41
- begin
42
- require 'nuggets/uri/content_type'
43
- rescue ::LoadError
44
- module URI
45
- def self.content_type(path) # :nodoc:
46
- nil
47
- end
48
- end
49
- end
50
-
51
- begin
52
- require 'mime/types'
53
- rescue ::LoadError
54
- module MIME # :nodoc:
55
- class Types # :nodoc:
56
- def self.of(path)
57
- []
58
- end
59
- end
60
- end
61
- end
62
-
63
- module Util
64
-
65
- module ContentType
66
-
67
- extend self
68
-
69
- # call-seq:
70
- # ContentType.of(path) => aString or +nil+
71
- #
72
- # Get the MIME-Type of the file living at +path+. Either by looking
73
- # directly into the file (requires FileMagic), or, assuming +path+
74
- # might denote a URI, by asking the web server (via OpenURI), or
75
- # finally by just looking at the file extension (requires MIME::Types).
76
- # Returns +nil+ in case no decision could be made.
77
- #
78
- # NOTE: This is really only useful with the filemagic and mime-types gems
79
- # installed.
80
- def of(path)
81
- ::File.content_type(path) || ::URI.content_type(path) || (
82
- t = ::MIME::Types.of(path).first and t.content_type
83
- )
84
- end
85
-
86
- end
87
-
88
- end
89
-
90
- # Just a short-cut to make the code read nicer...
91
- ContentType = ::Util::ContentType
92
-
93
- if $0 == __FILE__
94
- [
95
- __FILE__,
96
- 'bla/blub.jpg',
97
- 'bla/blub.blob',
98
- 'http://www.google.de',
99
- 'http://blackwinter.de/misc/ww.png',
100
- 'http://blackwinter.de/misc/ww.jpg',
101
- 'http://blackwinter.de/bla/blub.blob'
102
- ].each { |f|
103
- p [f, ::ContentType.of(f)]
104
- }
105
- end
4
+ warn "#{__FILE__}: Util::ContentType is deprecated, use Nuggets::ContentType instead."
@@ -1,78 +1,4 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 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
- #++
1
+ require 'nuggets/dotted_decimal'
2
+ module Util; DottedDecimal = ::Nuggets::DottedDecimal; end
27
3
 
28
- require 'nuggets/integer/to_binary_s'
29
-
30
- class Integer
31
-
32
- # call-seq:
33
- # int.to_dotted_decimal => aString
34
- #
35
- # Converts _int_ to dotted-decimal notation.
36
- def to_dotted_decimal
37
- to_binary_s(32).unpack('a8' * 4).map { |s| s.to_i(2) }.join('.')
38
- end
39
-
40
- end
41
-
42
- class String
43
-
44
- # call-seq:
45
- # str.from_dotted_decimal => anInteger
46
- #
47
- # Converts _str_ from dotted-decimal notation to integer.
48
- def from_dotted_decimal
49
- split('.').map { |i| i.to_i.to_binary_s(8) }.join.to_i(2)
50
- end
51
-
52
- end
53
-
54
- class Array
55
-
56
- def sort_by_dotted_decimal
57
- sort_by { |i| i.split('.').map { |j| j.to_i } }
58
- end
59
-
60
- end
61
-
62
- if $0 == __FILE__
63
- [2294967042, 4294967040].each { |i|
64
- p i.to_binary_s(32)
65
- p i.to_dotted_decimal
66
- }
67
-
68
- puts '#' * 34
69
-
70
- %w[77.47.161.3 196.101.53.1].each { |s|
71
- p s
72
- p s.from_dotted_decimal.to_binary_s(32)
73
- }
74
-
75
- a = %w[77.47.161.3 196.101.53.1 77.47.161.11]
76
- p a.sort
77
- p a.sort_by_dotted_decimal
78
- end
4
+ warn "#{__FILE__}: Util::DottedDecimal is deprecated, use Nuggets::DottedDecimal instead."
@@ -1,167 +1,4 @@
1
- # encoding: utf-8
1
+ require 'nuggets/i18n'
2
+ module Util; I18n = ::Nuggets::I18n; end
2
3
 
3
- #--
4
- ###############################################################################
5
- # #
6
- # A component of ruby-nuggets, some extensions to the Ruby programming #
7
- # language. #
8
- # #
9
- # Copyright (C) 2007-2011 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 Util
31
-
32
- module I18n
33
-
34
- DIACRITICS = {
35
- 'À' => 'A', # LATIN CAPITAL LETTER A WITH GRAVE
36
- 'Á' => 'A', # LATIN CAPITAL LETTER A WITH ACUTE
37
- 'Â' => 'A', # LATIN CAPITAL LETTER A WITH CIRCUMFLEX
38
- 'Ã' => 'A', # LATIN CAPITAL LETTER A WITH TILDE
39
- 'Ä' => 'AE', # LATIN CAPITAL LETTER A WITH DIAERESIS
40
- 'Å' => 'A', # LATIN CAPITAL LETTER A WITH RING ABOVE
41
- 'Æ' => 'AE', # LATIN CAPITAL LETTER AE
42
- 'Ç' => 'C', # LATIN CAPITAL LETTER C WITH CEDILLA
43
- 'È' => 'E', # LATIN CAPITAL LETTER E WITH GRAVE
44
- 'É' => 'E', # LATIN CAPITAL LETTER E WITH ACUTE
45
- 'Ê' => 'E', # LATIN CAPITAL LETTER E WITH CIRCUMFLEX
46
- 'Ë' => 'E', # LATIN CAPITAL LETTER E WITH DIAERESIS
47
- 'Ì' => 'I', # LATIN CAPITAL LETTER I WITH GRAVE
48
- 'Í' => 'I', # LATIN CAPITAL LETTER I WITH ACUTE
49
- 'Î' => 'I', # LATIN CAPITAL LETTER I WITH CIRCUMFLEX
50
- 'Ï' => 'I', # LATIN CAPITAL LETTER I WITH DIAERESIS
51
- 'Ð' => 'DH', # LATIN CAPITAL LETTER ETH
52
- 'Ñ' => 'N', # LATIN CAPITAL LETTER N WITH TILDE
53
- 'Ò' => 'O', # LATIN CAPITAL LETTER O WITH GRAVE
54
- 'Ó' => 'O', # LATIN CAPITAL LETTER O WITH ACUTE
55
- 'Ô' => 'O', # LATIN CAPITAL LETTER O WITH CIRCUMFLEX
56
- 'Õ' => 'O', # LATIN CAPITAL LETTER O WITH TILDE
57
- 'Ö' => 'OE', # LATIN CAPITAL LETTER O WITH DIAERESIS
58
- 'Ø' => 'O', # LATIN CAPITAL LETTER O WITH STROKE
59
- 'Ù' => 'U', # LATIN CAPITAL LETTER U WITH GRAVE
60
- 'Ú' => 'U', # LATIN CAPITAL LETTER U WITH ACUTE
61
- 'Û' => 'U', # LATIN CAPITAL LETTER U WITH CIRCUMFLEX
62
- 'Ü' => 'UE', # LATIN CAPITAL LETTER U WITH DIAERESIS
63
- 'Ý' => 'Y', # LATIN CAPITAL LETTER Y WITH ACUTE
64
- 'Þ' => 'TH', # LATIN CAPITAL LETTER THORN
65
- 'ß' => 'ss', # LATIN SMALL LETTER SHARP S
66
- 'à' => 'a', # LATIN SMALL LETTER A WITH GRAVE
67
- 'á' => 'a', # LATIN SMALL LETTER A WITH ACUTE
68
- 'â' => 'a', # LATIN SMALL LETTER A WITH CIRCUMFLEX
69
- 'ã' => 'a', # LATIN SMALL LETTER A WITH TILDE
70
- 'ä' => 'ae', # LATIN SMALL LETTER A WITH DIAERESIS
71
- 'å' => 'a', # LATIN SMALL LETTER A WITH RING ABOVE
72
- 'æ' => 'ae', # LATIN SMALL LETTER AE
73
- 'ç' => 'c', # LATIN SMALL LETTER C WITH CEDILLA
74
- 'è' => 'e', # LATIN SMALL LETTER E WITH GRAVE
75
- 'é' => 'e', # LATIN SMALL LETTER E WITH ACUTE
76
- 'ê' => 'e', # LATIN SMALL LETTER E WITH CIRCUMFLEX
77
- 'ë' => 'e', # LATIN SMALL LETTER E WITH DIAERESIS
78
- 'ì' => 'i', # LATIN SMALL LETTER I WITH GRAVE
79
- 'í' => 'i', # LATIN SMALL LETTER I WITH ACUTE
80
- 'î' => 'i', # LATIN SMALL LETTER I WITH CIRCUMFLEX
81
- 'ï' => 'i', # LATIN SMALL LETTER I WITH DIAERESIS
82
- 'ð' => 'dh', # LATIN SMALL LETTER ETH
83
- 'ñ' => 'n', # LATIN SMALL LETTER N WITH TILDE
84
- 'ò' => 'o', # LATIN SMALL LETTER O WITH GRAVE
85
- 'ó' => 'o', # LATIN SMALL LETTER O WITH ACUTE
86
- 'ô' => 'o', # LATIN SMALL LETTER O WITH CIRCUMFLEX
87
- 'õ' => 'o', # LATIN SMALL LETTER O WITH TILDE
88
- 'ö' => 'oe', # LATIN SMALL LETTER O WITH DIAERESIS
89
- 'ø' => 'o', # LATIN SMALL LETTER O WITH STROKE
90
- 'ù' => 'u', # LATIN SMALL LETTER U WITH GRAVE
91
- 'ú' => 'u', # LATIN SMALL LETTER U WITH ACUTE
92
- 'û' => 'u', # LATIN SMALL LETTER U WITH CIRCUMFLEX
93
- 'ü' => 'ue', # LATIN SMALL LETTER U WITH DIAERESIS
94
- 'ý' => 'y', # LATIN SMALL LETTER Y WITH ACUTE
95
- 'þ' => 'th', # LATIN SMALL LETTER THORN
96
- 'ÿ' => 'y' # LATIN SMALL LETTER Y WITH DIAERESIS
97
- }
98
-
99
- def self.args_for_map_diacritics
100
- @args_for_map_diacritics ||= begin
101
- map = ::Hash.new { |h, k| h[k] = [] }
102
-
103
- DIACRITICS.each { |a| a.each { |i| map[i].concat(a) } }
104
- map.each { |k, v| v.uniq!; map[k] = "(#{::Regexp.union(*v).source})" }
105
-
106
- [::Regexp.union(*map.keys.sort_by { |k| -k.length }), map.method(:[])]
107
- end
108
- end
109
-
110
- end
111
-
112
- end
113
-
114
- class String
115
-
116
- # call-seq:
117
- # str.replace_diacritics => new_str
118
- #
119
- # Substitutes any diacritics in _str_ with their replacements as per
120
- # Util::I18n::DIACRITICS.
121
- def replace_diacritics
122
- (_dup = dup).replace_diacritics! || _dup
123
- end
124
-
125
- # call-seq:
126
- # str.replace_diacritics! => str or +nil+
127
- #
128
- # Destructive version of #replace_diacritics.
129
- def replace_diacritics!
130
- diacritics = ::Util::I18n::DIACRITICS
131
-
132
- gsub!(/#{::Regexp.union(*diacritics.keys)}/) { |m|
133
- s = diacritics[m]
134
-
135
- # Try to adjust case:
136
- # 'Äh' => 'AEh' => 'Aeh'
137
- #
138
- # But:
139
- # 'SÖS' => 'SOES' (not 'SOeS'!)
140
- if s.length > 1
141
- t = $'[0..0]
142
- s[1..-1] = s[1..-1].downcase if t == t.downcase
143
- end
144
-
145
- s
146
- }
147
- end
148
-
149
- def map_diacritics
150
- (_dup = dup).map_diacritics! || _dup
151
- end
152
-
153
- def map_diacritics!
154
- re, block = ::Util::I18n.args_for_map_diacritics
155
- gsub!(re, &block)
156
- end
157
-
158
- end
159
-
160
- if $0 == __FILE__
161
- s = 'Äh, Rüby iß sö cüül, nö? SÖS!'
162
- p s
163
- p s.replace_diacritics
164
-
165
- s.replace_diacritics!
166
- p s
167
- end
4
+ warn "#{__FILE__}: Util::I18n is deprecated, use Nuggets::I18n instead."
@@ -1,45 +1,4 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2012 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
- #++
1
+ require 'nuggets/lazy_attr'
2
+ module Util; LazyAttr = ::Nuggets::LazyAttr; end
27
3
 
28
- module Util
29
-
30
- module LazyAttr
31
-
32
- private
33
-
34
- def lazy_attr(attr, freeze = true)
35
- class << self; self; end.class_eval { attr_reader attr }
36
-
37
- value = instance_variable_get(name = "@#{attr}") ||
38
- instance_variable_set(name, yield)
39
-
40
- freeze ? value.freeze : value
41
- end
42
-
43
- end
44
-
45
- end
4
+ warn "#{__FILE__}: Util::LazyAttr is deprecated, use Nuggets::LazyAttr instead."