rego-ruby-ext 0.0.1 → 0.0.2
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/VERSION +1 -1
- data/lib/hash.rb +39 -0
- data/lib/rego-ruby-ext.rb +1 -1
- data/rego-ruby-ext.gemspec +4 -2
- data/spec/hash_spec.rb +30 -0
- metadata +6 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/hash.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
# Merges self with another hash, recursively.
|
4
|
+
#
|
5
|
+
# This code was lovingly stolen from some random gem:
|
6
|
+
# http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
|
7
|
+
#
|
8
|
+
# Thanks to whoever made it.
|
9
|
+
|
10
|
+
def deep_merge(hash)
|
11
|
+
target = dup
|
12
|
+
|
13
|
+
hash.keys.each do |key|
|
14
|
+
if hash[key].is_a? Hash and self[key].is_a? Hash
|
15
|
+
target[key] = target[key].deep_merge(hash[key])
|
16
|
+
next
|
17
|
+
end
|
18
|
+
|
19
|
+
target[key] = hash[key]
|
20
|
+
end
|
21
|
+
|
22
|
+
target
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
# From: http://www.gemtacular.com/gemdocs/cerberus-0.2.2/doc/classes/Hash.html
|
27
|
+
# File lib/cerberus/utils.rb, line 42
|
28
|
+
|
29
|
+
def deep_merge!(second)
|
30
|
+
second.each_pair do |k,v|
|
31
|
+
if self[k].is_a?(Hash) and second[k].is_a?(Hash)
|
32
|
+
self[k].deep_merge!(second[k])
|
33
|
+
else
|
34
|
+
self[k] = second[k]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/lib/rego-ruby-ext.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
['nil', 'date', 'time', 'enumerable', 'numeric', 'symbol', 'string-interpolation', 'string'].each do |file_name|
|
1
|
+
['nil', 'date', 'time', 'enumerable', 'hash', 'numeric', 'symbol', 'string-interpolation', 'string'].each do |file_name|
|
2
2
|
require File.join(File.dirname(__FILE__), file_name)
|
3
3
|
end
|
4
4
|
|
data/rego-ruby-ext.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rego-ruby-ext}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alex Tkachev"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-09-14}
|
13
13
|
s.description = %q{Ruby core extensions that are common for all ReGO projects}
|
14
14
|
s.email = %q{tkachev.alex@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
"init.rb",
|
28
28
|
"lib/date.rb",
|
29
29
|
"lib/enumerable.rb",
|
30
|
+
"lib/hash.rb",
|
30
31
|
"lib/nil.rb",
|
31
32
|
"lib/numeric.rb",
|
32
33
|
"lib/rego-ruby-ext.rb",
|
@@ -37,6 +38,7 @@ Gem::Specification.new do |s|
|
|
37
38
|
"rego-ruby-ext.gemspec",
|
38
39
|
"spec/date_spec.rb",
|
39
40
|
"spec/enumerable_spec.rb",
|
41
|
+
"spec/hash_spec.rb",
|
40
42
|
"spec/nil_spec.rb",
|
41
43
|
"spec/numeric_spec.rb",
|
42
44
|
"spec/rego-ruby-ext_spec.rb",
|
data/spec/hash_spec.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
describe '#deep_merge' do
|
5
|
+
it 'should not modify original hash' do
|
6
|
+
h1 = {:a => 1, :b => 2}
|
7
|
+
h2 = h1.deep_merge(:c => 3)
|
8
|
+
h2.should_not == h1
|
9
|
+
h2.should include(:c)
|
10
|
+
h1.should_not include(:c)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should merge non deep hash regularly' do
|
14
|
+
h = {:a => 1, :b => 2}.deep_merge({:a => 3, :c => 4})
|
15
|
+
h.should include(:a)
|
16
|
+
h.should include(:c)
|
17
|
+
h[:c].should == 4
|
18
|
+
h[:a].should == 3
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should merge recursively' do
|
22
|
+
h = {:a => 1, :b =>2, :c => {:c1 => 1, :c2 => 2}, :d => 4}.deep_merge(:b => 'bb', :c => {:c2 => 'cc'})
|
23
|
+
h[:a].should == 1
|
24
|
+
h[:b].should == 'bb'
|
25
|
+
h[:c][:c1].should == 1
|
26
|
+
h[:c][:c2].should == 'cc'
|
27
|
+
h[:d].should == 4
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rego-ruby-ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alex Tkachev
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-09-14 00:00:00 +03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- init.rb
|
115
115
|
- lib/date.rb
|
116
116
|
- lib/enumerable.rb
|
117
|
+
- lib/hash.rb
|
117
118
|
- lib/nil.rb
|
118
119
|
- lib/numeric.rb
|
119
120
|
- lib/rego-ruby-ext.rb
|
@@ -124,6 +125,7 @@ files:
|
|
124
125
|
- rego-ruby-ext.gemspec
|
125
126
|
- spec/date_spec.rb
|
126
127
|
- spec/enumerable_spec.rb
|
128
|
+
- spec/hash_spec.rb
|
127
129
|
- spec/nil_spec.rb
|
128
130
|
- spec/numeric_spec.rb
|
129
131
|
- spec/rego-ruby-ext_spec.rb
|