amiel-hash_union_arrays 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +7 -0
- data/Rakefile.rb +10 -0
- data/lib/hash_union_arrays.rb +21 -0
- data/test/test_hash_union_arrays.rb +28 -0
- data/test/test_helper.rb +3 -0
- metadata +59 -0
data/README.textile
ADDED
data/Rakefile.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module HashIntersectArrays
|
2
|
+
|
3
|
+
# acts just like Hash#merge, except that any arrays
|
4
|
+
def self.ordered_union(a, b)
|
5
|
+
for k in b.keys
|
6
|
+
if b[k].is_a?(Array) and a[k] and a[k].is_a?(Array)
|
7
|
+
a[k] |= b[k]
|
8
|
+
else
|
9
|
+
a[k] = b[k]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
return a
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Hash
|
18
|
+
def |(other)
|
19
|
+
HashIntersectArrays::ordered_union(self, other)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class HashUnionArraysTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@a = {
|
6
|
+
:an_array => %w(foo bar baz),
|
7
|
+
:an_element => 'a element',
|
8
|
+
:another_element => 'an a element',
|
9
|
+
}
|
10
|
+
@b = {
|
11
|
+
:an_array => %w(foo bat bat_country),
|
12
|
+
:an_element => 'b element',
|
13
|
+
:element_another => 'a b element',
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def test_hash_union
|
19
|
+
result = @a | @b
|
20
|
+
should = {
|
21
|
+
:an_array => %w(foo bar baz bat bat_country),
|
22
|
+
:an_element => 'b element',
|
23
|
+
:another_element => 'an a element',
|
24
|
+
:element_another => 'a b element',
|
25
|
+
}
|
26
|
+
assert_equal should, result
|
27
|
+
end
|
28
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: amiel-hash_union_arrays
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.2"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Amiel Martin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-17 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Allows a hash with arrays as values to merge and union common arrays instead of overwriting them.
|
17
|
+
email: amiel@tatango.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.textile
|
26
|
+
- lib
|
27
|
+
- lib/hash_union_arrays.rb
|
28
|
+
- Rakefile.rb
|
29
|
+
- test
|
30
|
+
- test/test_helper.rb
|
31
|
+
- test/test_hash_union_arrays.rb
|
32
|
+
has_rdoc: false
|
33
|
+
homepage:
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.2.0
|
55
|
+
signing_key:
|
56
|
+
specification_version: 2
|
57
|
+
summary: Allows a hash with arrays as values to merge and union common arrays instead of overwriting them.
|
58
|
+
test_files: []
|
59
|
+
|