utility_belt 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/bin/amazon +4 -5
  2. data/bin/google +5 -16
  3. data/bin/pastie +6 -0
  4. data/lib/utility_belt.rb +13 -30
  5. data/lib/utility_belt/amazon_upload_shortcut.rb +25 -0
  6. data/lib/utility_belt/clipboard.rb +52 -0
  7. data/lib/{command_history.rb → utility_belt/command_history.rb} +20 -5
  8. data/lib/utility_belt/convertable_to_file.rb +34 -0
  9. data/lib/utility_belt/equipper.rb +72 -0
  10. data/lib/utility_belt/google.rb +33 -0
  11. data/lib/utility_belt/hash_math.rb +10 -0
  12. data/lib/utility_belt/interactive_editor.rb +78 -0
  13. data/lib/{irb_options.rb → utility_belt/irb_options.rb} +0 -0
  14. data/lib/{irb_verbosity_control.rb → utility_belt/irb_verbosity_control.rb} +0 -0
  15. data/lib/{is_an.rb → utility_belt/is_an.rb} +0 -0
  16. data/lib/{language_greps.rb → utility_belt/language_greps.rb} +0 -0
  17. data/lib/{not.rb → utility_belt/not.rb} +0 -0
  18. data/lib/utility_belt/pastie.rb +33 -0
  19. data/lib/utility_belt/pipe.rb +24 -0
  20. data/lib/utility_belt/prompt.rb +1 -0
  21. data/lib/{rails_finder_shortcut.rb → utility_belt/rails_finder_shortcut.rb} +0 -0
  22. data/lib/{rails_verbosity_control.rb → utility_belt/rails_verbosity_control.rb} +0 -0
  23. data/lib/{string_to_proc.rb → utility_belt/string_to_proc.rb} +0 -0
  24. data/lib/{symbol_to_proc.rb → utility_belt/symbol_to_proc.rb} +0 -0
  25. data/lib/{themes.rb → utility_belt/wirble.rb} +4 -0
  26. data/lib/{with.rb → utility_belt/with.rb} +0 -0
  27. data/spec/convertable_to_file_spec.rb +31 -0
  28. data/spec/equipper_spec.rb +70 -0
  29. data/spec/hash_math_spec.rb +32 -0
  30. data/spec/interactive_editor_spec.rb +146 -0
  31. data/{test → spec}/language_greps_spec.rb +3 -2
  32. data/spec/pastie_spec.rb +92 -0
  33. data/spec/pipe_spec.rb +30 -0
  34. data/spec/spec_helper.rb +8 -0
  35. data/{test → spec}/string_to_proc_spec.rb +2 -1
  36. data/{test → spec}/utility_belt_spec.rb +1 -0
  37. data/utility_belt.gemspec +5 -5
  38. metadata +42 -29
  39. data/Rakefile +0 -20
  40. data/lib/amazon_upload_shortcut.rb +0 -18
  41. data/lib/hash_math.rb +0 -13
  42. data/lib/interactive_editor.rb +0 -42
  43. data/lib/mac_clipboard.rb +0 -15
  44. data/lib/pastie.rb +0 -15
  45. data/test/hash_math_spec.rb +0 -16
File without changes
File without changes
@@ -0,0 +1,33 @@
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
+ end
24
+
25
+ return pastie_url
26
+ end
27
+ alias :pst :pastie
28
+ end
29
+ end
30
+
31
+ class Object
32
+ include UtilityBelt::Pastie
33
+ 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 @@
1
+
@@ -1,3 +1,7 @@
1
+ require 'wirble'
2
+ Wirble.init
3
+ Wirble.colorize
4
+
1
5
  module UtilityBelt
2
6
  module Themes
3
7
  def self.background(bkg)
