mack_ruby_core_extensions 0.0.5 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/doc/classes/Hash.html +41 -41
  2. data/doc/classes/Kernel.html +12 -10
  3. data/doc/classes/Object.html +146 -68
  4. data/doc/created.rid +1 -1
  5. data/doc/files/README.html +1 -1
  6. data/doc/files/lib/extensions/array_rb.html +1 -1
  7. data/doc/files/lib/extensions/class_rb.html +1 -1
  8. data/doc/files/lib/extensions/float_rb.html +1 -1
  9. data/doc/files/lib/extensions/hash_rb.html +8 -1
  10. data/doc/files/lib/extensions/kernel_rb.html +1 -1
  11. data/doc/files/lib/extensions/math_rb.html +1 -1
  12. data/doc/files/lib/extensions/method_not_implemented_rb.html +1 -1
  13. data/doc/files/lib/extensions/module_rb.html +1 -1
  14. data/doc/files/lib/extensions/nil_rb.html +1 -1
  15. data/doc/files/lib/extensions/object_rb.html +1 -1
  16. data/doc/files/lib/extensions/string_rb.html +1 -1
  17. data/doc/files/lib/extensions/symbol_rb.html +1 -1
  18. data/doc/files/lib/mack_ruby_core_extensions_rb.html +1 -1
  19. data/doc/files/lib/utils/inflections_rb.html +1 -1
  20. data/doc/files/lib/utils/inflector_rb.html +1 -1
  21. data/doc/fr_file_index.html +0 -1
  22. data/lib/extensions/hash.rb +3 -2
  23. data/lib/extensions/kernel.rb +6 -2
  24. data/lib/extensions/object.rb +50 -39
  25. data/lib/mack_ruby_core_extensions.rb +1 -1
  26. data/test/extensions/float_test.rb +2 -0
  27. data/test/extensions/hash_test.rb +23 -7
  28. data/test/extensions/kernel_test.rb +9 -6
  29. data/test/extensions/math_test.rb +20 -5
  30. data/test/extensions/module_test.rb +0 -6
  31. data/test/extensions/nil_test.rb +0 -6
  32. data/test/extensions/object_test.rb +30 -6
  33. data/test/test_helper.rb +41 -0
  34. metadata +2 -6
  35. data/doc/files/lib/extensions/logger_rb.html +0 -101
  36. data/lib/extensions/logger.rb +0 -0
  37. data/test/extensions/logger_test.rb +0 -15
  38. data/test/extensions/method_not_implemented_test.rb +0 -15
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Tue Mar 25 17:56:02 -0400 2008</td>
59
+ <td>Tue Mar 25 20:37:52 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Tue Mar 25 15:42:07 -0400 2008</td>
59
+ <td>Wed Mar 26 19:19:12 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -69,6 +69,13 @@
69
69
  <div id="contextContent">
70
70
 
71
71
 
72
+ <div id="requires-list">
73
+ <h3 class="section-bar">Required files</h3>
74
+
75
+ <div class="name-list">
76
+ uri&nbsp;&nbsp;
77
+ </div>
78
+ </div>
72
79
 
73
80
  </div>
74
81
 
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Mar 14 12:40:30 -0400 2008</td>
59
+ <td>Wed Mar 26 10:20:07 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Mar 14 12:40:30 -0400 2008</td>
59
+ <td>Tue Mar 25 20:37:52 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Mar 14 12:40:30 -0400 2008</td>
59
+ <td>Tue Mar 25 20:37:52 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Tue Mar 25 15:42:41 -0400 2008</td>
59
+ <td>Tue Mar 25 20:37:52 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Tue Mar 25 15:42:51 -0400 2008</td>
59
+ <td>Tue Mar 25 20:37:52 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Tue Mar 25 17:28:02 -0400 2008</td>
59
+ <td>Wed Mar 26 14:44:19 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Tue Mar 25 17:47:44 -0400 2008</td>
59
+ <td>Tue Mar 25 20:37:52 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Mar 14 12:40:30 -0400 2008</td>
59
+ <td>Tue Mar 25 20:37:52 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Tue Mar 25 17:56:57 -0400 2008</td>
59
+ <td>Wed Mar 26 10:23:16 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Tue Mar 25 16:34:03 -0400 2008</td>
59
+ <td>Tue Mar 25 20:37:52 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Tue Mar 25 17:26:23 -0400 2008</td>
59
+ <td>Tue Mar 25 20:37:52 -0400 2008</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -26,7 +26,6 @@
26
26
  <a href="files/lib/extensions/float_rb.html">lib/extensions/float.rb</a><br />
