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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60dcd32240040bca4933e703e41b095fc72b7376
4
- data.tar.gz: 7ab48f180ba23d603d00ad264f63d3cfa65e1eff
3
+ metadata.gz: 253d8874e5b94ce688f3b97a0e41a52a0397e2bb
4
+ data.tar.gz: 7901cb5cfc2138f3b3fe22c692b7ecb74299692e
5
5
  SHA512:
6
- metadata.gz: 1759b4701eb80e6a041bf14d299bf0ac728708f6a36414258ef8f3228ae823f2827fa0225e28340b6ec132d077b0bb5db42ab6dd7d9361d0bcfa11acfeac9efe
7
- data.tar.gz: 4c1431a95d67864b0b7cf9ce0095eadd0f28ecdce6dcc90125a630f1dc914b83c01fa0646eaa36bab5774872b57a0b11e6b15351bc886dae37c70bc209c02037
6
+ metadata.gz: d5630b19a59b8a25f4421b8525f419bdc5c98807358074f5f665c934ec96e2eccc8ead5277a1f0d23b321e273ab68c33911aaac1e08d2d2c9828203c6497ba32
7
+ data.tar.gz: 520a5f92d9011a4ea1089315f0109ba002e2379320786b0d6332092e9ffe23847351489c6749f81fecff4a0b5fff3cec0ffcc9de12176632ea2c436bd6bbf28c
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.9.1
5
+ This documentation refers to ruby-nuggets version 0.9.2
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -34,10 +34,9 @@ cause other libraries to misbehave. Use at your own risk!
34
34
  == LINKS
35
35
 
36
36
  <b></b>
37
- Documentation:: http://prometheus.rubyforge.org/ruby-nuggets
38
- Source code:: http://github.com/blackwinter/ruby-nuggets
39
- RubyForge project:: http://rubyforge.org/projects/prometheus
40
- RubyGem:: http://rubygems.org/gems/ruby-nuggets
37
+ Documentation:: http://blackwinter.github.com/ruby-nuggets
38
+ Source code:: http://github.com/blackwinter/ruby-nuggets
39
+ RubyGem:: http://rubygems.org/gems/ruby-nuggets
41
40
 
42
41
 
43
42
  == AUTHORS
data/Rakefile CHANGED
@@ -4,16 +4,14 @@ begin
4
4
  require 'hen'
5
5
 
6
6
  Hen.lay! {{
7
- :rubyforge => {
8
- :project => %q{prometheus},
9
- :package => %q{ruby-nuggets}
10
- },
11
-
12
7
  :gem => {
13
- :version => Nuggets::VERSION,
14
- :summary => %q{Some extensions to the Ruby programming language.},
15
- :author => %q{Jens Wille},
16
- :email => %q{jens.wille@gmail.com}
8
+ :name => %q{ruby-nuggets},
9
+ :version => Nuggets::VERSION,
10
+ :summary => %q{Some extensions to the Ruby programming language.},
11
+ :author => %q{Jens Wille},
12
+ :email => %q{jens.wille@gmail.com},
13
+ :homepage => :blackwinter,
14
+ :dependencies => %w[]
17
15
  }
18
16
  }}
19
17
  rescue LoadError => err
