FreedomCoder-utility_belt 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +7 -0
- data/README +372 -0
- data/bin/amazon +16 -0
- data/bin/google +7 -0
- data/bin/pastie +7 -0
- data/bin/twitt +15 -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 +298 -0
- data/lib/utility_belt.rb +22 -0
- data/lib/utility_belt/amazon_upload_shortcut.rb +25 -0
- data/lib/utility_belt/clipboard.rb +70 -0
- data/lib/utility_belt/command_history.rb +110 -0
- data/lib/utility_belt/convertable_to_file.rb +34 -0
- data/lib/utility_belt/equipper.rb +71 -0
- data/lib/utility_belt/google.rb +34 -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 +35 -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/twitty.rb +58 -0
- data/lib/utility_belt/wirble.rb +83 -0
- data/lib/utility_belt/with.rb +21 -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 +38 -0
- metadata +145 -0
@@ -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,35 @@
|
|
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
|
+
Kernel.system("start #{pastie_url}")
|
23
|
+
when :linux
|
24
|
+
kernel.system("xdg-open #{pastie_url}")
|
25
|
+
end
|
26
|
+
|
27
|
+
return pastie_url
|
28
|
+
end
|
29
|
+
alias :pst :pastie
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Object
|
34
|
+
include UtilityBelt::Pastie
|
35
|
+
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,58 @@
|
|
1
|
+
# automate creating twitts
|
2
|
+
%w{net/http json platform utility_belt}.each {|lib| require lib}
|
3
|
+
UtilityBelt.equip(:clipboard)
|
4
|
+
|
5
|
+
module UtilityBelt
|
6
|
+
module Twitty
|
7
|
+
def twitt(message = nill)
|
8
|
+
message ||= Clipboard.read if Clipboard.available?
|
9
|
+
if message.length > 160
|
10
|
+
Raise ArgumentError, "Message should be shorter than 160 characters"
|
11
|
+
else
|
12
|
+
http = Net::HTTP.new("twitter.com",80)
|
13
|
+
http.start do |http|
|
14
|
+
request = Net::HTTP::Post.new("/statuses/update.json")
|
15
|
+
request.basic_auth(ENV['TWITTER_USER'], ENV['TWITTER_PWD'])
|
16
|
+
response = http.request(request, "status=#{URI.escape(message)}")
|
17
|
+
case response
|
18
|
+
when Net::HTTPSuccess, Net::HTTPRedirection
|
19
|
+
return "Success!"
|
20
|
+
else
|
21
|
+
return response.error!
|
22
|
+
end
|
23
|
+
end
|
24
|
+
http.close
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def fetch_twitt(number=nil)
|
29
|
+
http = Net::HTTP.new("twitter.com",80)
|
30
|
+
http.start do |http|
|
31
|
+
request = Net::HTTP::Get.new("/statuses/friends_timeline.json")
|
32
|
+
request.basic_auth(ENV['TWITTER_USER'], ENV['TWITTER_PWD'])
|
33
|
+
response = http.request(request)
|
34
|
+
case response
|
35
|
+
when Net::HTTPSuccess, Net::HTTPRedirection
|
36
|
+
twitts = JSON.parse(response.body)
|
37
|
+
twitts[0..((number-1 unless number.nil?) || twitts.length)].each do |twitt|
|
38
|
+
screen_name = twitt["user"]["screen_name"]
|
39
|
+
text = twitt["text"]
|
40
|
+
case Platform::IMPL
|
41
|
+
when :macosx, :linux
|
42
|
+
puts "\033[31m#{screen_name}\033\[0m => \033[33m#{text}\033\[0m"
|
43
|
+
when :mswin
|
44
|
+
puts "#{screen_name} => #{value}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
return "true"
|
48
|
+
else
|
49
|
+
return response.error!
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Object
|
57
|
+
include UtilityBelt::Twitty
|
58
|
+
end if Object.const_defined? :IRB
|
@@ -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
|
+
|