amiel-hash_union_arrays 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ h2. Hash Union Arrays
2
+
3
+ Has a terrible name, but is very useful. This gem implements the | operator for Hash. Using it merges the two hashes together while applying set union to any common arrays.
4
+
5
+ h2. Author
6
+
7
+ Amiel Martin
@@ -0,0 +1,10 @@
1
+ require "rubygems"
2
+ require "rake/testtask"
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.test_files = FileList["test/test_*.rb"]
9
+ t.verbose = true
10
+ end
@@ -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
@@ -0,0 +1,3 @@
1
+ require File.join(File.dirname(__FILE__), *%w{.. lib hash_union_arrays})
2
+ require 'test/unit'
3
+
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
+