27
27
  <a href="files/lib/extensions/hash_rb.html">lib/extensions/hash.rb</a><br />
28
28
  <a href="files/lib/extensions/kernel_rb.html">lib/extensions/kernel.rb</a><br />
29
- <a href="files/lib/extensions/logger_rb.html">lib/extensions/logger.rb</a><br />
30
29
  <a href="files/lib/extensions/math_rb.html">lib/extensions/math.rb</a><br />
31
30
  <a href="files/lib/extensions/method_not_implemented_rb.html">lib/extensions/method_not_implemented.rb</a><br />
32
31
  <a href="files/lib/extensions/module_rb.html">lib/extensions/module.rb</a><br />
@@ -1,3 +1,4 @@
1
+ require 'uri'
1
2
  class Hash
2
3
 
3
4
  def join(pair_string, join_string)
@@ -24,7 +25,7 @@ class Hash
24
25
  if v.is_a?(Hash)
25
26
  stack << [k,v]
26
27
  else
27
- v = Rack::Utils.escape(v) if escape
28
+ v = URI.escape(v.to_s) if escape
28
29
  params << "#{k}=#{v}&"
29
30
  end
30
31
  end
@@ -34,7 +35,7 @@ class Hash
34
35
  if v.is_a?(Hash)
35
36
  stack << ["#{parent}[#{k}]", v]
36
37
  else
37
- v = Rack::Utils.escape(v) if escape
38
+ v = URI.escape(v.to_s) if escape
38
39
  params << "#{parent}[#{k}]=#{v}&"
39
40
  end
40
41
  end
@@ -3,7 +3,7 @@ require 'stringio'
3
3
 
4
4
  module Kernel
5
5
 
6
- def self.pp_to_s(object)
6
+ def pp_to_s(object)
7
7
  pp_out = StringIO.new
8
8
  PP.pp(object,pp_out)
9
9
  return pp_out.string
@@ -20,7 +20,11 @@ module Kernel
20
20
  return yield
21
21
  rescue #{retry_exceptions.join(", ")} => e
22
22
  retries -= 1
23
- retry if retries > 0
23
+ if retries > 0
24
+ retry
25
+ else
26
+ raise e
27
+ end
24
28
  end
25
29
  }
26
30
 
@@ -6,12 +6,18 @@ class Object
6
6
  self.to_s
7
7
  end
8
8
 
9
+ # Uses <code>define_method</code> to create an empty for the method parameter defined.
10
+ # That method will then raise MethodNotImplemented. This is useful for creating interfaces
11
+ # and you want to stub out methods that others need to implement.
9
12
  def self.needs_method(meth)
10
13
  define_method(meth) do
11
14
  raise MethodNotImplemented.new(meth)
12
15
  end
13
16
  end
14
17
 
18
+ # This prints out running time for the block provided. This is great for things
19
+ # like Rake tasks, etc... where you would like to know how long it, or a section of
20
+ # it took to run.
15
21
  def running_time(message = "", logger = nil)
16
22
  s_time = Time.now
17
23
  s = "---Starting at #{s_time}---"
@@ -33,45 +39,49 @@ class Object
33
39
  logger.info x unless logger.nil?
34
40
  end
35
41
 
42
+ # This method will call send to all the methods defined on the previous method.
43
+ #
44
+ # Example:
45
+ # Fruit.send_with_chain([:new, :get_citrus, :get_orange, :class]) # => Orange
46
+ #
47
+ # This would be the equivalent:
48
+ # Fruit.new.get_citrus.get_orange.class
36
49
  def send_with_chain(methods, *args)
37
50
  obj = self
38
- methods = [methods] unless methods.is_a?(Array)
39
- methods.each {|m| obj = obj.send(m, *args)}
51
+ [methods].flatten.each {|m| obj = obj.send(m, *args)}
40
52
  obj
41
53
  end
42
54
 
