rext 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +8 -0
- data/README.rdoc +2 -3
- data/lib/rext/hash/helpers.rb +58 -0
- data/lib/rext/string/helpers.rb +7 -37
- data/lib/rext/version.rb +1 -1
- data/rext.gemspec +2 -2
- data/spec/hash_spec.rb +32 -0
- data/spec/string_spec.rb +6 -18
- metadata +2 -2
data/History.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -47,6 +47,7 @@ Below are the methods currently provided by Rext.
|
|
47
47
|
- base64_decode
|
48
48
|
- html_escape
|
49
49
|
- merge_word / add_class
|
50
|
+
- remove
|
50
51
|
- digitize
|
51
52
|
- camelize
|
52
53
|
- constantize
|
@@ -58,9 +59,6 @@ Below are the methods currently provided by Rext.
|
|
58
59
|
- first
|
59
60
|
- last
|
60
61
|
- from
|
61
|
-
- file
|
62
|
-
- files
|
63
|
-
- path
|
64
62
|
- switchify
|
65
63
|
- word_frequency
|
66
64
|
- frequency_of_word
|
@@ -81,6 +79,7 @@ Below are the methods currently provided by Rext.
|
|
81
79
|
- delete_at / extract!
|
82
80
|
- switchify
|
83
81
|
- to_query
|
82
|
+
- apply_to / apply_to!
|
84
83
|
|
85
84
|
* Time
|
86
85
|
- in_words_since / in_words_since_now
|
data/lib/rext/hash/helpers.rb
CHANGED
@@ -1,6 +1,64 @@
|
|
1
1
|
|
2
2
|
class Hash
|
3
3
|
|
4
|
+
##
|
5
|
+
# Apply values to _object_'s accessors.
|
6
|
+
#
|
7
|
+
# === Examples
|
8
|
+
#
|
9
|
+
# class Foo
|
10
|
+
# attr_accessors :foo
|
11
|
+
# def initialize options = {}
|
12
|
+
# options.apply_to self
|
13
|
+
# end
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# foo = Foo.new :foo => 'bar'
|
17
|
+
# foo.foo # => 'bar'
|
18
|
+
#
|
19
|
+
# foo = Foo.new :something => 'else'
|
20
|
+
# # => NoMethodError because :something= does not exist
|
21
|
+
#
|
22
|
+
# === See
|
23
|
+
#
|
24
|
+
# * Hash#apply_to!
|
25
|
+
#
|
26
|
+
|
27
|
+
def apply_to object
|
28
|
+
each do |key, value|
|
29
|
+
object.send :"#{key}=", value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# Apply values to _object_'s accessors, checking
|
35
|
+
# if the object responds to each key.
|
36
|
+
#
|
37
|
+
# === Examples
|
38
|
+
#
|
39
|
+
# class Foo
|
40
|
+
# attr_accessors :foo
|
41
|
+
# def initialize options = {}
|
42
|
+
# options.apply_to! self
|
43
|
+
# end
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# foo = Foo.new :foo => 'bar'
|
47
|
+
# foo.foo # => 'bar'
|
48
|
+
#
|
49
|
+
# foo = Foo.new :something => 'else'
|
50
|
+
# # => nothing is raised
|
51
|
+
#
|
52
|
+
# === See
|
53
|
+
#
|
54
|
+
# * Hash#apply_to
|
55
|
+
#
|
56
|
+
|
57
|
+
def apply_to! object
|
58
|
+
each do |key, value|
|
59
|
+
object.send :"#{key}=", value if object.respond_to? :"#{key}="
|
60
|
+
end
|
61
|
+
end
|
4
62
|
|
5
63
|
##
|
6
64
|
# Convert self to an HTTP query string.
|
data/lib/rext/string/helpers.rb
CHANGED
@@ -3,43 +3,6 @@ require 'extlib'
|
|
3
3
|
|
4
4
|
class String
|
5
5
|
|
6
|
-
##
|
7
|
-
# Returns a File instance.
|
8
|
-
#
|
9
|
-
# === Examples
|
10
|
-
#
|
11
|
-
# 'History.rdoc'.file.mtime
|
12
|
-
#
|
13
|
-
|
14
|
-
def file
|
15
|
-
File.new self
|
16
|
-
end
|
17
|
-
|
18
|
-
##
|
19
|
-
# Returns a Pathname instance.
|
20
|
-
#
|
21
|
-
# === Examples
|
22
|
-
#
|
23
|
-
# 'lib'.path.join('foo').expand_path
|
24
|
-
#
|
25
|
-
|
26
|
-
def path
|
27
|
-
require 'pathname'
|
28
|
-
Pathname.new self
|
29
|
-
end
|
30
|
-
|
31
|
-
##
|
32
|
-
# Returns an array of files matching self.
|
33
|
-
#
|
34
|
-
# === Examples
|
35
|
-
#
|
36
|
-
# 'lib/**/*.rb'.files
|
37
|
-
#
|
38
|
-
|
39
|
-
def files
|
40
|
-
Dir[self]
|
41
|
-
end
|
42
|
-
|
43
6
|
##
|
44
7
|
# Merge the +word+ passed into the string. This
|
45
8
|
# is useful for adding classes which may already
|
@@ -196,6 +159,13 @@ class String
|
|
196
159
|
tr '_', '-'
|
197
160
|
end
|
198
161
|
|
162
|
+
##
|
163
|
+
# Shortcut for #gsub!(regexp, '')
|
164
|
+
|
165
|
+
def remove regexp
|
166
|
+
gsub! regexp, ''
|
167
|
+
end
|
168
|
+
|
199
169
|
##
|
200
170
|
# Return hash of word frequencies.
|
201
171
|
#
|
data/lib/rext/version.rb
CHANGED
data/rext.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{rext}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.6.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-11-
|
9
|
+
s.date = %q{2009-11-18}
|
10
10
|
s.description = %q{Ruby extensions}
|
11
11
|
s.email = %q{tj@vision-media.ca}
|
12
12
|
s.extra_rdoc_files = ["README.rdoc", "lib/rext.rb", "lib/rext/all.rb", "lib/rext/array.rb", "lib/rext/array/helpers.rb", "lib/rext/date.rb", "lib/rext/date/helpers.rb", "lib/rext/enumerable.rb", "lib/rext/enumerable/helpers.rb", "lib/rext/hash.rb", "lib/rext/hash/helpers.rb", "lib/rext/integer.rb", "lib/rext/integer/helpers.rb", "lib/rext/module.rb", "lib/rext/module/helpers.rb", "lib/rext/numeric.rb", "lib/rext/numeric/bytes.rb", "lib/rext/numeric/time.rb", "lib/rext/object.rb", "lib/rext/object/helpers.rb", "lib/rext/object/metaclass.rb", "lib/rext/proc.rb", "lib/rext/proc/helpers.rb", "lib/rext/process.rb", "lib/rext/process/helpers.rb", "lib/rext/string.rb", "lib/rext/string/encode.rb", "lib/rext/string/helpers.rb", "lib/rext/symbol.rb", "lib/rext/symbol/helpers.rb", "lib/rext/time.rb", "lib/rext/time/helpers.rb", "lib/rext/version.rb", "tasks/benchmark.rake", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
|
data/spec/hash_spec.rb
CHANGED
@@ -14,6 +14,38 @@ describe Hash do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
describe "#apply_to" do
|
18
|
+
before :each do
|
19
|
+
@user = Struct.new(:name, :age).new
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should assign key values to accessors of the same name" do
|
23
|
+
{ :name => 'Tj', :age => 22 }.apply_to(@user)
|
24
|
+
@user.name.should == 'Tj'
|
25
|
+
@user.age.should == 22
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should raise an error when the accessor does not exist" do
|
29
|
+
lambda { { :foo => 'bar' }.apply_to(@user) }.should raise_error(NoMethodError)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#apply_to" do
|
34
|
+
before :each do
|
35
|
+
@user = Struct.new(:name, :age).new
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should assign key values to accessors of the same name" do
|
39
|
+
{ :name => 'Tj', :age => 22 }.apply_to!(@user)
|
40
|
+
@user.name.should == 'Tj'
|
41
|
+
@user.age.should == 22
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should not raise an error when the accessor does not exist" do
|
45
|
+
lambda { { :foo => 'bar'}.apply_to!(@user) }.should_not raise_error
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
17
49
|
describe "#to_query" do
|
18
50
|
it "should build a query string" do
|
19
51
|
{ :foo => 'bar' }.to_query.should == 'foo=bar'
|
data/spec/string_spec.rb
CHANGED
@@ -68,24 +68,6 @@ describe String do
|
|
68
68
|
lambda { ''.switchify }.should raise_error(String::InvalidSwitchError)
|
69
69
|
end
|
70
70
|
end
|
71
|
-
|
72
|
-
describe "#path" do
|
73
|
-
it "should return an instance of a pathname" do
|
74
|
-
'History.rdoc'.path.should be_an_instance_of(Pathname)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
describe "#files" do
|
79
|
-
it "should return result of Dir glob" do
|
80
|
-
'lib/*.*'.files.should be_an_instance_of(Array)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
describe "#file" do
|
85
|
-
it "should return an instance of a file" do
|
86
|
-
'History.rdoc'.file.should be_an_instance_of(File)
|
87
|
-
end
|
88
|
-
end
|
89
71
|
|
90
72
|
describe "#add_class" do
|
91
73
|
it "should add a word that does not exist" do
|
@@ -190,5 +172,11 @@ describe String do
|
|
190
172
|
end
|
191
173
|
end
|
192
174
|
|
175
|
+
describe "#remove" do
|
176
|
+
it "should remove the matched pattern" do
|
177
|
+
'foobarbazbar'.remove(/bar/).should == 'foobaz'
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
193
181
|
end
|
194
182
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.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-11-
|
12
|
+
date: 2009-11-18 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|