akitaonrails-utility_belt 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/History.txt +5 -0
  2. data/Manifest.txt +7 -0
  3. data/README +348 -0
  4. data/bin/amazon +16 -0
  5. data/bin/google +7 -0
  6. data/bin/pastie +7 -0
  7. data/html/andreas00.css +449 -0
  8. data/html/authorship.html +86 -0
  9. data/html/bg.gif +0 -0
  10. data/html/front.jpg +0 -0
  11. data/html/index.html +81 -0
  12. data/html/menubg.gif +0 -0
  13. data/html/menubg2.gif +0 -0
  14. data/html/test.jpg +0 -0
  15. data/html/usage.html +298 -0
  16. data/lib/utility_belt.rb +22 -0
  17. data/lib/utility_belt/amazon_upload_shortcut.rb +25 -0
  18. data/lib/utility_belt/clipboard.rb +68 -0
  19. data/lib/utility_belt/command_history.rb +129 -0
  20. data/lib/utility_belt/convertable_to_file.rb +34 -0
  21. data/lib/utility_belt/equipper.rb +71 -0
  22. data/lib/utility_belt/google.rb +26 -0
  23. data/lib/utility_belt/hash_math.rb +13 -0
  24. data/lib/utility_belt/interactive_editor.rb +81 -0
  25. data/lib/utility_belt/irb_options.rb +3 -0
  26. data/lib/utility_belt/irb_verbosity_control.rb +30 -0
  27. data/lib/utility_belt/is_an.rb +4 -0
  28. data/lib/utility_belt/language_greps.rb +28 -0
  29. data/lib/utility_belt/not.rb +15 -0
  30. data/lib/utility_belt/pastie.rb +29 -0
  31. data/lib/utility_belt/pipe.rb +24 -0
  32. data/lib/utility_belt/print_methods.rb +72 -0
  33. data/lib/utility_belt/rails_finder_shortcut.rb +18 -0
  34. data/lib/utility_belt/rails_verbosity_control.rb +8 -0
  35. data/lib/utility_belt/string_to_proc.rb +72 -0
  36. data/lib/utility_belt/symbol_to_proc.rb +30 -0
  37. data/lib/utility_belt/webbrowser.rb +37 -0
  38. data/lib/utility_belt/wirble.rb +83 -0
  39. data/lib/utility_belt/with.rb +21 -0
  40. data/spec/convertable_to_file_spec.rb +31 -0
  41. data/spec/equipper_spec.rb +70 -0
  42. data/spec/hash_math_spec.rb +17 -0
  43. data/spec/interactive_editor_spec.rb +146 -0
  44. data/spec/language_greps_spec.rb +9 -0
  45. data/spec/pastie_spec.rb +92 -0
  46. data/spec/pipe_spec.rb +30 -0
  47. data/spec/spec_helper.rb +8 -0
  48. data/spec/string_to_proc_spec.rb +41 -0
  49. data/spec/utility_belt_spec.rb +4 -0
  50. data/utility_belt.gemspec +22 -0
  51. metadata +136 -0
@@ -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
+
@@ -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
@@ -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
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+ require "lib/utility_belt/language_greps"
3
+ describe "language greps" do
4
+
5
+ it "should handle String#blank?" do
6
+ "".should be_blank
7
+ end
8
+
9
+ 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
data/spec/pipe_spec.rb ADDED
@@ -0,0 +1,30 @@
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', 'pipe')
7
+
8
+ describe "String#|" do
9
+ before :each do
10
+ @pipe = stub(:pipe, :write => nil, :close_write => nil, :read => nil)
11
+ IO.stub!(:popen).and_yield(@pipe).and_return("RESULT")
12
+ end
13
+
14
+ it "should open a pipe" do
15
+ IO.should_receive(:popen).with("COMMAND", 'r+').and_return(@pipe)
16
+ "foo" | "COMMAND"
17
+ end
18
+
19
+ it "should write itself to the the pipe, close it, then read from it" do
20
+ @pipe.should_receive(:write).with("foo").ordered
21
+ @pipe.should_receive(:close_write).ordered
22
+ @pipe.should_receive(:read)
23
+
24
+ "foo" | "COMMAND"
25
+ end
26
+
27
+ it "should return the result of the IO.popen block" do
28
+ ("foo" | "COMMAND").should == "RESULT"
29
+ end
30
+ end
@@ -0,0 +1,8 @@
1
+ # Make sure Rubygems' mangling of the path is already done before we do our own
2
+ # mangling.
3
+ require 'rubygems'
4
+
5
+ # Ensure that when we require UtilityBelt libs they are from the files under
6
+ # test, NOT from the installed gem.
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), ".."))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+ require "lib/utility_belt/string_to_proc"
3
+ describe "String to Proc" do
4
+
5
+ before(:all) do
6
+ @one2five = 1..5
7
+ end
8
+
9
+ it "should handle simple arrow notation" do
10
+ @one2five.map(&'x -> x + 1').should eql(@one2five.map { |x| x + 1 })
11
+ @one2five.map(&'x -> x*x').should eql(@one2five.map { |x| x*x })
12
+ @one2five.inject(&'x y -> x*y').should eql(@one2five.inject { |x,y| x*y })
13
+ 'x y -> x**y'.to_proc()[2,3].should eql(lambda { |x,y| x**y }[2,3])
14
+ 'y x -> x**y'.to_proc()[2,3].should eql(lambda { |y,x| x**y }[2,3])
15
+ end
16
+
17
+ it "should handle chained arrows" do
18
+ 'x -> y -> x**y'.to_proc()[2][3].should eql(lambda { |x| lambda { |y| x**y } }[2][3])
19
+ 'x -> y z -> y**(z-x)'.to_proc()[1][2,3].should eql(lambda { |x| lambda { |y,z| y**(z-x) } }[1][2,3])
20
+ end
21
+
22
+ it "should handle the default parameter" do
23
+ @one2five.map(&'2**_/2').should eql(@one2five.map { |x| 2**x/2 })
24
+ @one2five.select(&'_%2==0').should eql(@one2five.select { |x| x%2==0 })
25
+ end
26
+
27
+ it "should handle point-free notation" do
28
+ @one2five.inject(&'*').should eql(@one2five.inject { |mem, var| mem * var })
29
+ @one2five.select(&'>2').should eql(@one2five.select { |x| x>2 })
30
+ @one2five.select(&'2<').should eql(@one2five.select { |x| 2<x })
31
+ @one2five.map(&'2*').should eql(@one2five.map { |x| 2*x })
32
+ (-3..3).map(&'.abs').should eql((-3..3).map { |x| x.abs })
33
+ end
34
+
35
+ it "should handle implied parameters as best it can" do
36
+ @one2five.inject(&'x*y').should eql(@one2five.inject(&'*'))
37
+ 'x**y'.to_proc()[2,3].should eql(8)
38
+ 'y**x'.to_proc()[2,3].should eql(8)
39
+ end
40
+
41
+ end