43
- =begin
44
- ivar_cache allows you to cache the results of the block into an instance variable in a class,
45
- and then will serve up that instance variable the next time you call that method again.
46
-
47
- old way:
48
- def show_page_link
49
- unless @show_page_link # check if instance variable exists
50
- # if the instance variable doesn't exist let's do some work and assign it to the instance variable.
51
- @show_page_link = link_to("show", some_url(:id => self.id, :foo => bar, etc... => etc))
52
- end
53
- @show_page_link # now return the instance variable
54
- end
55
-
56
- new way:
57
- def show_page_link
58
- ivar_cache do
59
- link_to("show", some_url(:id => self.id, :foo => bar, etc... => etc))
60
- end
61
- end
62
-
63
- this does everything the old way did, but it is much cleaner, and a lot less code!
64
- in case you're wondering it caches the result, by default, to an instance variable named after the method,
65
- so in our above example the instance variable would be name, @show_page_link. this can be overridden like such:
66
-
67
- def show_page_link
68
- ivar_cache("foo_var") do
69
- link_to("show", some_url(:id => self.id, :foo => bar, etc... => etc))
70
- end
71
- end
72
-
73
- now it will cache it to @foo_var
74
- =end
55
+ # ivar_cache allows you to cache the results of the block into an instance variable in a class,
56
+ # and then will serve up that instance variable the next time you call that method again.
57
+ #
58
+ # old way:
59
+ # def show_page_link
60
+ # unless @show_page_link # check if instance variable exists
61
+ # # if the instance variable doesn't exist let's do some work and assign it to the instance variable.
62
+ # @show_page_link = link_to("show", some_url(:id => self.id, :foo => bar, etc... => etc))
63
+ # end
64
+ # @show_page_link # now return the instance variable
65
+ # end
66
+ #
67
+ # new way:
68
+ # def show_page_link
69
+ # ivar_cache do
70
+ # link_to("show", some_url(:id => self.id, :foo => bar, etc... => etc))
71
+ # end
72
+ # end
73
+ #
74
+ # this does everything the old way did, but it is much cleaner, and a lot less code!
75
+ # in case you're wondering it caches the result, by default, to an instance variable named after the method,
76
+ # so in our above example the instance variable would be name, <code>@show_page_link</code>. this can be overridden like such:
77
+ #
78
+ # def show_page_link
79
+ # ivar_cache("foo_var") do
80
+ # link_to("show", some_url(:id => self.id, :foo => bar, etc... => etc))
81
+ # end
82
+ # end
83
+ #
84
+ # now it will cache it to <code>@foo_var</code>
75
85
  def ivar_cache(var_name = nil, &block)
76
86
  if var_name.nil?
77
87
  call = caller[0]
@@ -89,18 +99,19 @@ class Object
89
99
  call = caller[0]
90
100
  var_name = call[(call.index('`')+1)...call.index("'")]
91
101
  end
92
- remove_instance_variable("@#{var_name}") rescue
102
+ remove_instance_variable("@#{var_name}") #rescue
93
103
  yield if block_given?
94
104
  end
95
105
 
106
+ # Returns the namespaces for a particular object.
107
+ #
108
+ # Examples:
109
+ # Animals::Dog::Poodle.new.namespaces # => ["Animals", "Dog"]
96
110
  def namespaces
97
111
  ivar_cache("object_namespaces") do
98
112
  nss = []
99
113
  full_name = self.class.name.to_s
100
- ns = full_name.split("::")
101
- ns.each do |n|
102
- nss << n
103
- end
114
+ nss = full_name.split("::")
104
115
  nss.pop
105
116
  nss
106
117
  end
@@ -2,6 +2,6 @@
2
2
  require "utils/#{k}"
3
3
  end
4
4
 
5
- [:array, :class, :float, :hash, :kernel, :logger, :math, :method_not_implemented, :module, :nil, :object, :string, :symbol].each do |k|
5
+ [:array, :class, :float, :hash, :kernel, :math, :method_not_implemented, :module, :nil, :object, :string, :symbol].each do |k|
6
6
  require "extensions/#{k}"
7
7
  end
@@ -4,6 +4,8 @@ class FloatTest < Test::Unit::TestCase
4
4
 
5
5
  def test_prec
6
6
  assert_equal 1.1, 1.1234.prec(1)
7
+ assert_equal 1.12, 1.1234.prec(2)
8
+ assert_equal 1.0, 1.0.prec(1)
7
9
  end
8
10
 
9
11
  end
@@ -1,12 +1,5 @@
1
1
  require File.dirname(__FILE__) + '/../test_helper.rb'
2
-
3
2
  class HashTest < Test::Unit::TestCase
