rext 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.rdoc +97 -0
- data/Manifest +54 -0
- data/README.rdoc +136 -0
- data/Rakefile +16 -0
- data/benchmarks/enumerable.rb +14 -0
- data/benchmarks/proc.rb +24 -0
- data/lib/rext.rb +24 -0
- data/lib/rext/all.rb +18 -0
- data/lib/rext/array.rb +6 -0
- data/lib/rext/array/helpers.rb +64 -0
- data/lib/rext/date.rb +6 -0
- data/lib/rext/date/helpers.rb +11 -0
- data/lib/rext/enumerable.rb +6 -0
- data/lib/rext/enumerable/helpers.rb +88 -0
- data/lib/rext/hash.rb +6 -0
- data/lib/rext/hash/helpers.rb +42 -0
- data/lib/rext/integer.rb +6 -0
- data/lib/rext/integer/helpers.rb +29 -0
- data/lib/rext/module.rb +6 -0
- data/lib/rext/module/helpers.rb +85 -0
- data/lib/rext/numeric.rb +7 -0
- data/lib/rext/numeric/bytes.rb +16 -0
- data/lib/rext/numeric/time.rb +57 -0
- data/lib/rext/object.rb +7 -0
- data/lib/rext/object/helpers.rb +62 -0
- data/lib/rext/object/metaclass.rb +29 -0
- data/lib/rext/proc.rb +7 -0
- data/lib/rext/proc/helpers.rb +22 -0
- data/lib/rext/string.rb +7 -0
- data/lib/rext/string/encode.rb +82 -0
- data/lib/rext/string/helpers.rb +246 -0
- data/lib/rext/symbol.rb +6 -0
- data/lib/rext/symbol/helpers.rb +16 -0
- data/lib/rext/time.rb +6 -0
- data/lib/rext/time/helpers.rb +43 -0
- data/lib/rext/version.rb +4 -0
- data/rext.gemspec +30 -0
- data/spec/array_spec.rb +61 -0
- data/spec/date_spec.rb +14 -0
- data/spec/enumerable_spec.rb +55 -0
- data/spec/hash_spec.rb +36 -0
- data/spec/integer_spec.rb +19 -0
- data/spec/module_spec.rb +41 -0
- data/spec/numeric_spec.rb +31 -0
- data/spec/object_spec.rb +101 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/string_spec.rb +194 -0
- data/spec/symbol_spec.rb +14 -0
- data/spec/time_spec.rb +22 -0
- data/tasks/benchmark.rake +13 -0
- data/tasks/docs.rake +13 -0
- data/tasks/gemspec.rake +3 -0
- data/tasks/spec.rake +25 -0
- metadata +147 -0
data/History.rdoc
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
|
2
|
+
=== 0.3.4 / 2009-10-08
|
3
|
+
|
4
|
+
* Added metadef alias to meta_def
|
5
|
+
* Added #extendable
|
6
|
+
|
7
|
+
=== 0.3.3 / 2009-08-24
|
8
|
+
|
9
|
+
* Added Symbol#to_proc [#5]
|
10
|
+
|
11
|
+
=== 0.3.2 / 2009-08-24
|
12
|
+
|
13
|
+
* Added Symbol#to_proc [#5]
|
14
|
+
|
15
|
+
=== 0.3.1 / 2009-08-17
|
16
|
+
|
17
|
+
* Added #try
|
18
|
+
* Fixed some requires
|
19
|
+
|
20
|
+
=== 0.3.0 / 2009-08-11
|
21
|
+
|
22
|
+
* Moved some string encoding related methods to string/encode.rb
|
23
|
+
* Added String#to_md5 and String#to_sha512
|
24
|
+
* Added autospec support
|
25
|
+
|
26
|
+
=== 0.2.2 / 2009-06-12
|
27
|
+
|
28
|
+
* Fixed require for libs previously removed
|
29
|
+
|
30
|
+
=== 0.2.1 / 2009-06-10
|
31
|
+
|
32
|
+
* Removed BasicObject due to compatibility issues with other libraries
|
33
|
+
|
34
|
+
=== 0.2.0 / 2009-06-10
|
35
|
+
|
36
|
+
* Added Enumerable#includes_all?
|
37
|
+
|
38
|
+
=== 0.1.1 / 2009-06-10
|
39
|
+
|
40
|
+
* Removed sketchy BasicObject #method_added hack for now
|
41
|
+
* Added Module#setup
|
42
|
+
* Added / Implemented Object#returning
|
43
|
+
|
44
|
+
=== 0.1.0 / 2009-05-04
|
45
|
+
|
46
|
+
* Added Module#call_method
|
47
|
+
* Added String#word_frequency and String#frequency_of_word
|
48
|
+
|
49
|
+
=== 0.0.8 / 2009-04-13
|
50
|
+
|
51
|
+
* Added Array#chunk/in_groups_of
|
52
|
+
* Added Array#pad
|
53
|
+
|
54
|
+
=== 0.0.7 / 2009-04-08
|
55
|
+
|
56
|
+
* Added Hash#switchify
|
57
|
+
* Added String#switchify
|
58
|
+
|
59
|
+
=== 0.0.6 / 2009-04-01
|
60
|
+
|
61
|
+
* Added String#from
|
62
|
+
|
63
|
+
=== 0.0.5 / 2009-04-01
|
64
|
+
|
65
|
+
* Added Object#indifferent_hash [#3]
|
66
|
+
* Added String#ends_with?
|
67
|
+
* Added String#starts_with?
|
68
|
+
* Refactored String#singular?
|
69
|
+
* Removed Todo list, use lighthouse now
|
70
|
+
|
71
|
+
=== 0.0.4 / 2009-03-25
|
72
|
+
|
73
|
+
* Added Object#metaclass
|
74
|
+
* Added Object#meta_eval
|
75
|
+
* Added Object#meta_def
|
76
|
+
* Added BasicObject for Ruby < 1.9
|
77
|
+
* Added n param to String#first / last
|
78
|
+
* Added byte conversion 1.byte, 6.kilobytes etc
|
79
|
+
* Added form param to #to_time
|
80
|
+
* Added Array#from / to
|
81
|
+
* Moved time related methods from Integer to Numeric
|
82
|
+
|
83
|
+
=== 0.0.3 / 2009-03-25
|
84
|
+
|
85
|
+
* Added String#base64_encode
|
86
|
+
* Added String#base64_decode
|
87
|
+
* Added String#constantize
|
88
|
+
* Added String#camelize
|
89
|
+
|
90
|
+
=== 0.0.2 / 2009-03-23
|
91
|
+
|
92
|
+
* Added Proc#yield_or_eval docs
|
93
|
+
* Renamed class/helpers to module/helpers
|
94
|
+
|
95
|
+
=== 0.0.1 / 2009-03-23
|
96
|
+
|
97
|
+
* Initial release
|
data/Manifest
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
History.rdoc
|
2
|
+
Manifest
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
benchmarks/enumerable.rb
|
6
|
+
benchmarks/proc.rb
|
7
|
+
lib/rext.rb
|
8
|
+
lib/rext/all.rb
|
9
|
+
lib/rext/array.rb
|
10
|
+
lib/rext/array/helpers.rb
|
11
|
+
lib/rext/date.rb
|
12
|
+
lib/rext/date/helpers.rb
|
13
|
+
lib/rext/enumerable.rb
|
14
|
+
lib/rext/enumerable/helpers.rb
|
15
|
+
lib/rext/hash.rb
|
16
|
+
lib/rext/hash/helpers.rb
|
17
|
+
lib/rext/integer.rb
|
18
|
+
lib/rext/integer/helpers.rb
|
19
|
+
lib/rext/module.rb
|
20
|
+
lib/rext/module/helpers.rb
|
21
|
+
lib/rext/numeric.rb
|
22
|
+
lib/rext/numeric/bytes.rb
|
23
|
+
lib/rext/numeric/time.rb
|
24
|
+
lib/rext/object.rb
|
25
|
+
lib/rext/object/helpers.rb
|
26
|
+
lib/rext/object/metaclass.rb
|
27
|
+
lib/rext/proc.rb
|
28
|
+
lib/rext/proc/helpers.rb
|
29
|
+
lib/rext/string.rb
|
30
|
+
lib/rext/string/encode.rb
|
31
|
+
lib/rext/string/helpers.rb
|
32
|
+
lib/rext/symbol.rb
|
33
|
+
lib/rext/symbol/helpers.rb
|
34
|
+
lib/rext/time.rb
|
35
|
+
lib/rext/time/helpers.rb
|
36
|
+
lib/rext/version.rb
|
37
|
+
rext.gemspec
|
38
|
+
spec/array_spec.rb
|
39
|
+
spec/date_spec.rb
|
40
|
+
spec/enumerable_spec.rb
|
41
|
+
spec/hash_spec.rb
|
42
|
+
spec/integer_spec.rb
|
43
|
+
spec/module_spec.rb
|
44
|
+
spec/numeric_spec.rb
|
45
|
+
spec/object_spec.rb
|
46
|
+
spec/spec.opts
|
47
|
+
spec/spec_helper.rb
|
48
|
+
spec/string_spec.rb
|
49
|
+
spec/symbol_spec.rb
|
50
|
+
spec/time_spec.rb
|
51
|
+
tasks/benchmark.rake
|
52
|
+
tasks/docs.rake
|
53
|
+
tasks/gemspec.rake
|
54
|
+
tasks/spec.rake
|
data/README.rdoc
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
|
2
|
+
= Rext
|
3
|
+
|
4
|
+
Rext is primarily used for the Evolution CMS project, and its dependencies,
|
5
|
+
however it is de-coupled and free to use within your own project.
|
6
|
+
|
7
|
+
== Motivations
|
8
|
+
|
9
|
+
* ActiveSupport does not contain everything we need, and is messy IMO
|
10
|
+
* Very easy to cherry-pick what you need
|
11
|
+
* Provide better documentation
|
12
|
+
* Attention to quality and performance
|
13
|
+
* Abstraction of loosely coupled helpers used throughout Evolution and its sub-gems
|
14
|
+
|
15
|
+
== Usage
|
16
|
+
|
17
|
+
require 'rext/all'
|
18
|
+
|
19
|
+
# or
|
20
|
+
|
21
|
+
require 'rext/string'
|
22
|
+
require 'rext/hash'
|
23
|
+
...
|
24
|
+
|
25
|
+
# or
|
26
|
+
|
27
|
+
require 'rext/string/escape'
|
28
|
+
require 'rext/enumerable/every'
|
29
|
+
...
|
30
|
+
|
31
|
+
== Extensions
|
32
|
+
|
33
|
+
Below are the methods currently provided by Rext.
|
34
|
+
|
35
|
+
* Object
|
36
|
+
- metaclass
|
37
|
+
- meta_def
|
38
|
+
- meta_eval
|
39
|
+
- indifferent_hash
|
40
|
+
- returning
|
41
|
+
- try
|
42
|
+
|
43
|
+
* String
|
44
|
+
- url_encode
|
45
|
+
- url_decode
|
46
|
+
- base64_encode
|
47
|
+
- base64_decode
|
48
|
+
- html_escape
|
49
|
+
- merge_word / add_class
|
50
|
+
- digitize
|
51
|
+
- camelize
|
52
|
+
- constantize
|
53
|
+
- wrap
|
54
|
+
- starts_with?
|
55
|
+
- ends_with?
|
56
|
+
- plural?
|
57
|
+
- singular?
|
58
|
+
- first
|
59
|
+
- last
|
60
|
+
- from
|
61
|
+
- file
|
62
|
+
- files
|
63
|
+
- path
|
64
|
+
- switchify
|
65
|
+
- word_frequency
|
66
|
+
- frequency_of_word
|
67
|
+
- to_md5
|
68
|
+
- to_sha512
|
69
|
+
|
70
|
+
* Integer
|
71
|
+
- ordanalize
|
72
|
+
|
73
|
+
* Numeric
|
74
|
+
- bytes, kilobytes, etc
|
75
|
+
- seconds, minutes, hours, days, ...
|
76
|
+
- to_seconds, to_minutes, to_hours, ...
|
77
|
+
- ago / before
|
78
|
+
- since / from_now
|
79
|
+
|
80
|
+
* Hash
|
81
|
+
- delete_at / extract!
|
82
|
+
- switchify
|
83
|
+
|
84
|
+
* Time
|
85
|
+
- in_words_since / in_words_since_now
|
86
|
+
- to_time
|
87
|
+
|
88
|
+
* Date
|
89
|
+
- to_time
|
90
|
+
|
91
|
+
* Module
|
92
|
+
- chainable
|
93
|
+
- extendable
|
94
|
+
- call_method
|
95
|
+
|
96
|
+
* Proc
|
97
|
+
- yield_or_eval
|
98
|
+
|
99
|
+
* Enumerable
|
100
|
+
- group_by
|
101
|
+
- includes_all?
|
102
|
+
- every
|
103
|
+
|
104
|
+
* Symbol
|
105
|
+
- to_proc
|
106
|
+
|
107
|
+
== Contrib
|
108
|
+
|
109
|
+
Feel free to fork Rext on github, or submit a ticket:
|
110
|
+
http://github.com/visionmedia/rext/issues
|
111
|
+
|
112
|
+
== License
|
113
|
+
|
114
|
+
(The MIT License)
|
115
|
+
|
116
|
+
Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
|
117
|
+
|
118
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
119
|
+
a copy of this software and associated documentation files (the
|
120
|
+
'Software'), to deal in the Software without restriction, including
|
121
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
122
|
+
distribute, sublicense, an d/or sell copies of the Software, and to
|
123
|
+
permit persons to whom the Software is furnished to do so, subject to
|
124
|
+
the following conditions:
|
125
|
+
|
126
|
+
The above copyright notice and this permission notice shall be
|
127
|
+
included in all copies or substantial portions of the Software.
|
128
|
+
|
129
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
130
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
131
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
132
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
133
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
134
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
135
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
136
|
+
ALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
$:.unshift 'lib'
|
3
|
+
require 'rext'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'rake'
|
6
|
+
require 'echoe'
|
7
|
+
|
8
|
+
Echoe.new "rext", Rext::VERSION do |p|
|
9
|
+
p.author = "TJ Holowaychuk"
|
10
|
+
p.email = "tj@vision-media.ca"
|
11
|
+
p.summary = "Ruby extensions"
|
12
|
+
p.url = "http://github.com/visionmedia/rext"
|
13
|
+
p.runtime_dependencies = []
|
14
|
+
end
|
15
|
+
|
16
|
+
Dir['tasks/**/*.rake'].sort.each { |lib| load lib }
|
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
list = %w( just some random foo bar like cookies )
|
3
|
+
|
4
|
+
benchmark 'Enumerable#group_by', :times => $times do
|
5
|
+
report 'with block' do
|
6
|
+
list.group_by { |item| item.length }
|
7
|
+
end
|
8
|
+
report 'with evaled block' do
|
9
|
+
list.group_by { length }
|
10
|
+
end
|
11
|
+
report 'with proxy' do
|
12
|
+
list.group_by.length
|
13
|
+
end
|
14
|
+
end
|
data/benchmarks/proc.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
|
2
|
+
def yield_or_eval_test &block
|
3
|
+
block.yield_or_eval 'foo'
|
4
|
+
end
|
5
|
+
|
6
|
+
def manual &block
|
7
|
+
foo = 'foo'
|
8
|
+
if block.arity > 0
|
9
|
+
yield foo
|
10
|
+
else
|
11
|
+
foo.instance_eval &block
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
benchmark 'Proc', :times => $times do
|
16
|
+
report '#yield_or_eval' do
|
17
|
+
yield_or_eval_test { length }
|
18
|
+
yield_or_eval_test { |foo| foo.length }
|
19
|
+
end
|
20
|
+
report 'manual arity check' do
|
21
|
+
manual { length }
|
22
|
+
manual { |foo| foo.length }
|
23
|
+
end
|
24
|
+
end
|
data/lib/rext.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
|
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
|
+
require 'rext/version'
|
data/lib/rext/all.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
#--
|
3
|
+
# Load all extensions.
|
4
|
+
#++
|
5
|
+
|
6
|
+
require 'rext/version'
|
7
|
+
require 'rext/symbol'
|
8
|
+
require 'rext/object'
|
9
|
+
require 'rext/module'
|
10
|
+
require 'rext/proc'
|
11
|
+
require 'rext/enumerable'
|
12
|
+
require 'rext/array'
|
13
|
+
require 'rext/hash'
|
14
|
+
require 'rext/numeric'
|
15
|
+
require 'rext/integer'
|
16
|
+
require 'rext/string'
|
17
|
+
require 'rext/date'
|
18
|
+
require 'rext/time'
|
data/lib/rext/array.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
|
2
|
+
require 'enumerator'
|
3
|
+
|
4
|
+
class Array
|
5
|
+
|
6
|
+
##
|
7
|
+
# Return array of elements after +position+.
|
8
|
+
|
9
|
+
def from position
|
10
|
+
self[position..-1]
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
# Return array of elements up to +position+.
|
15
|
+
|
16
|
+
def to position
|
17
|
+
self[0..position]
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# Pad array with expected length +n+, and +pad_with+ an
|
22
|
+
# optional object or nil.
|
23
|
+
#
|
24
|
+
# === Examples
|
25
|
+
#
|
26
|
+
# [1,2].pad(4) # => [1,2,nil,nil]
|
27
|
+
# [1,2].pad(4) # => [1,2,'x','x']
|
28
|
+
# [1,2].pad(2) # => [1,2]
|
29
|
+
#
|
30
|
+
|
31
|
+
def pad n, pad_with = nil
|
32
|
+
fill pad_with, length, n - length
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Split an array into chunks of length +n+. Optionally you
|
37
|
+
# may +pad_with+ an object to retain a uniform length per chunk.
|
38
|
+
#
|
39
|
+
# === Examples
|
40
|
+
#
|
41
|
+
# [1,2,3].chunk(2) # => [[1,2], [3, nil]]
|
42
|
+
# [1,2,3].chunk(2, 'x') # => [[1,2], [3, 'x']]
|
43
|
+
# [1,2,3].chunk(2, false) # => [[1,2], [3]]
|
44
|
+
# [1,2,3].in_groups_of(2) do |chunk|
|
45
|
+
# # Do something
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# === See
|
49
|
+
#
|
50
|
+
# * Array#pad
|
51
|
+
#
|
52
|
+
|
53
|
+
def chunk n, pad_with = nil, &block
|
54
|
+
returning [] do |chunks|
|
55
|
+
each_slice n do |chunk|
|
56
|
+
chunk.pad n, pad_with unless pad_with == false
|
57
|
+
yield chunk if block
|
58
|
+
chunks << chunk
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
alias :in_groups_of :chunk
|
63
|
+
|
64
|
+
end
|