visionmedia-rext 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,4 +1,10 @@
1
1
 
2
+ === 0.3.0 / 2009-08-11
3
+
4
+ * Moved some string encoding related methods to string/encode.rb
5
+ * Added String#to_md5 and String#to_sha512
6
+ * Added autospec support
7
+
2
8
  === 0.2.2 / 2009-06-12
3
9
 
4
10
  * Fixed require for libs previously removed
data/Manifest CHANGED
@@ -22,7 +22,7 @@ lib/rext/object/metaclass.rb
22
22
  lib/rext/object.rb
23
23
  lib/rext/proc/helpers.rb
24
24
  lib/rext/proc.rb
25
- lib/rext/string/escape.rb
25
+ lib/rext/string/encode.rb
26
26
  lib/rext/string/helpers.rb
27
27
  lib/rext/string.rb
28
28
  lib/rext/time/helpers.rb
@@ -41,6 +41,7 @@ spec/integer_spec.rb
41
41
  spec/module_spec.rb
42
42
  spec/numeric_spec.rb
43
43
  spec/object_spec.rb
44
+ spec/spec.opts
44
45
  spec/spec_helper.rb
45
46
  spec/string_spec.rb
46
47
  spec/time_spec.rb
data/README.rdoc CHANGED
@@ -38,6 +38,7 @@ Below are the methods currently provided by Rext.
38
38
  - meta_eval
39
39
  - indifferent_hash
40
40
  - returning
41
+ - try
41
42
 
42
43
  * String
43
44
  - url_encode
@@ -63,6 +64,8 @@ Below are the methods currently provided by Rext.
63
64
  - switchify
64
65
  - word_frequency
65
66
  - frequency_of_word
67
+ - to_md5
68
+ - to_sha512
66
69
 
67
70
  * Integer
68
71
  - ordanalize
@@ -38,4 +38,25 @@ class Object
38
38
  value
39
39
  end
40
40
 
41
+ ##
42
+ # Retry a _block_ of statements upto a number of _times_.
43
+ # When no error is raised the value returned by _block_ is
44
+ # simply returned.
45
+ #
46
+ # === Examples
47
+ #
48
+ # try 3 do
49
+ # open 'http://vision-media.ca'
50
+ # end
51
+ # # => allows 3 tries to fetch the response
52
+ #
53
+
54
+ def try times = 1, options = {}, &block
55
+ val = yield
56
+ rescue options[:on] || Exception
57
+ retry if (times -= 1) > 0
58
+ else
59
+ val
60
+ end
61
+
41
62
  end
data/lib/rext/string.rb CHANGED
@@ -3,5 +3,5 @@
3
3
  # Load String specific rext extensions.
4
4
  #++
5
5
 
6
- require 'rext/string/escape'
6
+ require 'rext/string/encode'
7
7
  require 'rext/string/helpers'
@@ -0,0 +1,80 @@
1
+
2
+ require 'rack'
3
+
4
+ class String
5
+
6
+ ##
7
+ # Return 32 character md5 string.
8
+ #
9
+ # === Examples
10
+ #
11
+ # 'test'.to_md5 # => 098f6bcd4621d373cade4e832627b4f6
12
+ #
13
+
14
+ def to_md5
15
+ Digest::MD5.hexdigest self
16
+ end
17
+
18
+ ##
19
+ # Return 128 character sha512 string.
20
+ #
21
+ # === Examples
22
+ #
23
+ # 'test'.to_sha512 # => ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff
24
+ #
25
+
26
+ def to_sha512
27
+ Digest::SHA512.hexdigest self
28
+ end
29
+
30
+ ##
31
+ # Return Base 64 decoded string.
32
+ #
33
+ # === Examples
34
+ #
35
+ # 'Y29va2llcw=='.base64_decode # => cookies
36
+ #
37
+
38
+ def base64_decode
39
+ unpack('m').first
40
+ end
41
+
42
+ ##
43
+ # Return Base 64 encoded string.
44
+ #
45
+ # === Examples
46
+ #
47
+ # 'cookies'.base64_encode # => Y29va2llcw==
48
+ #
49
+
50
+ def base64_encode
51
+ [self].pack('m').chop
52
+ end
53
+
54
+ ##
55
+ # URL encode. Shortcut for Rack::Utils.encode.
56
+
57
+ def url_encode
58
+ Rack::Utils.escape self
59
+ end
60
+
61
+ ##
62
+ # URL decode. Shortcut for Rack::Utils.unescape.
63
+
64
+ def url_decode
65
+ Rack::Utils.unescape self
66
+ end
67
+
68
+ ##
69
+ # Escape html entities. Shortcut for Rack::Utils.escape_html.
70
+ #
71
+ # === Examples
72
+ #
73
+ # 'im <strong>strong</strong>.escape_html # => im &lt;strong&gt;strong&lt;/strong&gt;
74
+ #
75
+
76
+ def escape_html
77
+ Rack::Utils.escape_html self
78
+ end
79
+
80
+ end
@@ -1,32 +1,10 @@
1
1
 