4
-
5
- def setup
6
- end
7
-
8
- def teardown
9
- end
10
3
 
11
4
  def test_minus
12
5
  h = {:a => "aaa", :b => "bbb", :c => "ccc"}
@@ -31,4 +24,27 @@ class HashTest < Test::Unit::TestCase
31
24
  assert ("rel=\"nofollow\" alt=\"pickles\"" == h.join("%s=\"%s\"", " ") || "alt=\"pickles\" rel=\"nofollow\"" == h.join("%s=\"%s\"", " "))
32
25
  end
33
26
 
27
+ def test_to_params
28
+ h = {:a => "aaa", :b => "bbb", :c => "ccc"}
29
+ assert(h.to_params == "a=aaa&b=bbb&c=ccc" ||
30
+ h.to_params == "b=bbb&c=ccc&a=aaa" ||
31
+ h.to_params == "b=bbb&c=ccc&a=aaa" ||
32
+ h.to_params == "b=bbb&a=aaa&c=ccc")
33
+
34
+ h = {:a => "aaa", :b => "bbb", :c => "Hello World!"}
35
+
36
+ assert(h.to_params == "b=bbb&c=Hello%20World!&a=aaa" ||
37
+ h.to_params == "b=bbb&a=aaa&c=Hello%20World!")
38
+
39
+ assert(h.to_params(false) == "b=bbb&c=Hello World!&a=aaa" ||
40
+ h.to_params(false) == "b=bbb&a=aaa&c=Hello World!")
41
+
42
+ h = {:a => "aaa", :b => {:one => "won", :two => "too"}}
43
+ assert_equal "a=aaa&b[one]=won&b[two]=too", h.to_params
44
+
45
+ h = {:one => "one", :two => "too", :three => 3}
46
+ assert_match "three=3", h.to_params
47
+
48
+ end
49
+
34
50
  end
@@ -2,14 +2,17 @@ require File.dirname(__FILE__) + '/../test_helper.rb'
2
2
 
3
3
  class KernelTest < Test::Unit::TestCase
4
4
 
5
- def setup
5
+ def test_pp_to_s
6
+ x = pp_to_s([1, 2, :three, "four"])
7
+ assert_equal %{[1, 2, :three, \"four\"]\n}, x
6
8
  end
7
9
 
8
- def teardown
10
+ def test_retryable
11
+ assert_equal 1, retryable { 1 }
12
+ assert_raise(Exception) { retryable { raise Exception.new } }
13
+ assert_raise(TypeError) { retryable { retryable { 1 + nil} } }
14
+ assert_raise(TypeError) { retryable { retryable(:on => NameError) { 1 + nil} } }
9
15
  end
10
16
 
11
- # replace this with tests of your own.
12
- def test_truth
13
- assert true
14
- end
17
+
15
18
  end
@@ -2,14 +2,29 @@ require File.dirname(__FILE__) + '/../test_helper.rb'
2
2
 
3
3
  class MathTest < Test::Unit::TestCase
4
4
 
5
- def setup
5
+ def test_log2
6
+ assert_equal 2.32193, Math.log2(5).prec(5)
7
+ assert_equal 4.64386, Math.log2(25).prec(5)
6
8
  end
7
9
 
8
- def teardown
10
+ def test_min
11
+ assert_equal 2, Math.min(2, 5)
12
+ assert_equal 2, Math.min(5, 2)
13
+ assert_equal 2, Math.min(2, 2)
14
+ assert_equal 2.0, Math.min(2, 5)
15
+ assert_equal 2.5, Math.min(5.1, 2.5)
16
+ assert_equal 2.12, Math.min(2.12, 2.54)
17
+ assert_raise(ArgumentError) { assert_equal 2, Math.min(2, nil) }
9
18
  end
10
19
 
11
- # replace this with tests of your own.
12
- def test_truth
13
- assert true
20
+ def test_max
21
+ assert_equal 5, Math.max(2, 5)
22
+ assert_equal 5, Math.max(5, 2)
23
+ assert_equal 2, Math.max(2, 2)
24
+ assert_equal 5, Math.max(2, 5)
25
+ assert_equal 5.1, Math.max(5.1, 2.5)
26
+ assert_equal 2.54, Math.max(2.12, 2.54)
27
+ assert_raise(ArgumentError) { assert_equal 2, Math.max(2, nil) }
14
28
  end
29
+
15
30
  end