rwilcox-utility_belt 1.0.7
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.
- data/History.txt +5 -0
- data/Manifest.txt +7 -0
- data/README +356 -0
- data/bin/amazon +16 -0
- data/bin/google +7 -0
- data/bin/pastie +7 -0
- data/html/andreas00.css +449 -0
- data/html/authorship.html +86 -0
- data/html/bg.gif +0 -0
- data/html/front.jpg +0 -0
- data/html/index.html +81 -0
- data/html/menubg.gif +0 -0
- data/html/menubg2.gif +0 -0
- data/html/test.jpg +0 -0
- data/html/usage.html +304 -0
- data/lib/utility_belt/amazon_upload_shortcut.rb +25 -0
- data/lib/utility_belt/clipboard.rb +52 -0
- data/lib/utility_belt/command_history.rb +110 -0
- data/lib/utility_belt/convertable_to_file.rb +34 -0
- data/lib/utility_belt/cwd_irbrc.rb +6 -0
- data/lib/utility_belt/equipper.rb +71 -0
- data/lib/utility_belt/google.rb +33 -0
- data/lib/utility_belt/hash_math.rb +13 -0
- data/lib/utility_belt/interactive_editor.rb +78 -0
- data/lib/utility_belt/irb_options.rb +3 -0
- data/lib/utility_belt/irb_verbosity_control.rb +30 -0
- data/lib/utility_belt/is_an.rb +4 -0
- data/lib/utility_belt/language_greps.rb +28 -0
- data/lib/utility_belt/not.rb +15 -0
- data/lib/utility_belt/pastie.rb +34 -0
- data/lib/utility_belt/pipe.rb +24 -0
- data/lib/utility_belt/rails_finder_shortcut.rb +18 -0
- data/lib/utility_belt/rails_verbosity_control.rb +8 -0
- data/lib/utility_belt/string_to_proc.rb +72 -0
- data/lib/utility_belt/symbol_to_proc.rb +30 -0
- data/lib/utility_belt/wirble.rb +83 -0
- data/lib/utility_belt/with.rb +21 -0
- data/lib/utility_belt.rb +22 -0
- data/spec/convertable_to_file_spec.rb +31 -0
- data/spec/equipper_spec.rb +70 -0
- data/spec/hash_math_spec.rb +17 -0
- data/spec/interactive_editor_spec.rb +146 -0
- data/spec/language_greps_spec.rb +9 -0
- data/spec/pastie_spec.rb +92 -0
- data/spec/pipe_spec.rb +30 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/string_to_proc_spec.rb +41 -0
- data/spec/utility_belt_spec.rb +4 -0
- data/utility-belt.gemspec +22 -0
- metadata +147 -0
@@ -0,0 +1,78 @@
|
|
1
|
+
# Giles Bowkett, Greg Brown, and several audience members from Giles' Ruby East presentation.
|
2
|
+
require 'tempfile'
|
3
|
+
class InteractiveEditor
|
4
|
+
DEBIAN_SENSIBLE_EDITOR = "/usr/bin/sensible-editor"
|
5
|
+
MACOSX_OPEN_CMD = "open"
|
6
|
+
XDG_OPEN = "/usr/bin/xdg-open"
|
7
|
+
|
8
|
+
def self.sensible_editor
|
9
|
+
return ENV["VISUAL"] if ENV["VISUAL"]
|
10
|
+
return ENV["EDITOR"] if ENV["EDITOR"]
|
11
|
+
return MACOSX_OPEN_CMD if Platform::IMPL == :macosx
|
12
|
+
if Platform::IMPL == :linux
|
13
|
+
if File.executable?(XDG_OPEN)
|
14
|
+
return XDG_OPEN
|
15
|
+
end
|
16
|
+
if File.executable?(DEBIAN_SENSIBLE_EDITOR)
|
17
|
+
return DEBIAN_SENSIBLE_EDITOR
|
18
|
+
end
|
19
|
+
end
|
20
|
+
raise "Could not determine what editor to use. Please specify."
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_accessor :editor
|
24
|
+
def initialize(editor = :vim)
|
25
|
+
@editor = editor.to_s
|
26
|
+
if @editor == "mate"
|
27
|
+
@editor = "mate -w"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
def edit_interactively
|
31
|
+
unless @file
|
32
|
+
@file = Tempfile.new("irb_tempfile")
|
33
|
+
end
|
34
|
+
system("#{@editor} #{@file.path}")
|
35
|
+
Object.class_eval(`cat #{@file.path}`)
|
36
|
+
rescue Exception => error
|
37
|
+
puts error
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module InteractiveEditing
|
42
|
+
def edit_interactively(editor = InteractiveEditor.sensible_editor)
|
43
|
+
unless IRB.conf[:interactive_editors] && IRB.conf[:interactive_editors][editor]
|
44
|
+
IRB.conf[:interactive_editors] ||= {}
|
45
|
+
IRB.conf[:interactive_editors][editor] = InteractiveEditor.new(editor)
|
46
|
+
end
|
47
|
+
IRB.conf[:interactive_editors][editor].edit_interactively
|
48
|
+
end
|
49
|
+
|
50
|
+
def handling_jruby_bug(&block)
|
51
|
+
if RUBY_PLATFORM =~ /java/
|
52
|
+
puts "JRuby IRB has a bug which prevents successful IRB vi/emacs editing."
|
53
|
+
puts "The JRuby team is aware of this and working on it. But it might be unfixable."
|
54
|
+
puts "(http://jira.codehaus.org/browse/JRUBY-2049)"
|
55
|
+
else
|
56
|
+
yield
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def vi
|
61
|
+
handling_jruby_bug {edit_interactively(:vim)}
|
62
|
+
end
|
63
|
+
|
64
|
+
def mate
|
65
|
+
edit_interactively(:mate)
|
66
|
+
end
|
67
|
+
|
68
|
+
# TODO: Hardcore Emacs users use emacsclient or gnuclient to open documents in
|
69
|
+
# their existing sessions, rather than starting a brand new Emacs process.
|
70
|
+
def emacs
|
71
|
+
handling_jruby_bug {edit_interactively(:emacs)}
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Since we only intend to use this from the IRB command line, I see no reason to
|
76
|
+
# extend the entire Object class with this module when we can just extend the
|
77
|
+
# IRB main object.
|
78
|
+
self.extend InteractiveEditing if Object.const_defined? :IRB
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# IRB verbosity: http://groups.google.com/group/ruby-talk-google/browse_thread/thread/9c1febbe05513dc0
|
2
|
+
module IRB
|
3
|
+
def self.result_format
|
4
|
+
conf[:PROMPT][conf[:PROMPT_MODE]][:RETURN]
|
5
|
+
end
|
6
|
+
def self.result_format=(str)
|
7
|
+
result_format.replace(str)
|
8
|
+
end
|
9
|
+
def self.show_results
|
10
|
+
self.result_format = "=> %s\n"
|
11
|
+
end
|
12
|
+
def self.hide_results
|
13
|
+
self.result_format = ''
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Object
|
18
|
+
def verbose
|
19
|
+
IRB.show_results
|
20
|
+
end
|
21
|
+
alias :v :verbose
|
22
|
+
|
23
|
+
def quiet
|
24
|
+
IRB.hide_results
|
25
|
+
end
|
26
|
+
alias :q :quiet
|
27
|
+
|
28
|
+
alias :x :exit
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# fix by Bob Hutchison:
|
2
|
+
class String
|
3
|
+
unless public_method_defined? :blank?
|
4
|
+
def blank?
|
5
|
+
self !~ /\S/
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
# TODO: upgrade these to take either regexes or strings
|
11
|
+
|
12
|
+
# http://gilesbowkett.blogspot.com/2006/12/smalltalk-cleverness-translated-into.html
|
13
|
+
# http://gilesbowkett.com/blog_code_samples/122906_seaside_rails/controller.txt
|
14
|
+
class Object
|
15
|
+
def grep_classes(search_term)
|
16
|
+
classes = []
|
17
|
+
ObjectSpace.each_object {|object| classes << object.name if object.is_a? Class and not object.name.blank?}
|
18
|
+
classes.find_all {|klass| klass.downcase.include? search_term.downcase}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# http://gilesbowkett.blogspot.com/2007/11/irb-what-was-that-method-again.html
|
23
|
+
class Object
|
24
|
+
def grep_methods(search_term)
|
25
|
+
methods.find_all {|method| method.downcase.include? search_term.downcase}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# http://blog.jayfields.com/2007/08/ruby-adding-not-method-for-readability.html
|
2
|
+
class Object
|
3
|
+
define_method :not do
|
4
|
+
Not.new(self)
|
5
|
+
end
|
6
|
+
class Not
|
7
|
+
private *instance_methods.select { |m| m !~ /(^__|^\W|^binding$)/ }
|
8
|
+
def initialize(subject)
|
9
|
+
@subject = subject
|
10
|
+
end
|
11
|
+
def method_missing(sym, *args, &blk)
|
12
|
+
!@subject.send(sym,*args,&blk)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# automate creating pasties
|
2
|
+
%w{platform net/http utility_belt}.each {|lib| require lib}
|
3
|
+
UtilityBelt.equip(:clipboard)
|
4
|
+
|
5
|
+
module UtilityBelt
|
6
|
+
module Pastie
|
7
|
+
def pastie(stuff_to_paste = nil)
|
8
|
+
stuff_to_paste ||= Clipboard.read if Clipboard.available?
|
9
|
+
# return nil unless stuff_to_paste
|
10
|
+
|
11
|
+
pastie_url = Net::HTTP.post_form(URI.parse("http://pastie.caboo.se/pastes/create"),
|
12
|
+
{"paste_parser" => "ruby",
|
13
|
+
"paste[authorization]" => "burger",
|
14
|
+
"paste[body]" => stuff_to_paste}).body.match(/href="([^\"]+)"/)[1]
|
15
|
+
|
16
|
+
Clipboard.write(pastie_url) if Clipboard.available?
|
17
|
+
|
18
|
+
case Platform::IMPL
|
19
|
+
when :macosx
|
20
|
+
Kernel.system("open #{pastie_url}")
|
21
|
+
when :mswin
|
22
|
+
pastie_url = pastie_url.chop if pastie_url[-1].chr == "\000"
|
23
|
+
Kernel.system("start #{pastie_url}")
|
24
|
+
end
|
25
|
+
|
26
|
+
return pastie_url
|
27
|
+
end
|
28
|
+
alias :pst :pastie
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Object
|
33
|
+
include UtilityBelt::Pastie
|
34
|
+
end if Object.const_defined? :IRB
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# This extension adds a UNIX-style pipe to strings
|
2
|
+
#
|
3
|
+
# Synopsis:
|
4
|
+
#
|
5
|
+
# >> puts "UtilityBelt is better than alfalfa" | "cowsay"
|
6
|
+
# ____________________________________
|
7
|
+
# < UtilityBelt is better than alfalfa >
|
8
|
+
# ------------------------------------
|
9
|
+
# \ ^__^
|
10
|
+
# \ (oo)\_______
|
11
|
+
# (__)\ )\/\
|
12
|
+
# ||----w |
|
13
|
+
# || ||
|
14
|
+
# => nil
|
15
|
+
#
|
16
|
+
class String
|
17
|
+
def |(cmd)
|
18
|
+
IO.popen(cmd, 'r+') do |pipe|
|
19
|
+
pipe.write(self)
|
20
|
+
pipe.close_write
|
21
|
+
pipe.read
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# http://www.clarkware.com/cgi/blosxom/2007/09/03#ConsoleFindShortcut
|
2
|
+
# Mike Clark's find() shortcut for Rails console
|
3
|
+
|
4
|
+
# Creates shortcut methods for finding models.
|
5
|
+
UTILITY_BELT_IRB_STARTUP_PROCS[:define_model_find_shortcuts] = lambda do
|
6
|
+
if defined? ActiveRecord::Base || defined? ActiveResource::Base
|
7
|
+
model_files = Dir.glob("app/models/**/*.rb")
|
8
|
+
table_names = model_files.map { |f| File.basename(f).split('.')[0..-2].join }
|
9
|
+
table_names.each do |table_name|
|
10
|
+
Object.instance_eval do
|
11
|
+
define_method(table_name) do |*args|
|
12
|
+
table_name.camelize.constantize.send(:find, *args)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
# note: Mike wrote this for ARec, but it works on ARes too since it doesn't hit the DB
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# Rails verbosity: http://weblog.jamisbuck.org/2007/1/8/watching-activerecord-do-it-s-thing
|
2
|
+
# Marcel said they toyed with making this the console default on core
|
3
|
+
class Object
|
4
|
+
def log
|
5
|
+
ActiveRecord::Base.clear_active_connections!
|
6
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# String#to_proc
|
2
|
+
#
|
3
|
+
# See http://weblog.raganwald.com/2007/10/stringtoproc.html
|
4
|
+
#
|
5
|
+
# Ported from the String Lambdas in Oliver Steele's Functional Javascript
|
6
|
+
# http://osteele.com/sources/javascript/functional/
|
7
|
+
#
|
8
|
+
# This work is licensed under the MIT License:
|
9
|
+
#
|
10
|
+
# (c) 2007 Reginald Braithwaite
|
11
|
+
# Portions Copyright (c) 2006 Oliver Steele
|
12
|
+
#
|
13
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
14
|
+
# a copy of this software and associated documentation files (the
|
15
|
+
# "Software"), to deal in the Software without restriction, including
|
16
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
17
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
18
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
19
|
+
# the following conditions:
|
20
|
+
#
|
21
|
+
# The above copyright notice and this permission notice shall be
|
22
|
+
# included in all copies or substantial portions of the Software.
|
23
|
+
#
|
24
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
25
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
26
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
27
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
28
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
29
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
30
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
31
|
+
|
32
|
+
class String
|
33
|
+
unless public_method_defined? :to_proc
|
34
|
+
def to_proc &block
|
35
|
+
params = []
|
36
|
+
expr = self
|
37
|
+
sections = expr.split(/\s*->\s*/m)
|
38
|
+
if sections.length > 1 then
|
39
|
+
eval_block(sections.reverse!.inject { |e, p| "(Proc.new { |#{p.split(/\s/).join(', ')}| #{e} })" }, block)
|
40
|
+
elsif expr.match(/\b_\b/)
|
41
|
+
eval_block("Proc.new { |_| #{expr} }", block)
|
42
|
+
else
|
43
|
+
leftSection = expr.match(/^\s*(?:[+*\/%&|\^\.=<>\[]|!=)/m)
|
44
|
+
rightSection = expr.match(/[+\-*\/%&|\^\.=<>!]\s*$/m)
|
45
|
+
if leftSection || rightSection then
|
46
|
+
if (leftSection) then
|
47
|
+
params.push('$left')
|
48
|
+
expr = '$left' + expr
|
49
|
+
end
|
50
|
+
if (rightSection) then
|
51
|
+
params.push('$right')
|
52
|
+
expr = expr + '$right'
|
53
|
+
end
|
54
|
+
else
|
55
|
+
self.gsub(
|
56
|
+
/(?:\b[A-Z]|\.[a-zA-Z_$])[a-zA-Z_$\d]*|[a-zA-Z_$][a-zA-Z_$\d]*:|self|arguments|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/, ''
|
57
|
+
).scan(
|
58
|
+
/([a-z_$][a-z_$\d]*)/i
|
59
|
+
) do |v|
|
60
|
+
params.push(v) unless params.include?(v)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
eval_block("Proc.new { |#{params.join(', ')}| #{expr} }", block)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
private
|
67
|
+
def eval_block(code, block)
|
68
|
+
eval code, block && block.binding
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2005 David Heinemeier Hansson
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
class Symbol
|
25
|
+
unless public_method_defined? :to_proc
|
26
|
+
def to_proc
|
27
|
+
Proc.new { |*args| args.shift.__send__(self, *args) }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'wirble'
|
2
|
+
Wirble.init
|
3
|
+
Wirble.colorize
|
4
|
+
|
5
|
+
module UtilityBelt
|
6
|
+
module Themes
|
7
|
+
def self.background(bkg)
|
8
|
+
case bkg
|
9
|
+
when :dark
|
10
|
+
Wirble::Colorize.colors = UtilityBelt::Themes::WIRBLE_DEFAULT_THEME
|
11
|
+
when :light
|
12
|
+
Wirble::Colorize.colors = UtilityBelt::Themes::THEME_FOR_WHITE_BKG
|
13
|
+
end
|
14
|
+
end
|
15
|
+
WIRBLE_DEFAULT_THEME = {
|
16
|
+
# delimiter colors
|
17
|
+
:comma => :blue,
|
18
|
+
:refers => :blue,
|
19
|
+
|
20
|
+
# container colors (hash and array)
|
21
|
+
:open_hash => :green,
|
22
|
+
:close_hash => :green,
|
23
|
+
:open_array => :green,
|
24
|
+
:close_array => :green,
|
25
|
+
|
26
|
+
# object colors
|
27
|
+
:open_object => :light_red,
|
28
|
+
:object_class => :white,
|
29
|
+
:object_addr_prefix => :blue,
|
30
|
+
:object_line_prefix => :blue,
|
31
|
+
:close_object => :light_red,
|
32
|
+
|
33
|
+
# symbol colors
|
34
|
+
:symbol => :yellow,
|
35
|
+
:symbol_prefix => :yellow,
|
36
|
+
|
37
|
+
# string colors
|
38
|
+
:open_string => :red,
|
39
|
+
:string => :cyan,
|
40
|
+
:close_string => :red,
|
41
|
+
|
42
|
+
# misc colors
|
43
|
+
:number => :cyan,
|
44
|
+
:keyword => :green,
|
45
|
+
:class => :light_green,
|
46
|
+
:range => :red,
|
47
|
+
}
|
48
|
+
THEME_FOR_WHITE_BKG = {
|
49
|
+
# delimiter colors
|
50
|
+
:comma => :purple,
|
51
|
+
:refers => :blue,
|
52
|
+
|
53
|
+
# container colors (hash and array)
|
54
|
+
:open_hash => :red,
|
55
|
+
:close_hash => :red,
|
56
|
+
:open_array => :red,
|
57
|
+
:close_array => :red,
|
58
|
+
|
59
|
+
# object colors
|
60
|
+
:open_object => :dark_gray,
|
61
|
+
:object_class => :purple,
|
62
|
+
:object_addr_prefix => :blue,
|
63
|
+
:object_line_prefix => :blue,
|
64
|
+
:close_object => :dark_gray,
|
65
|
+
|
66
|
+
# symbol colors
|
67
|
+
:symbol => :black,
|
68
|
+
:symbol_prefix => :light_gray,
|
69
|
+
|
70
|
+
# string colors
|
71
|
+
:open_string => :blue,
|
72
|
+
:string => :dark_gray,
|
73
|
+
:close_string => :blue,
|
74
|
+
|
75
|
+
# misc colors
|
76
|
+
:number => :black,
|
77
|
+
:keyword => :brown,
|
78
|
+
:class => :red,
|
79
|
+
:range => :blue,
|
80
|
+
}
|
81
|
+
COLORS = WIRBLE_DEFAULT_THEME
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Object#with -- by Dan Yoder, dev.zeraweb.com -- this is something from Pascal
|
2
|
+
# and JavaScript. here's the use case.
|
3
|
+
|
4
|
+
# instead of:
|
5
|
+
# some_object.do_x
|
6
|
+
# some_object.do_y
|
7
|
+
# some_object.do_z
|
8
|
+
#
|
9
|
+
# you can instead do:
|
10
|
+
# with(some_object)
|
11
|
+
# do_x
|
12
|
+
# do_y
|
13
|
+
# do_z
|
14
|
+
# end
|
15
|
+
|
16
|
+
class Object
|
17
|
+
def with(object, &block)
|
18
|
+
object.instance_eval &block
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
data/lib/utility_belt.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# This started as my (Giles Bowkett's) .irbrc file, turned into a recipe on IRB for the Pragmatic Programmers,
|
2
|
+
# and soon became a scrapbook of cool code snippets from all over the place. All the RDoc lives in the README.
|
3
|
+
# Check that file for usage information, authorship, copyright, and extensive details. You can also find a
|
4
|
+
# nice, HTMLified version of the README content at http://utilitybelt.rubyforge.org.
|
5
|
+
|
6
|
+
UTILITY_BELT_IRB_STARTUP_PROCS = {} unless Object.const_defined? :UTILITY_BELT_IRB_STARTUP_PROCS
|
7
|
+
|
8
|
+
%w{rubygems active_support utility_belt/equipper}.each {|internal_library| require internal_library}
|
9
|
+
|
10
|
+
if Object.const_defined? :IRB
|
11
|
+
|
12
|
+
# Called when the irb session is ready, after any external libraries have been loaded. This
|
13
|
+
# allows the user to specify which gadgets in the utility belt to equip. (Kind of pushing the
|
14
|
+
# metaphor, but hey, what the hell.)
|
15
|
+
IRB.conf[:IRB_RC] = lambda do
|
16
|
+
UtilityBelt.equip(:defaults) unless UtilityBelt.equipped?
|
17
|
+
UTILITY_BELT_IRB_STARTUP_PROCS.each {|symbol, proc| proc.call}
|
18
|
+
end
|
19
|
+
|
20
|
+
# default: dark background
|
21
|
+
UtilityBelt::Themes.background(:dark) if defined? UtilityBelt::Themes
|
22
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.join(File.dirname(__FILE__), "spec_helper")
|
3
|
+
|
4
|
+
require 'spec'
|
5
|
+
require 'irb'
|
6
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'utility_belt', 'convertable_to_file')
|
7
|
+
|
8
|
+
describe ConvertableToFile do
|
9
|
+
include ConvertableToFile
|
10
|
+
|
11
|
+
before :each do
|
12
|
+
@tempfile = stub("temp file", :<< => nil, :path => nil)
|
13
|
+
Tempfile.stub!(:open).and_yield(@tempfile)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should create a temp file using object id as basename" do
|
17
|
+
should_receive(:object_id).and_return(6789)
|
18
|
+
Tempfile.should_receive(:open).with("6789").and_yield(@tempfile)
|
19
|
+
to_file
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should dump self to the opened temp file" do
|
23
|
+
@tempfile.should_receive(:<<).with(self)
|
24
|
+
to_file
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return the temp file path" do
|
28
|
+
@tempfile.should_receive(:path).and_return("TEMP_PATH")
|
29
|
+
to_file.should == "TEMP_PATH"
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
|
+
require 'lib/utility_belt/equipper'
|
3
|
+
|
4
|
+
# Mocks for the gadgets
|
5
|
+
UTILITY_BELT_IRB_STARTUP_PROCS = {}
|
6
|
+
|
7
|
+
module IRB
|
8
|
+
def self.conf
|
9
|
+
{}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "UtilityBelt equipper" do
|
14
|
+
|
15
|
+
ALL_GADGETS = UtilityBelt::Equipper::GADGETS
|
16
|
+
DEFAULT_GADGETS = UtilityBelt::Equipper::DEFAULTS
|
17
|
+
|
18
|
+
before(:all) do
|
19
|
+
# I know, global variables are bad, but I can't get this to work otherwise
|
20
|
+
Kernel.__send__(:alias_method, :old_require, :require)
|
21
|
+
Kernel.__send__(:define_method, :require, proc {|library| $required_libs << library[13..-1] })
|
22
|
+
end
|
23
|
+
|
24
|
+
before(:each) do
|
25
|
+
$required_libs = []
|
26
|
+
end
|
27
|
+
|
28
|
+
after(:each) do
|
29
|
+
$required_libs = nil
|
30
|
+
end
|
31
|
+
|
32
|
+
after(:all) do
|
33
|
+
Kernel.__send__(:alias_method, :require, :old_require)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should load all gadgets" do
|
37
|
+
UtilityBelt.equip(:all)
|
38
|
+
$required_libs.should == ALL_GADGETS
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should load no gadgets" do
|
42
|
+
UtilityBelt.equip(:none)
|
43
|
+
$required_libs.should == []
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should load all default gadegts" do
|
47
|
+
UtilityBelt.equip(:defaults)
|
48
|
+
$required_libs.should == DEFAULT_GADGETS
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should load all gadgets except is_an" do
|
52
|
+
UtilityBelt.equip(:all, :except => ['is_an'])
|
53
|
+
$required_libs.should == ALL_GADGETS - ['is_an']
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should load no gadgets except is_an" do
|
57
|
+
UtilityBelt.equip(:none, :except => ['is_an'])
|
58
|
+
$required_libs.should == ['is_an']
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should accept a string for the except-param" do
|
62
|
+
UtilityBelt.equip(:none, :except => 'is_an')
|
63
|
+
$required_libs.should == ['is_an']
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should accept a symbol for the except-param" do
|
67
|
+
UtilityBelt.equip(:none, :except => :is_an)
|
68
|
+
$required_libs.should == ['is_an']
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
|
+
require "lib/utility_belt/hash_math"
|
3
|
+
describe "Hash math" do
|
4
|
+
|
5
|
+
it "should add hashes" do
|
6
|
+
({:a => :b} + {:c => :d}).should == {:a => :b, :c => :d}
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should subtract hashes" do
|
10
|
+
({:a => :b, :c => :d} - {:c => :d}).should == {:a => :b}
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should subtract key/value pairs by key" do
|
14
|
+
({:a => :b, :c => :d} - :c).should == {:a => :b}
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|