2
2
  require 'extlib'
3
+ require 'digest/sha2'
4
+ require 'digest/md5'
3
5
 
4
6
  class String
5
7
 
6
- ##
7
- # Return Base 64 decoded string.
8
- #
9
- # === Examples
10
- #
11
- # 'Y29va2llcw=='.base64_decode # => cookies
12
- #
13
-
14
- def base64_decode
15
- unpack('m').first
16
- end
17
-
18
- ##
19
- # Return Base 64 encoded string.
20
- #
21
- # === Examples
22
- #
23
- # 'cookies'.base64_encode # => Y29va2llcw==
24
- #
25
-
26
- def base64_encode
27
- [self].pack('m').chop
28
- end
29
-
30
8
  ##
31
9
  # Returns a File instance.
32
10
  #
data/lib/rext/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Rext
3
- VERSION = '0.2.2'
3
+ VERSION = '0.3.0'
4
4
  end
data/rext.gemspec CHANGED
@@ -2,20 +2,20 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{rext}
5
- s.version = "0.2.2"
5
+ s.version = "0.3.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["TJ Holowaychuk"]
9
- s.date = %q{2009-06-12}
9
+ s.date = %q{2009-08-11}
10
10
  s.description = %q{Ruby extensions}
11
11
  s.email = %q{tj@vision-media.ca}
