ruby-nuggets 0.7.4 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/README +1 -1
  2. data/lib/nuggets/all.rb +4 -4
  3. data/lib/nuggets/all_mixins.rb +4 -4
  4. data/lib/nuggets/array/flatten_once.rb +1 -1
  5. data/lib/nuggets/array/format.rb +3 -3
  6. data/lib/nuggets/array/only.rb +2 -2
  7. data/lib/nuggets/array/rand.rb +1 -1
  8. data/lib/nuggets/array/shuffle.rb +2 -2
  9. data/lib/nuggets/array/standard_deviation_mixin.rb +1 -1
  10. data/lib/nuggets/array/to_hash.rb +4 -4
  11. data/lib/nuggets/enumerable/agrep.rb +5 -5
  12. data/lib/nuggets/enumerable/all_any_extended.rb +10 -10
  13. data/lib/nuggets/enumerable/minmax.rb +2 -2
  14. data/lib/nuggets/env/set_mixin.rb +2 -2
  15. data/lib/nuggets/env/user_encoding_mixin.rb +3 -3
  16. data/lib/nuggets/env/user_home_mixin.rb +1 -1
  17. data/lib/nuggets/file/which_mixin.rb +2 -2
  18. data/lib/nuggets/hash/at.rb +1 -1
  19. data/lib/nuggets/hash/insert.rb +1 -1
  20. data/lib/nuggets/hash/nest_mixin.rb +1 -1
  21. data/lib/nuggets/hash/only.rb +2 -2
  22. data/lib/nuggets/integer/factorial.rb +1 -1
  23. data/lib/nuggets/io/agrep.rb +1 -1
  24. data/lib/nuggets/io/modes.rb +12 -12
  25. data/lib/nuggets/io/null_mixin.rb +1 -1
  26. data/lib/nuggets/net/success.rb +60 -0
  27. data/lib/nuggets/numeric/duration.rb +2 -2
  28. data/lib/nuggets/object/singleton_class_mixin.rb +5 -5
  29. data/lib/nuggets/string/evaluate_mixin.rb +1 -1
  30. data/lib/nuggets/string/msub.rb +5 -5
  31. data/lib/nuggets/string/nsub.rb +1 -1
  32. data/lib/nuggets/string/sub_with_md.rb +2 -2
  33. data/lib/nuggets/string/xor_mixin.rb +1 -1
  34. data/lib/nuggets/uri/content_type_mixin.rb +6 -5
  35. data/lib/nuggets/uri/exist_mixin.rb +12 -5
  36. data/lib/nuggets/util/ansicolor2css.rb +2 -2
  37. data/lib/nuggets/util/cli.rb +12 -12
  38. data/lib/nuggets/util/content_type.rb +9 -9
  39. data/lib/nuggets/util/i18n.rb +2 -2
  40. data/lib/nuggets/util/ruby.rb +29 -29
  41. data/lib/nuggets/version.rb +1 -1
  42. data/lib/nuggets.rb +8 -8
  43. metadata +133 -132
@@ -42,7 +42,7 @@ module Nuggets
42
42
 
43
43
  length = binary.map { |s| s.length }.inject { |a, b|
44
44
  if require_same_length
45
- a == b ? a : raise(ArgumentError, 'must be of same length')
45
+ a == b ? a : raise(::ArgumentError, 'must be of same length')
46
46
  else
47
47
  [a, b].max
48
48
  end
@@ -25,21 +25,22 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require 'socket'
29
- require 'open-uri'
28
+ require 'nuggets/uri/exist_mixin'
30
29
 
31
30
  module Nuggets
32
31
  module URI
33
32
  module ContentTypeMixin
34
33
 
34
+ def self.extended(base)
35
+ base.extend Nuggets::URI::ExistMixin
36
+ end
37
+
35
38
  # call-seq:
36
39
  # URI.content_type(uri) => aString or +nil+
37
40
  #
38
41
  # Return the content type of +uri+, or +nil+ if not found.
39
42
  def content_type(uri)
40
- open(uri.to_s).content_type
41
- rescue OpenURI::HTTPError, SocketError, Errno::ENOENT, Errno::EHOSTUNREACH, NoMethodError
42
- nil
43
+ exist?(uri) { |res| res.content_type }
43
44
  end
44
45
 
45
46
  end
@@ -25,7 +25,7 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require 'open-uri'
28
+ require 'nuggets/net/success'
29
29
 
30
30
  module Nuggets
31
31
  module URI
@@ -36,11 +36,18 @@ module Nuggets
36
36
  #
37
37
  # Return +true+ if the URI +uri+ exists.
38
38
  def exist?(uri)
39
- open(uri.to_s)
40
- true
41
- rescue OpenURI::HTTPError, SocketError, Errno::ENOENT
42
- false
39
+ uri = ::URI.parse(uri.to_s)
40
+ return unless uri.is_a?(::URI::HTTP)
41
+
42
+ path = uri.path
43
+ path = '/' if path.empty?
44
+
45
+ res = ::Net::HTTP.start(uri.host, uri.port) { |http| http.head(path) }
46
+
47
+ block_given? ? yield(res) : true if res.success?
48
+ rescue ::SocketError, ::Errno::EHOSTUNREACH
43
49
  end
50
+
44
51
  alias_method :exists?, :exist?
45
52
 
46
53
  end
@@ -85,7 +85,7 @@ module Util
85
85
  '107' => 'background-color: white' # on bright white
86
86
  }