@@ -0,0 +1,125 @@
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
+ #++
27
+
28
+ module Nuggets
29
+ module ANSIColor2CSS
30
+
31
+ extend self
32
+
33
+ ATTRIBUTES = {
34
+ '0' => nil, # clear
35
+ '1' => 'font-weight: bold', # bold
36
+ '2' => '', # dark
37
+ '3' => 'font-style: italic', # italic -- not widely implemented
38
+ '4' => 'text-decoration: underline', # underline
39
+ '5' => 'text-decoration: blink', # blink
40
+ '6' => 'text-decoration: blink', # rapid blink -- not widely implemented
41
+ '7' => '', # reverse
42
+ '8' => 'opacity: 0', # concealed
43
+ '9' => 'text-decoration: line-through', # strikethrough -- not widely implemented
44
+ '22' => 'font-weight: normal', # bold off
45
+ '23' => 'font-style: normal', # italic off
46
+ '24' => 'text-decoration: none', # underline off
47
+ '25' => 'text-decoration: none', # blink off
48
+ '27' => '', # reverse off
49
+ '28' => 'opacity: 1', # concealed off
50
+ '29' => 'text-decoration: none', # strikethrough off
51
+ '30' => 'color: black', # black
52
+ '31' => 'color: maroon', # red
53
+ '32' => 'color: green', # green
54
+ '33' => 'color: olive', # yellow
55
+ '34' => 'color: navy', # blue
56
+ '35' => 'color: purple', # magenta
57
+ '36' => 'color: teal', # cyan
58
+ '37' => 'color: silver', # white
59
+ '39' => 'color: silver', # default (white)
60
+ '40' => 'background-color: black', # on black
61
+ '41' => 'background-color: maroon', # on red
62
+ '42' => 'background-color: green', # on green
63
+ '43' => 'background-color: olive', # on yellow
64
+ '44' => 'background-color: navy', # on blue
65
+ '45' => 'background-color: purple', # on magenta
66
+ '46' => 'background-color: teal', # on cyan
67
+ '47' => 'background-color: silver', # on white
68
+ '49' => 'background-color: black', # on default (black)
69
+ '90' => 'color: gray', # bright black
70
+ '91' => 'color: red', # bright red
71
+ '92' => 'color: lime', # bright green
72
+ '93' => 'color: yellow', # bright yellow
73
+ '94' => 'color: blue', # bright blue
74
+ '95' => 'color: fuchsia', # bright magenta
75
+ '96' => 'color: cyan', # bright cyan
76
+ '97' => 'color: white', # bright white
77
+ '100' => 'background-color: gray', # on bright black
78
+ '101' => 'background-color: red', # on bright red
79
+ '102' => 'background-color: lime', # on bright green
80
+ '103' => 'background-color: yellow', # on bright yellow
81
+ '104' => 'background-color: blue', # on bright blue
82
+ '105' => 'background-color: fuchsia', # on bright magenta
83
+ '106' => 'background-color: cyan', # on bright cyan
84
+ '107' => 'background-color: white' # on bright white
85
+ }
86
+
87
+ ATTRIBUTES_RE = ::Regexp.union(*ATTRIBUTES.keys)
88
+
89
+ DELIMITER = ';'
90
+
91
+ COLOR_RE = %r{
92
+ \e \[ ( #{ATTRIBUTES_RE} (?: #{DELIMITER} #{ATTRIBUTES_RE} )* ) m
93
+ }x
94
+
95
+ STYLE = '<span style="%s">'
96
+ CLEAR = '</span>'
97
+
98
+ def convert(string)
99
+ string.gsub(COLOR_RE) { format($1.split(DELIMITER).uniq) }
100
+ end
101
+
102
+ def format(attributes)
103
+ "#{clear(attributes)}#{style(attributes) if attributes.any?}"
104
+ end
105
+
106
+ def clear(attributes)
107
+ CLEAR if attributes.delete('0')
108
+ end
109
+
110
+ def style(attributes)
111
+ STYLE % ATTRIBUTES.values_at(*attributes).join('; ')
112
+ end
113
+
114
+ end
115
+ end
116
+
117
+ class String
118
+
119
+ def ansicolor2css
120
+ ::Nuggets::ANSIColor2CSS.convert(self)
121
+ end
122
+
123
+ alias_method :ansicolour2css, :ansicolor2css
124
+
125
+ end
@@ -0,0 +1,240 @@
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
+ #++
27
+
28
+ require 'optparse'
29
+ require 'yaml'
30
+ require 'zlib'
31
+ require 'highline'
32
+
33
+ module Nuggets
34
+ class CLI
35
+
36
+ class << self
37
+
38
+ def usage(prog)
39
+ "Usage: #{prog} [-h|--help] [options]"
40
+ end
41
+
42
+ def version
43
+ parent_const_get(:VERSION)
44
+ end
45
+
46
+ def defaults
47
+ {}
48
+ end
49
+
50
+ def execute(*args)
51
+ new.execute(*args)
52
+ end
53
+
54
+ private
55
+
56
+ def parent_const_get(const, range = 0...-1)
57
+ name.split('::').inject([::Object]) { |memo, name|
58
+ memo << memo.last.const_get(name)
59
+ }.reverse[range].each { |mod|
60
+ return mod.const_get(const) if mod.const_defined?(const)
61
+ }
62
+
63
+ raise ::NameError, "uninitialized constant #{self}::#{const}"
64
+ end
65
+
66
+ end
67
+
68
+ attr_reader :options, :config, :defaults
69
+ attr_reader :stdin, :stdout, :stderr
70
+
71
+ attr_accessor :prog
72
+
73
+ def initialize(defaults = nil, *args)
74
+ @defaults, @prog = defaults || self.class.defaults, $0
75
+
76
+ init(*args)
77
+
78
+ # prevent backtrace on ^C
79
+ trap(:INT) { exit 130 }
80
+ end
81
+
82
+ def progname
83
+ ::File.basename(prog)
84
+ end
85
+
86
+ def usage
87
+ self.class.usage(prog)
88
+ end
89
+
90
+ def version
91
+ self.class.version
92
+ end
93
+
94
+ def execute(arguments = ::ARGV, *inouterr)
95
+ reset(*inouterr)
96
+ parse_options(arguments)
97
+ run(arguments)
98
+ rescue => err
99
+ raise if $VERBOSE
100
+ abort "#{err.backtrace.first}: #{err} (#{err.class})"
101
+ ensure
102
+ options.each_value { |value|
103
+ value.close if value.is_a?(::Zlib::GzipWriter)
104
+ }
105
+ end
106
+
107
+ def reset(stdin = ::STDIN, stdout = ::STDOUT, stderr = ::STDERR)
108
+ @stdin, @stdout, @stderr = stdin, stdout, stderr
109
+ @options, @config = {}, {}
110
+ end
111
+
112
+ private
113
+
114
+ def init(*args)
115
+ reset
116
+ end
117
+
118
+ def ask(question, &block)
119
+ ::HighLine.new(stdin, stdout).ask(question, &block)
120
+ end
121
+
122
+ def puts(*msg)
123
+ stdout.puts(*msg)
124
+ end
125
+
126
+ def warn(*msg)
127
+ stderr.puts(*msg)
128
+ end
129
+
130
+ def quit(msg = nil, include_usage = msg != false)
131
+ out = []
132
+
133
+ out << "#{progname}: #{msg}" if msg
134
+ out << usage if include_usage
135
+
136
+ abort out.any? && out.join("\n\n")
137
+ end
138
+
139
+ def abort(msg = nil, status = 1)
140
+ warn(msg) if msg
141
+ exit(status)
142
+ end
143
+
144
+ def shut(msg = nil, status = 0)
145
+ puts(msg) if msg
146
+ exit(status)
147
+ end
148
+
149
+ def exit(status = 0)
150
+ ::Kernel.exit(status)
151
+ end
152
+
153
+ def open_file_or_std(file, write = false)
154
+ if file == '-'
155
+ write ? stdout : stdin
156
+ else
157
+ gz = file =~ /\.gz\z/i
158
+
159
+ if write
160
+ gz ? ::Zlib::GzipWriter.open(file) : ::File.open(file, 'w')
161
+ else
162
+ quit "No such file: #{file}" unless ::File.readable?(file)
163
+ (gz ? ::Zlib::GzipReader : ::File).open(file)
164
+ end
165
+ end
166
+ end
167
+
168
+ def load_config(file = options[:config] || default = defaults[:config])
169
+ return unless file
170
+
171
+ if ::File.readable?(file)
172
+ @config = ::YAML.load_file(file)
173
+ else
174
+ quit "No such file: #{file}" unless default
175
+ end
176
+ end
177
+
178
+ def merge_config(args = [config, defaults])
179
+ args.each { |hash| hash && hash.each { |key, value|
180
+ options[key] = value unless options.has_key?(key)
181
+ } }
182
+ end
183
+
184
+ def parse_options(arguments)
185
+ option_parser.parse!(arguments)
186
+
187
+ load_config
188
+ merge_config
189
+ end
190
+
191
+ def option_parser
192
+ ::OptionParser.new { |opts|
193
+ opts.banner = usage
194
+
195
+ pre_opts(opts)
196
+
197
+ opts.separator ''
198
+ opts.separator 'Options:'
199
+
200
+ opts(opts)
201
+
202
+ opts.separator ''
203
+ opts.separator 'Generic options:'
204
+
205
+ generic_opts(opts)
206
+ post_opts(opts)
207
+ }.extend(Nuggets::CLI::OptionParserExtension)
208
+ end
209
+
210
+ def pre_opts(opts)
211
+ end
212
+
213
+ def opts(opts)
214
+ end
215
+
216
+ def generic_opts(opts)
217
+ opts.on('-h', '--help', 'Print this help message and exit') {
218
+ shut opts
219
+ }
220
+
221
+ opts.on('--version', 'Print program version and exit') {
222
+ shut "#{progname} v#{version}"
223
+ }
224
+ end
225
+
226
+ def post_opts(opts)
227
+ end
228
+
229
+ module OptionParserExtension
230
+
231
+ KEY_POOL = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
232
+
233
+ def keys
234
+ { :used => keys = top.short.keys, :free => KEY_POOL - keys }
235
+ end
236
+
237
+ end
238
+
239
+ end
240
+ end
@@ -0,0 +1,103 @@
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
+ #++
27
+
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 Nuggets
64
+ module ContentType
65
+
66
+ extend self
67
+
68
+ # call-seq:
69
+ # ContentType.of(path) => aString or +nil+
70
+ #
71
+ # Get the MIME-Type of the file living at +path+. Either by looking
72
+ # directly into the file (requires FileMagic), or, assuming +path+
73
+ # might denote a URI, by asking the web server (via OpenURI), or
74
+ # finally by just looking at the file extension (requires MIME::Types).
75
+ # Returns +nil+ in case no decision could be made.
76
+ #
77
+ # NOTE: This is really only useful with the filemagic and mime-types gems
78
+ # installed.
79
+ def of(path)
80
+ ::File.content_type(path) || ::URI.content_type(path) || (
81
+ t = ::MIME::Types.of(path).first and t.content_type
82
+ )
83
+ end
84
+
85
+ end
86
+ end
87
+
88
+ # Just a short-cut to make the code read nicer...
89
+ ContentType = ::Nuggets::ContentType
90
+
91
+ if $0 == __FILE__
92
+ [
93
+ __FILE__,
94
+ 'bla/blub.jpg',
95
+ 'bla/blub.blob',
96
+ 'http://www.google.de',
97
+ 'http://blackwinter.de/misc/ww.png',
98
+ 'http://blackwinter.de/misc/ww.jpg',
99
+ 'http://blackwinter.de/bla/blub.blob'
100
+ ].each { |f|
101
+ p [f, ::ContentType.of(f)]
102
+ }
103
+ end
@@ -0,0 +1,78 @@
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
+ #++
27
+
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