12
- s.extra_rdoc_files = ["lib/rext/all.rb", "lib/rext/array/helpers.rb", "lib/rext/array.rb", "lib/rext/date/helpers.rb", "lib/rext/date.rb", "lib/rext/enumerable/helpers.rb", "lib/rext/enumerable.rb", "lib/rext/hash/helpers.rb", "lib/rext/hash.rb", "lib/rext/integer/helpers.rb", "lib/rext/integer.rb", "lib/rext/module/helpers.rb", "lib/rext/module.rb", "lib/rext/numeric/bytes.rb", "lib/rext/numeric/time.rb", "lib/rext/numeric.rb", "lib/rext/object/helpers.rb", "lib/rext/object/metaclass.rb", "lib/rext/object.rb", "lib/rext/proc/helpers.rb", "lib/rext/proc.rb", "lib/rext/string/escape.rb", "lib/rext/string/helpers.rb", "lib/rext/string.rb", "lib/rext/time/helpers.rb", "lib/rext/time.rb", "lib/rext/version.rb", "lib/rext.rb", "README.rdoc", "tasks/benchmark.rake", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
13
- s.files = ["benchmarks/enumerable.rb", "benchmarks/proc.rb", "History.rdoc", "lib/rext/all.rb", "lib/rext/array/helpers.rb", "lib/rext/array.rb", "lib/rext/date/helpers.rb", "lib/rext/date.rb", "lib/rext/enumerable/helpers.rb", "lib/rext/enumerable.rb", "lib/rext/hash/helpers.rb", "lib/rext/hash.rb", "lib/rext/integer/helpers.rb", "lib/rext/integer.rb", "lib/rext/module/helpers.rb", "lib/rext/module.rb", "lib/rext/numeric/bytes.rb", "lib/rext/numeric/time.rb", "lib/rext/numeric.rb", "lib/rext/object/helpers.rb", "lib/rext/object/metaclass.rb", "lib/rext/object.rb", "lib/rext/proc/helpers.rb", "lib/rext/proc.rb", "lib/rext/string/escape.rb", "lib/rext/string/helpers.rb", "lib/rext/string.rb", "lib/rext/time/helpers.rb", "lib/rext/time.rb", "lib/rext/version.rb", "lib/rext.rb", "Manifest", "Rakefile", "README.rdoc", "rext.gemspec", "spec/array_spec.rb", "spec/date_spec.rb", "spec/enumerable_spec.rb", "spec/hash_spec.rb", "spec/integer_spec.rb", "spec/module_spec.rb", "spec/numeric_spec.rb", "spec/object_spec.rb", "spec/spec_helper.rb", "spec/string_spec.rb", "spec/time_spec.rb", "tasks/benchmark.rake", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
12
+ s.extra_rdoc_files = ["lib/rext/all.rb", "lib/rext/array/helpers.rb", "lib/rext/array.rb", "lib/rext/date/helpers.rb", "lib/rext/date.rb", "lib/rext/enumerable/helpers.rb", "lib/rext/enumerable.rb", "lib/rext/hash/helpers.rb", "lib/rext/hash.rb", "lib/rext/integer/helpers.rb", "lib/rext/integer.rb", "lib/rext/module/helpers.rb", "lib/rext/module.rb", "lib/rext/numeric/bytes.rb", "lib/rext/numeric/time.rb", "lib/rext/numeric.rb", "lib/rext/object/helpers.rb", "lib/rext/object/metaclass.rb", "lib/rext/object.rb", "lib/rext/proc/helpers.rb", "lib/rext/proc.rb", "lib/rext/string/encode.rb", "lib/rext/string/helpers.rb", "lib/rext/string.rb", "lib/rext/time/helpers.rb", "lib/rext/time.rb", "lib/rext/version.rb", "lib/rext.rb", "README.rdoc", "tasks/benchmark.rake", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
13
+ s.files = ["benchmarks/enumerable.rb", "benchmarks/proc.rb", "History.rdoc", "lib/rext/all.rb", "lib/rext/array/helpers.rb", "lib/rext/array.rb", "lib/rext/date/helpers.rb", "lib/rext/date.rb", "lib/rext/enumerable/helpers.rb", "lib/rext/enumerable.rb", "lib/rext/hash/helpers.rb", "lib/rext/hash.rb", "lib/rext/integer/helpers.rb", "lib/rext/integer.rb", "lib/rext/module/helpers.rb", "lib/rext/module.rb", "lib/rext/numeric/bytes.rb", "lib/rext/numeric/time.rb", "lib/rext/numeric.rb", "lib/rext/object/helpers.rb", "lib/rext/object/metaclass.rb", "lib/rext/object.rb", "lib/rext/proc/helpers.rb", "lib/rext/proc.rb", "lib/rext/string/encode.rb", "lib/rext/string/helpers.rb", "lib/rext/string.rb", "lib/rext/time/helpers.rb", "lib/rext/time.rb", "lib/rext/version.rb", "lib/rext.rb", "Manifest", "Rakefile", "README.rdoc", "rext.gemspec", "spec/array_spec.rb", "spec/date_spec.rb", "spec/enumerable_spec.rb", "spec/hash_spec.rb", "spec/integer_spec.rb", "spec/module_spec.rb", "spec/numeric_spec.rb", "spec/object_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/string_spec.rb", "spec/time_spec.rb", "tasks/benchmark.rake", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
14
14
  s.homepage = %q{http://github.com/visionmedia/rext}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rext", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{rext}
18
- s.rubygems_version = %q{1.3.4}
18
+ s.rubygems_version = %q{1.3.5}
19
19
  s.summary = %q{Ruby extensions}
20
20
 
21
21
  if s.respond_to? :specification_version then
data/spec/array_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
  require 'rext/array'
3
4
 
4
5
  describe Array do
data/spec/date_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
  require 'rext/date'
3
4
 
4
5
  describe Date do
@@ -1,4 +1,5 @@
1
1
 
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
  require 'rext/enumerable'
3
4
 
4
5
  describe Enumerable do
data/spec/hash_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
  require 'rext/hash'
3
4
 
4
5
  describe Hash do
@@ -20,7 +21,7 @@ describe Hash do
20
21
  end
21
22
 
22
23
  it "should stringify numbers and symbols" do
23
- { :interval => 15, :icon => :jpeg }.switchify.should == ['--interval', '15', '--icon', 'jpeg']
24
+ { :interval => 15, :icon => :jpeg }.switchify.sort.should == ['--icon', '--interval', '15', 'jpeg']
24
25
  end
25
26
 
26
27
  it "should work with little switches" do
data/spec/integer_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
  require 'rext/integer'
3
4
 
4
5
  describe Integer do
data/spec/module_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
  require 'rext/module'
3
4
 
4
5
  describe Module do
data/spec/numeric_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
  require 'rext/numeric'
3
4
 
4
5
  describe Numeric do
data/spec/object_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
  require 'rext/object'
3
4
 
4
5
  describe Object do
@@ -51,6 +52,37 @@ describe Object do
51
52
  end
52
53
  end
53
54
 
55
+ describe "#try" do
56
+ it "should try the block once with no args" do
57
+ times = 0
58
+ try { times += 1 }
59
+ times.should == 1
60
+ end
61
+
62
+ it "should try the block multiple times when failing" do
63
+ times = 0
64
+ try(3) { times += 1; raise 'foo' }
65
+ times.should == 3
66
+ end
67
+
68
+ it "should try the block one time when succesful" do
69
+ times = 0
70
+ try(3) { times += 1 }
71
+ times.should == 1
72
+ end
73
+
74
+ it "should return the value returned by the block" do
75
+ val = try(3) { 'test' }
76
+ val.should == 'test'
77
+ end
78
+
79
+ it "should allow specific exceptions to be rescued" do
80
+ lambda { try(3){ raise ArgumentError, 'foo' } }.should_not raise_error
81
+ lambda { try(3, :on => ArgumentError){ raise ArgumentError, 'foo' } }.should_not raise_error
82
+ lambda { try(3, :on => TypeError){ raise ArgumentError, 'foo' } }.should raise_error(ArgumentError)
83
+ end
84
+ end
85
+
54
86
  describe "#returning" do
55
87
  it "should return the value given to it" do
56
88
  def frequency_of enum
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
data/spec/string_spec.rb CHANGED
@@ -1,9 +1,40 @@
1
1
 
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
  require 'rext/string'
3
4
 
4
5
  describe String do
5
- describe "helpers" do
6
+ describe "encode" do
7
+ describe "#to_md5" do
8
+ it "should return an md5 hash" do
9
+ 'test'.to_md5.should == '098f6bcd4621d373cade4e832627b4f6'
10
+ 'test'.to_md5.should == 'test'.to_md5
11
+ 'test'.to_md5.should_not == 'tests'.to_md5
12
+ end
13
+ end
6
14
 
15
+ describe "#to_sha512" do
16
+ it "should return an md5 hash" do
17
+ 'test'.to_sha512.should == 'ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff'
18
+ 'test'.to_sha512.should == 'test'.to_sha512
19
+ 'test'.to_sha512.should_not == 'tests'.to_sha512
20
+ end
21
+ end
22
+
23
+ describe "#base64_encode" do
24
+ it "should base64 encode a string" do
25
+ 'tj'.base64_encode.should == 'dGo='
26
+ "foo \n bar\n\n".base64_encode.base64_decode.should == "foo \n bar\n\n"
27
+ end
28
+ end
29
+
30
+ describe "#base64_decode" do
31
+ it "should decode a base64 string" do
32
+ 'dGo='.base64_decode.should == 'tj'
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "helpers" do
7
38
  describe "#word_frequency" do
8
39
  it "should return a hash with word keys and count values" do
9
40
  'mm i love cookies mm'.word_frequency.
@@ -38,19 +69,6 @@ describe String do
38
69
  end
39
70
  end
40
71
 
41
- describe "#base64_encode" do
42
- it "should base64 encode a string" do
43
- 'tj'.base64_encode.should == 'dGo='
44
- "foo \n bar\n\n".base64_encode.base64_decode.should == "foo \n bar\n\n"
45
- end
46
- end
47
-
48
- describe "#base64_decode" do
49
- it "should decode a base64 string" do
50
- 'dGo='.base64_decode.should == 'tj'
51
- end
52
- end
53
-
54
72
  describe "#path" do
55
73
  it "should return an instance of a pathname" do
56
74
  'History.rdoc'.path.should be_an_instance_of(Pathname)
data/spec/time_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
  require 'rext/time'
3
4
 
4
5
  describe Time do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visionmedia-rext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Holowaychuk
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-12 00:00:00 -07:00
12
+ date: 2009-08-11 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -41,7 +41,7 @@ extra_rdoc_files:
41
41
  - lib/rext/object.rb
42
42
  - lib/rext/proc/helpers.rb
43
43
  - lib/rext/proc.rb
44
- - lib/rext/string/escape.rb
44
+ - lib/rext/string/encode.rb
45
45
  - lib/rext/string/helpers.rb
46
46
  - lib/rext/string.rb
47
47
  - lib/rext/time/helpers.rb
@@ -78,7 +78,7 @@ files:
78
78
  - lib/rext/object.rb
79
79
  - lib/rext/proc/helpers.rb
80
80
  - lib/rext/proc.rb
81
- - lib/rext/string/escape.rb
81
+ - lib/rext/string/encode.rb
82
82
  - lib/rext/string/helpers.rb
83
83
  - lib/rext/string.rb
84
84
  - lib/rext/time/helpers.rb
@@ -97,6 +97,7 @@ files:
97
97
  - spec/module_spec.rb
98
98
  - spec/numeric_spec.rb
99
99
  - spec/object_spec.rb
100
+ - spec/spec.opts
100
101
  - spec/spec_helper.rb
101
102
  - spec/string_spec.rb
102
103
  - spec/time_spec.rb
@@ -106,6 +107,7 @@ files:
106
107
  - tasks/spec.rake
107
108
  has_rdoc: false
108
109
  homepage: http://github.com/visionmedia/rext
110
+ licenses:
109
111
  post_install_message:
110
112
  rdoc_options:
111
113
  - --line-numbers
@@ -131,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
133
  requirements: []
132
134
 
133
135
  rubyforge_project: rext
134
- rubygems_version: 1.2.0
136
+ rubygems_version: 1.3.5
135
137
  signing_key:
136
138
  specification_version: 3
137
139
  summary: Ruby extensions
@@ -1,32 +0,0 @@
1
-
2
- require 'rack'
3
-
4
- class String
5
-
6
- ##
7
- # URL encode. Shortcut for Rack::Utils.encode.
8
-
9
- def url_encode
10
- Rack::Utils.escape self
11
- end
12
-
13
- ##
14
- # URL decode. Shortcut for Rack::Utils.unescape.
15
-
16
- def url_decode
17
- Rack::Utils.unescape self
18
- end
19
-
20
- ##
21
- # Escape html entities. Shortcut for Rack::Utils.escape_html.
22
- #
23
- # === Examples
24
- #
25
- # 'im <strong>strong</strong>.escape_html # => im &lt;strong&gt;strong&lt;/strong&gt;
26
- #
27
-
28
- def escape_html
29
- Rack::Utils.escape_html self
30
- end
31
-
32
- end