File without changes
@@ -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,32 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+ require "lib/utility_belt/hash_math"
3
+ describe "Hash math" do
4
+
5
+ before do
6
+ @a = { :a => 1 }
7
+ @b = { :b => 2 }
8
+ @c = { :a => 1, :b => 2}
9
+ end
10
+
11
+ it "should add hashes" do
12
+ ({:a => :b} + {:c => :d}).should == {:a => :b, :c => :d}
13
+ end
14
+
15
+ it "should subtract hashes" do
16
+ ({:a => :b, :c => :d} - {:c => :d}).should == {:a => :b}
17
+ end
18
+
19
+ it "should subtract key/value pairs by key" do
20
+ ({:a => :b, :c => :d} - :c).should == {:a => :b}
21
+ end
22
+
23
+ it "should return a new object instead of mutating the operands" do
24
+ (@c - @a).should_not === @c
25
+ end
26
+
27
+ it "should not alter the state of the operands" do
28
+ (@c - @b).should == @a # but...
29
+ @c.should == { :a => 1, :b => 2 }
30
+ end
31
+
32
+ end
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/env ruby
2
+ require File.join(File.dirname(__FILE__), "spec_helper")
3
+
4
+ require 'spec'
5
+ require 'irb'
6
+ require 'delegate'
7
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'utility_belt', 'interactive_editor')
8
+
9
+ # Using SimpleDelegator allows us to replace the constants without triggering a
10
+ # "constant redefined" warning.
11
+ module StubPlatform
12
+ ARCH = SimpleDelegator.new(:x86)
13
+ OS = SimpleDelegator.new(:unix)
14
+ IMPL = SimpleDelegator.new(:linux)
15
+ end
16
+
17
+ # Sneak a stub Platform class into InteractiveEditor so that we don't have to
18
+ # override constants on the real Platform module
19
+ #
20
+ # P.S. This ugliness is why libraries should always prefer to expose methods
21
+ # over constants.
22
+ class InteractiveEditor
23
+ Platform = StubPlatform
24
+ end
25
+
26
+ describe InteractiveEditor, "given no clues as to what editor to use" do
27
+ before :each do
28
+ ENV.delete("VISUAL")
29
+ ENV.delete("EDITOR")
30
+ Kernel.stub!(:test).and_return(false)
31
+ File.stub!(:executable?).and_return(false)
32
+ end
33
+
34
+ it "should complain" do
35
+ lambda do
36
+ InteractiveEditor.sensible_editor
37
+ end.should raise_error
38
+ end
39
+ end
40
+
41
+ describe InteractiveEditor,
42
+ "given a Mac OS X platform and no editor environment vars" do
43
+
44
+ before :each do
45
+ ENV.delete("VISUAL")
46
+ ENV.delete("EDITOR")
47
+ @old_impl = StubPlatform::IMPL.__getobj__
48
+ StubPlatform::IMPL.__setobj__(:macosx)
49
+ end
50
+
51
+ after :each do
52
+ StubPlatform::IMPL.__setobj__(@old_impl)
53
+ end
54
+
55
+ it "should use the OS X 'open' command as the default editor" do
56
+ InteractiveEditor.sensible_editor.should == "open"
57
+ end
58
+ end
59
+
60
+ # xdg-open is a facility from the freedesktop.org, available on some recent free
61
+ # desktop operating systems (like Ubuntu). It uses the desktop environments
62
+ # filetype associations to determine what program to open a file in.
63
+ describe InteractiveEditor,
64
+ "given a Linux OS and no environment vars" do
65
+
66
+ before :each do
67
+ ENV.delete("VISUAL")
68
+ ENV.delete("EDITOR")
69
+ @old_impl = StubPlatform::IMPL.__getobj__
70
+ StubPlatform::OS.__setobj__(:linux)
71
+ File.stub!(:executable?).and_return(true)
72
+ end
73
+
74
+ after :each do
75
+ StubPlatform::IMPL.__setobj__(@old_impl)
76
+ end
77
+
78
+ it "should attempt to use 'xdg-open' command as the default editor" do
79
+ File.should_receive(:executable?).
80
+ with("/usr/bin/xdg-open").
81
+ and_return(true)
82
+ InteractiveEditor.sensible_editor.should == "/usr/bin/xdg-open"
83
+ end
84
+ end
85
+
86
+ # /usr/bin/sensible-editor is a Debian-ism AFAIK
87
+ describe InteractiveEditor,
88
+ "given the existence of /usr/bin/sensible-editor and no xdg-open" do
89
+ it "should use /usr/bin/sensible-editor as the default editor" do
90
+ File.should_receive(:executable?).
91
+ with("/usr/bin/xdg-open").
92
+ and_return(false)
93
+ File.should_receive(:executable?).
94
+ with("/usr/bin/sensible-editor").
95
+ and_return(true)
96
+ InteractiveEditor.sensible_editor.should == "/usr/bin/sensible-editor"
97
+ end
98
+ end
99
+
100
+ describe InteractiveEditor, "given an EDITOR environment variable" do
101
+ before :each do
102
+ File.stub!(:executable?).and_return(true)
103
+ ENV["EDITOR"] = "MY_EDITOR"
104
+ end
105
+
106
+ after :each do
107
+ ENV.delete("EDITOR")
108
+ end
109
+
110
+ it "should use the EDITOR environment variable to determine a sensible editor" do
111
+ InteractiveEditor.sensible_editor.should == "MY_EDITOR"
112
+ end
113
+ end
114
+
115
+ describe InteractiveEditor, "given a VISUAL environment variable" do
116
+ before :each do
117
+ File.stub!(:executable?).and_return(true)
118
+ ENV["EDITOR"] = "MY_EDITOR"
119
+ ENV["VISUAL"] = "MY_VISUAL_EDITOR"
120
+ end
121
+
122
+ after :each do
123
+ ENV.delete("EDITOR")
124
+ ENV.delete("VISUAL")
125
+ end
126
+
127
+ it "should use the environment variable to determine a sensible editor" do
128
+ InteractiveEditor.sensible_editor.should == "MY_VISUAL_EDITOR"
129
+ end
130
+ end
131
+
132
+ describe InteractiveEditing, "(calling out to an external editor)" do
133
+ before :each do
134
+ @it = Object.new
135
+ @it.extend(InteractiveEditing)
136
+ @editor = stub("Editor", :edit_interactively => nil)
137
+ @editor_path = stub("Editor Path")
138
+ InteractiveEditor.stub!(:sensible_editor).and_return(@editor_path)
139
+ InteractiveEditor.stub!(:new).and_return(@editor)
140
+ end
141
+
142
+ it "should use InteractiveEditor to determine default editor" do
143
+ @it.edit_interactively
144
+ IRB.conf[:interactive_editors][@editor_path].should equal(@editor)
145
+ end
146
+ end
@@ -1,6 +1,7 @@
1
- require "lib/language_greps"
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+ require "lib/utility_belt/language_greps"
2
3
  describe "language greps" do