87
87
 
88
- ATTRIBUTES_RE = Regexp.union(*ATTRIBUTES.keys)
88
+ ATTRIBUTES_RE = ::Regexp.union(*ATTRIBUTES.keys)
89
89
 
90
90
  DELIMITER = ';'
91
91
 
@@ -119,7 +119,7 @@ end
119
119
  class String
120
120
 
121
121
  def ansicolor2css
122
- Util::ANSIColor2CSS.convert(self)
122
+ ::Util::ANSIColor2CSS.convert(self)
123
123
  end
124
124
 
125
125
  alias_method :ansicolour2css, :ansicolor2css
@@ -55,7 +55,7 @@ module Util
55
55
  private
56
56
 
57
57
  def parent_const_get(const, range = 0...-1)
58
- name.split('::').inject([Object]) { |memo, name|
58
+ name.split('::').inject([::Object]) { |memo, name|
59
59
  memo << memo.last.const_get(name)
60
60
  }.reverse[range].each { |mod|
61
61
  return mod.const_get(const) if mod.const_defined?(const)
@@ -81,7 +81,7 @@ module Util
81
81
  end
82
82
 
83
83
  def progname
84
- File.basename(prog)
84
+ ::File.basename(prog)
85
85
  end
86
86
 
87
87
  def usage
@@ -92,7 +92,7 @@ module Util
92
92
  self.class.version
93
93
  end
94
94
 
95
- def execute(arguments = ARGV, *inouterr)
95
+ def execute(arguments = ::ARGV, *inouterr)
96
96
  reset(*inouterr)
97
97
  parse_options(arguments)
98
98
  run(arguments)
@@ -101,11 +101,11 @@ module Util
101
101
  abort "#{err.backtrace.first}: #{err} (#{err.class})"
102
102
  ensure
103
103
  options.each_value { |value|
104
- value.close if value.is_a?(Zlib::GzipWriter)
104
+ value.close if value.is_a?(::Zlib::GzipWriter)
105
105
  }
106
106
  end
107
107
 
108
- def reset(stdin = STDIN, stdout = STDOUT, stderr = STDERR)
108
+ def reset(stdin = ::STDIN, stdout = ::STDOUT, stderr = ::STDERR)
109
109
  @stdin, @stdout, @stderr = stdin, stdout, stderr
110
110
  @options, @config = {}, {}
111
111
  end
@@ -117,7 +117,7 @@ module Util
117
117
  end
118
118
 
119
119
  def ask(question, &block)
120
- HighLine.new(stdin, stdout).ask(question, &block)
120
+ ::HighLine.new(stdin, stdout).ask(question, &block)
121
121
  end
122
122
 
123
123
  def warn(msg)
@@ -139,7 +139,7 @@ module Util
139
139
  end
140
140
 
141
141
  def exit(status = 0)
142
- Kernel.exit(status)
142
+ ::Kernel.exit(status)
143
143
  end
144
144
 
145
145
  def open_file_or_std(file, write = false)
@@ -149,16 +149,16 @@ module Util
149
149
  gz = file =~ /\.gz\z/i
150
150
 
151
151
  if write
152
- gz ? Zlib::GzipWriter.open(file) : File.open(file, 'w')
152
+ gz ? ::Zlib::GzipWriter.open(file) : ::File.open(file, 'w')
153
153
  else
154
- quit "No such file: #{file}" unless File.readable?(file)
155
- (gz ? Zlib::GzipReader : File).open(file)
154
+ quit "No such file: #{file}" unless ::File.readable?(file)
155
+ (gz ? ::Zlib::GzipReader : ::File).open(file)
156
156
  end
157
157
  end
158
158
  end
159
159
 
160
160
  def load_config(file = options[:config] || defaults[:config])
161
- @config = YAML.load_file(file) if File.readable?(file)
161
+ @config = ::YAML.load_file(file) if ::File.readable?(file)
162
162
  end
163
163
 
164
164
  def merge_config(args = [config, defaults])
@@ -175,7 +175,7 @@ module Util
175
175
  end
176
176
 
177
177
  def option_parser
178
- OptionParser.new { |opts|
178
+ ::OptionParser.new { |opts|
179
179
  opts.banner = usage
180
180
 
181
181
  pre_opts(opts)
@@ -27,20 +27,20 @@
27
27
 
28
28
  begin
29
29
  require 'rubygems'
30
- rescue LoadError
30
+ rescue ::LoadError
31
31
  end
32
32
 
33
33
  begin
34
34
  require 'filemagic/ext'
35
- rescue LoadError
36
- def File.content_type(path) # :nodoc:
35
+ rescue ::LoadError
36
+ def ::File.content_type(path) # :nodoc:
37
37
  nil
38
38
  end
39
39
  end
40
40
 
41
41
  begin
42
42
  require 'nuggets/uri/content_type'
43
- rescue LoadError
43
+ rescue ::LoadError
44
44
  module URI
45
45
  def self.content_type(path) # :nodoc:
46
46
  nil
@@ -50,7 +50,7 @@ end
50
50
 
51
51
  begin
52
52
  require 'mime/types'
53
- rescue LoadError
53
+ rescue ::LoadError
54
54
  module MIME # :nodoc:
55
55
  class Types # :nodoc:
56
56
  def self.of(path)
@@ -78,8 +78,8 @@ module Util
78
78
  # NOTE: This is really only useful with the filemagic and mime-types gems
79
79
  # installed.
80
80
  def of(path)
81
- File.content_type(path) || URI.content_type(path) || (
82
- t = MIME::Types.of(path).first and t.content_type
81
+ ::File.content_type(path) || ::URI.content_type(path) || (
82
+ t = ::MIME::Types.of(path).first and t.content_type
83
83
  )
84
84
  end
85
85
 
@@ -88,7 +88,7 @@ module Util
88
88
  end
89
89
 
90
90
  # Just a short-cut to make the code read nicer...
91
- ContentType = Util::ContentType
91
+ ContentType = ::Util::ContentType
92
92
 
93
93
  if $0 == __FILE__
94
94
  [
@@ -100,6 +100,6 @@ if $0 == __FILE__
100
100
  'http://blackwinter.de/misc/ww.jpg',
101
101
  'http://blackwinter.de/bla/blub.blob'
102
102
  ].each { |f|
103
- p [f, ContentType.of(f)]
103
+ p [f, ::ContentType.of(f)]
104
104
  }
105
105
  end
@@ -116,8 +116,8 @@ class String
116
116
  #
117
117
  # Destructive version of #replace_diacritics.
118
118
  def replace_diacritics!
119
- gsub!(/#{Regexp.union(*Util::I18n::DIACRITICS.keys)}/) { |m|
120
- s = Util::I18n::DIACRITICS[m]
119
+ gsub!(/#{::Regexp.union(*::Util::I18n::DIACRITICS.keys)}/) { |m|
120
+ s = ::Util::I18n::DIACRITICS[m]
121
121
 
122
122
  # Try to adjust case:
123
123
  # 'Äh' => 'AEh' => 'Aeh'
@@ -53,11 +53,11 @@ module Util
53
53
 
54
54
  extend self
55
55
 
56
- CONFIG = RbConfig::CONFIG
56
+ CONFIG = ::RbConfig::CONFIG
57
57
 
58
58
  # Store original $GEM_HOME value so that even if the app customizes
59
59
  # $GEM_HOME we can still work with the original value.
60
- if gem_home = ENV['GEM_HOME']
60
+ if gem_home = ::ENV['GEM_HOME']
61
61
  gem_home = gem_home.strip.freeze
62
62
  gem_home = nil if gem_home.empty?
63
63
  end
@@ -79,10 +79,10 @@ module Util
79
79
  return @ruby_command = ruby_executable unless rvm?
80
80
 
81
81
  if name = rvm_ruby_string and dir = rvm_path
82
- if File.exist?(filename = File.join(dir, 'wrappers', name, 'ruby'))
82
+ if ::File.exist?(filename = ::File.join(dir, 'wrappers', name, 'ruby'))
83
83
  # Old wrapper scripts reference $HOME which causes
84
84
  # things to blow up when run by a different user.
85
- return @ruby_command = filename unless File.read(filename).include?('$HOME')
85
+ return @ruby_command = filename unless ::File.read(filename).include?('$HOME')
86
86
  end
87
87
 
88
88
  abort 'Your RVM wrapper scripts are too old. ' << UPDATE_RVM
@@ -103,7 +103,7 @@ module Util
103
103
  def ruby_executable
104
104
  @ruby_executable ||= begin
105
105
  dir, name, ext = CONFIG.values_at(*%w[bindir RUBY_INSTALL_NAME EXEEXT])
106
- File.join(dir, name + ext).sub(/.*\s.*/m, '"\&"')
106
+ ::File.join(dir, name + ext).sub(/.*\s.*/m, '"\&"')
107
107
  end
108
108
  end
109
109
 
@@ -111,7 +111,7 @@ module Util
111
111
  def ruby_supports_fork?
112
112
  # MRI >= 1.9.2's respond_to? returns false
113
113
  # for methods that are not implemented.
114
- Process.respond_to?(:fork) &&
114
+ ::Process.respond_to?(:fork) &&
115
115
  RUBY_ENGINE != 'jruby' &&
116
116
  RUBY_ENGINE != 'macruby' &&
117
117
  CONFIG['target_os'] !~ /mswin|windows|mingw/
@@ -130,9 +130,9 @@ module Util
130
130
 
131
131
  return @rvm_path = nil unless rvm?
132
132
 
133
- [ENV['rvm_path'], '~/.rvm', '/usr/local/rvm'].compact.each { |path|
134
- path = File.expand_path(path)
135
- return @rvm_path = path if File.directory?(path)
133
+ [::ENV['rvm_path'], '~/.rvm', '/usr/local/rvm'].compact.each { |path|
134
+ path = ::File.expand_path(path)
135
+ return @rvm_path = path if ::File.directory?(path)
136
136
  }
137
137
 
138
138
  # Failure to locate the RVM path is probably caused by the
@@ -165,7 +165,7 @@ module Util
165
165
  # try various strategies...
166
166
 
167
167
  # $GEM_HOME usually contains the gem set name.
168
- return @rvm_ruby_string = File.basename(GEM_HOME) if GEM_HOME && GEM_HOME.include?('rvm/gems/')
168
+ return @rvm_ruby_string = ::File.basename(GEM_HOME) if GEM_HOME && GEM_HOME.include?('rvm/gems/')
169
169
 
170
170
  # User somehow managed to nuke $GEM_HOME. Extract info from $LOAD_PATH.
171
171
  $LOAD_PATH.each { |path| return @rvm_ruby_string = $1 if path =~ %r{^.*rvm/gems/([^/]+)} }
@@ -246,7 +246,7 @@ module Util
246
246
  args.pop.each { |key, val|
247
247
  opt = "-#{key.to_s[0, 1]}"
248
248
 
249
- if val.is_a?(Array)
249
+ if val.is_a?(::Array)
250
250
  val.each { |wal|
251
251
  argv << opt << wal.to_s
252
252
  }
@@ -257,7 +257,7 @@ module Util
257
257
  argv << "#{opt}#{val unless val == true}"
258
258
  end
259
259
  end
260
- } if args.last.is_a?(Hash)
260
+ } if args.last.is_a?(::Hash)
261
261
 
262
262
  argv.concat(args.map! { |arg| arg.to_s.strip })
263
263
  end
@@ -265,36 +265,36 @@ module Util
265
265
  private
266
266
 
267
267
  def locate_ruby_tool_by_basename(name)
268
- dir = if RUBY_PLATFORM =~ /darwin/ && ruby_command =~ OSX_RUBY_RE
268
+ dir = if ::RUBY_PLATFORM =~ /darwin/ && ruby_command =~ OSX_RUBY_RE
269
269
  # On OS X we must look for Ruby binaries in /usr/bin.
270
270
  # RubyGems puts executables (e.g. 'rake') in there, not in
271
271
  # /System/Libraries/(...)/bin.
272
272
  '/usr/bin'
273
273
  else
274
- File.dirname(ruby_command)
274
+ ::File.dirname(ruby_command)
275
275
  end
276
276
 
277
- filename = File.join(dir, name)
278
- return filename if File.file?(filename) && File.executable?(filename)
277
+ filename = ::File.join(dir, name)
278
+ return filename if ::File.file?(filename) && ::File.executable?(filename)
279
279
 
280
280
  # RubyGems might put binaries in a directory other
281
281
  # than Ruby's bindir. Debian packaged RubyGems and
282
282
  # DebGem packaged RubyGems are the prime examples.
283
283
  begin
284
- require 'rubygems' unless defined?(Gem)
284
+ require 'rubygems' unless defined?(::Gem)
285
285
 
286
- filename = File.join(Gem.bindir, name)
287
- return filename if File.file?(filename) && File.executable?(filename)
288
- rescue LoadError
286
+ filename = ::File.join(::Gem.bindir, name)
287
+ return filename if ::File.file?(filename) && ::File.executable?(filename)
288
+ rescue ::LoadError
289
289
  end
290
290
 
291
291
  # Looks like it's not in the RubyGems bindir. Search in $PATH, but
292
292
  # be very careful about this because whatever we find might belong
293
293
  # to a different Ruby interpreter than the current one.
294
- ENV['PATH'].split(File::PATH_SEPARATOR).each { |path|
295
- filename = File.join(path, name)
294
+ ::ENV['PATH'].split(::File::PATH_SEPARATOR).each { |path|
295
+ filename = ::File.join(path, name)
296
296
 
297
- if File.file?(filename) && File.executable?(filename)
297
+ if ::File.file?(filename) && ::File.executable?(filename)
298
298
  return filename if shebang_command(filename) == ruby_command
299
299
  end
300
300
  }
@@ -303,7 +303,7 @@ module Util
303
303
  end
304
304
 
305
305
  def shebang_command(filename)
306
- File.foreach(filename) { |line|
306
+ ::File.foreach(filename) { |line|
307
307
  return $1 if line =~ /\A#!\s*(\S*)/
308
308
 
309
309
  # Allow one extra line for magic comment.
@@ -315,14 +315,14 @@ module Util
315
315
 
316
316
  end
317
317
 
318
- def File.ruby; Util::Ruby.ruby_command; end
318
+ def ::File.ruby; ::Util::Ruby.ruby_command; end
319
319
 
320
320
  begin
321
321
  require 'open4'
322
322
 
323
- def Process.ruby(*args)
324
- argv = Util::Ruby.ruby_options_to_argv(args, File.ruby)
325
- Open4.popen4(*argv, &block_given? ? Proc.new : nil)
323
+ def ::Process.ruby(*args)
324
+ argv = ::Util::Ruby.ruby_options_to_argv(args, ::File.ruby)
325
+ ::Open4.popen4(*argv, &block_given? ? ::Proc.new : nil)
326
326
  end
327
- rescue LoadError
327
+ rescue ::LoadError
328
328
  end
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 7
7
- TINY = 4
7
+ TINY = 5
8
8
 
9
9
  class << self
10
10
 
data/lib/nuggets.rb CHANGED
@@ -42,18 +42,18 @@ def Nuggets(*nuggets)
42
42
  loaded_nuggets = []
43
43
 
44
44
  load_nuggets = lambda { |base, *nuggets|
45
- nuggets_by_hierarchy = nuggets.last.is_a?(Hash) ? nuggets.pop : {}
45
+ nuggets_by_hierarchy = nuggets.last.is_a?(::Hash) ? nuggets.pop : {}
46
46
 
47
47
  nuggets.each { |nugget|
48
48
  begin
49
- require path = File.join(base.to_s, nugget.to_s.downcase)
49
+ require path = ::File.join(base.to_s, nugget.to_s.downcase)
50
50
  loaded_nuggets << path
51
- rescue LoadError
51
+ rescue ::LoadError
52
52
  # if it's a directory, load anything in it
53
53
  $LOAD_PATH.each { |dir|
54
- if File.directory?(dir_path = File.join(dir, path))
55
- load_nuggets[path, *Dir[File.join(dir_path, '*')].map { |file|
56
- File.basename(file, '.rb') unless file =~ /_mixin\.rb\z/
54
+ if ::File.directory?(dir_path = ::File.join(dir, path))
55
+ load_nuggets[path, *::Dir[::File.join(dir_path, '*')].map { |file|
56
+ ::File.basename(file, '.rb') unless file =~ /_mixin\.rb\z/
57
57
  }.compact]
58
58
  break
59
59
  end
@@ -62,8 +62,8 @@ def Nuggets(*nuggets)
62
62
  }
63
63
 
64
64
  nuggets_by_hierarchy.each { |hierarchy, nuggets|
65
- nuggets = [nuggets] if nuggets.is_a?(Hash)
66
- load_nuggets[File.join(base.to_s, hierarchy.to_s.downcase), *nuggets]
65
+ nuggets = [nuggets] if nuggets.is_a?(::Hash)
66
+ load_nuggets[::File.join(base.to_s, hierarchy.to_s.downcase), *nuggets]
67
67
  }
68
68
  }
69
69