tworgy-ruby 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/tworgy/{recursively.rb → hash_and_array_extensions.rb} +18 -4
- data/lib/tworgy/testing.rb +12 -0
- data/lib/tworgy-ruby-testing.rb +1 -0
- data/lib/tworgy-ruby.rb +1 -1
- data/spec/lib/tworgy/hash_and_array_extensions_spec.rb +57 -0
- data/spec/lib/tworgy/testing_spec.rb +17 -0
- data/spec/spec_helper.rb +1 -0
- metadata +10 -6
- data/spec/lib/tworgy/recursively_spec.rb +0 -31
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ begin
|
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
7
|
gem.name = "tworgy-ruby"
|
8
8
|
gem.summary = %Q{Handy Ruby extensions}
|
9
|
-
gem.description = %Q{
|
9
|
+
gem.description = %Q{Handy Ruby extensions}
|
10
10
|
gem.email = "code@tworgy.com"
|
11
11
|
gem.homepage = "http://github.com/matholroyd/tworgy-ruby"
|
12
12
|
gem.authors = ["Mat Holroyd"]
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
1
3
|
class Array
|
2
4
|
def recursively(&block)
|
3
5
|
map do |item|
|
@@ -12,6 +14,10 @@ class Array
|
|
12
14
|
def recursively!(&block)
|
13
15
|
replace(recursively(&block))
|
14
16
|
end
|
17
|
+
|
18
|
+
def ostructify
|
19
|
+
collect { |i| (i.is_a?(Hash) || i.is_a?(Array)) ? i.ostructify : i }
|
20
|
+
end
|
15
21
|
end
|
16
22
|
|
17
23
|
class Hash
|
@@ -25,16 +31,24 @@ class Hash
|
|
25
31
|
yield hash
|
26
32
|
end
|
27
33
|
end
|
28
|
-
|
34
|
+
|
29
35
|
def recursively!(&block)
|
30
36
|
replace(recursively(&block))
|
31
37
|
end
|
32
38
|
|
33
39
|
def ostructify
|
34
|
-
|
35
|
-
hash[key] = value.is_a?(Hash) ? value.ostructify : value
|
40
|
+
temp = inject({}) do |hash, (key, value)|
|
41
|
+
hash[key] = (value.is_a?(Hash) || value.is_a?(Array)) ? value.ostructify : value
|
36
42
|
hash
|
37
43
|
end
|
38
|
-
OpenStruct.new
|
44
|
+
result = OpenStruct.new temp
|
45
|
+
|
46
|
+
if result.instance_variable_get('@table')[:id] != nil
|
47
|
+
def result.id
|
48
|
+
instance_variable_get('@table')[:id]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
result
|
39
52
|
end
|
40
53
|
end
|
54
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'tworgy/testing'
|
data/lib/tworgy-ruby.rb
CHANGED
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
|
2
|
+
|
3
|
+
describe "recursively" do
|
4
|
+
|
5
|
+
describe Hash do
|
6
|
+
|
7
|
+
it 'should symbolize the keys recursively' do
|
8
|
+
{'a' => {'b' => 3}}.recursively { |hash| symbolize_keys(hash) }.should == {:a => {:b => 3}}
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'ostructify' do
|
12
|
+
before :each do
|
13
|
+
@object = {:person => {:name => 'bob', :age => 5}, :array => [{:something => 'in the array'}]}.ostructify
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should turn a nested hash into nested OpenStructs' do
|
17
|
+
@object.person.name.should == 'bob'
|
18
|
+
@object.person.age.should == 5
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should keep ostructify all the way past arrays' do
|
22
|
+
@object.array[0].something.should == 'in the array'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should replace the "def id" if id was defined, so dont get those annoying ruby warnings' do
|
26
|
+
capture_stderr {
|
27
|
+
o = {:id => 1}.ostructify
|
28
|
+
o.id.should == 1
|
29
|
+
}.should == ""
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe Array do
|
36
|
+
describe 'ostructify' do
|
37
|
+
before :each do
|
38
|
+
@object = [{:person => {:name => 'bob', :age => 5}}, {:animal => 'dog'}].ostructify
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should recursively ostructify things in the array' do
|
42
|
+
@object[0].person.name.should == 'bob'
|
43
|
+
@object[0].person.age.should == 5
|
44
|
+
@object[1].animal.should == 'dog'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def symbolize_keys(hash)
|
51
|
+
hash.inject({}) do |result, (key, value)|
|
52
|
+
result[key.to_sym] = value
|
53
|
+
result
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
|
2
|
+
|
3
|
+
describe 'rspec helper' do
|
4
|
+
it 'should capture nothing' do
|
5
|
+
capture_stderr {
|
6
|
+
1 + 1
|
7
|
+
1.to_s
|
8
|
+
}.should == ""
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should capture errors on stderr' do
|
12
|
+
capture_stderr {
|
13
|
+
[].id
|
14
|
+
}.should =~ /Object#id will be deprecated/
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tworgy-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mat Holroyd
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-12-08 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -22,7 +22,7 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: 1.2.9
|
24
24
|
version:
|
25
|
-
description:
|
25
|
+
description: Handy Ruby extensions
|
26
26
|
email: code@tworgy.com
|
27
27
|
executables: []
|
28
28
|
|
@@ -35,12 +35,15 @@ files:
|
|
35
35
|
- LICENSE
|
36
36
|
- README.rdoc
|
37
37
|
- Rakefile
|
38
|
+
- lib/tworgy-ruby-testing.rb
|
38
39
|
- lib/tworgy-ruby.rb
|
39
40
|
- lib/tworgy/dbc.rb
|
40
|
-
- lib/tworgy/
|
41
|
+
- lib/tworgy/hash_and_array_extensions.rb
|
41
42
|
- lib/tworgy/standard_extensions.rb
|
43
|
+
- lib/tworgy/testing.rb
|
42
44
|
- lib/tworgy/tmail.rb
|
43
|
-
- spec/lib/tworgy/
|
45
|
+
- spec/lib/tworgy/hash_and_array_extensions_spec.rb
|
46
|
+
- spec/lib/tworgy/testing_spec.rb
|
44
47
|
- spec/spec.opts
|
45
48
|
- spec/spec_helper.rb
|
46
49
|
has_rdoc: true
|
@@ -72,5 +75,6 @@ signing_key:
|
|
72
75
|
specification_version: 3
|
73
76
|
summary: Handy Ruby extensions
|
74
77
|
test_files:
|
75
|
-
- spec/lib/tworgy/
|
78
|
+
- spec/lib/tworgy/hash_and_array_extensions_spec.rb
|
79
|
+
- spec/lib/tworgy/testing_spec.rb
|
76
80
|
- spec/spec_helper.rb
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
|
2
|
-
|
3
|
-
describe "recursively" do
|
4
|
-
|
5
|
-
describe Hash do
|
6
|
-
|
7
|
-
it 'should symbolize the keys recursively' do
|
8
|
-
{'a' => {'b' => 3}}.recursively { |hash| symbolize_keys(hash) }.should == {:a => {:b => 3}}
|
9
|
-
end
|
10
|
-
|
11
|
-
describe 'ostructify' do
|
12
|
-
before :each do
|
13
|
-
@object = {:person => {:name => 'bob', :age => 5}}.ostructify
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'should turn a nested hash into nested OpenStructs' do
|
17
|
-
@object.person.name.should == 'bob'
|
18
|
-
@object.person.age.should == 5
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def symbolize_keys(hash)
|
25
|
-
hash.inject({}) do |result, (key, value)|
|
26
|
-
result[key.to_sym] = value
|
27
|
-
result
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
|