3
-
4
+
4
5
  it "should handle String#blank?" do
5
6
  "".should be_blank
6
7
  end
@@ -0,0 +1,92 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+ require 'rubygems'
3
+ gem 'rspec'
4
+ require 'spec'
5
+ Platform = Module.new unless Object.const_defined?('Platform')
6
+ Net = Module.new unless Object.const_defined?('Net')
7
+
8
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib/utility_belt'))
9
+ UtilityBelt.equip(:pastie)
10
+ include UtilityBelt::Pastie
11
+ Clipboard = UtilityBelt::Clipboard unless Object.const_defined?('Clipboard')
12
+
13
+ describe "pastie being called" do
14
+
15
+ before(:all) do
16
+ Net::HTTP = mock('HTTP') unless Net.const_defined?('HTTP')
17
+ URI = mock('URI') unless Object.const_defined?('URI')
18
+ Clipboard = mock('clipboard') unless Object.const_defined?('Clipboard')
19
+ end
20
+
21
+ before(:each) do
22
+ @page = mock('page')
23
+ @page.stub!(:body).and_return('href="foo"')
24
+ Net::HTTP.stub!(:post_form).and_return(@page)
25
+ URI.stub!(:parse)
26
+ Clipboard.stub!(:read)
27
+ Clipboard.stub!(:write)
28
+ Kernel.stub!(:system)
29
+ end
30
+
31
+ it "should be available in global namespace and not blow-up with default stub/mocking" do
32
+ pastie
33
+ end
34
+
35
+ it "should uri-parse the pastie uri" do
36
+ URI.should_receive(:parse).with("http://pastie.caboo.se/pastes/create")
37
+ pastie
38
+ end
39
+
40
+ it "should pass the uri-parsed result into the post" do
41
+ URI.should_receive(:parse).and_return('a_uri_object')
42
+ Net::HTTP.should_receive(:post_form).with('a_uri_object', anything()).and_return(@page)
43
+ pastie
44
+ end
45
+
46
+ it "should call system open on the pastie return" do
47
+ @page.should_receive(:body).and_return('href="returned_url"')
48
+ case Platform::IMPL
49
+ when :macosx
50
+ Kernel.should_receive(:system).with("open returned_url")
51
+ when :mswin
52
+ Kernel.should_receive(:system).with("start returned_url")
53
+ end
54
+ pastie
55
+ end
56
+
57
+ it "should write resulting url into the clipboard" do
58
+ @page.should_receive(:body).and_return('href="returned_url"')
59
+ Clipboard.should_receive(:write).with('returned_url')
60
+ pastie
61
+ end
62
+
63
+ describe "with no parameter it uses the clipboard" do
64
+ it "should read the clipboard" do
65
+ Clipboard.should_receive(:read)
66
+ pastie
67
+ end
68
+
69
+ it "should put the clipboard results in the post to pastie" do
70
+ Clipboard.should_receive(:read).and_return('bar')
71
+ Net::HTTP.should_receive(:post_form).with(anything(),{"paste_parser" => "ruby",
72
+ "paste[authorization]" => "burger",
73
+ "paste[body]" => 'bar'}).and_return(@page)
74
+ pastie
75
+ end
76
+ end
77
+
78
+ describe "with a parameter instead" do
79
+ #TODO: windows/linux safer now, since no clipboard functionality?
80
+ it "should not even read the clipboard" do
81
+ Clipboard.should_not_receive(:read)
82
+ pastie "baz"
83
+ end
84
+
85
+ it "should pass in the parameter instead" do
86
+ Net::HTTP.should_receive(:post_form).with(anything(),{"paste_parser" => "ruby",
87
+ "paste[authorization]" => "burger",
88
+ "paste[body]" => 'baz'}).and_return(@page)
89
+ pastie "baz"
90
+ end
91
+ end
92
+ end