deep_merge 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG +14 -3
- data/README.md +113 -0
- data/lib/deep_merge/core.rb +1 -1
- data/test/test_deep_merge.rb +12 -0
- metadata +39 -36
- data/README +0 -95
- data/VERSION +0 -1
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f913722448f3e72d18adb80b86afbb4a003c10de
|
4
|
+
data.tar.gz: f3823fab6b9906a8149f7b17325043ff84be3e17
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6aac94519acdebed405367cec544ca627e95523b9662d75bcf87f68b391f14d9c7f41c4ff6caca8b760eb4fa4fecc10686762c9c4ee5f54a5aa323e97172e2cf
|
7
|
+
data.tar.gz: f03e7183467b02ee60a4dfacbc056e107c449ca4d74d20a10481bc2e619ceb03eafdf27056563b846272a8ecdfd98e64fd17c5ee7e5bfb89d9d726bb45d6d5bb
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
2014-01-21 Dan DeLeo <dan@kallistec.com>
|
2
|
+
* Update knockout behavior to better handle nil (b7de40b5)
|
3
|
+
|
4
|
+
2011-08-15 Dan DeLeo <dan@kallistec.com>
|
5
|
+
* Document how to use w/ Rails 3 via Bundler
|
6
|
+
|
7
|
+
2011-07-28 Dan DeLeo <dan@kallistec.com>
|
8
|
+
* Use a plain ol' gemspec and Rakefile for gem creation
|
9
|
+
|
10
|
+
* Ship version 1.0.0
|
11
|
+
|
1
12
|
2011-05-23 Joe Van Dyk <joe@fixieconsulting.com>
|
2
13
|
|
3
14
|
* Added Changelog
|
@@ -10,12 +21,12 @@
|
|
10
21
|
|
11
22
|
* Removing extra whitespace
|
12
23
|
|
13
|
-
2010-01-11 Dan DeLeo <
|
24
|
+
2010-01-11 Dan DeLeo <dan@kallistec.com>
|
14
25
|
|
15
26
|
* fix boolean merging according to mdkent's patch explicitly test
|
16
27
|
for nils w/ #nil? instead of negating. Thanks mdkent!
|
17
28
|
|
18
|
-
2009-12-25 Dan DeLeo <
|
29
|
+
2009-12-25 Dan DeLeo <dan@kallistec.com>
|
19
30
|
|
20
31
|
* miscellaneous cleanup
|
21
32
|
|
@@ -23,7 +34,7 @@
|
|
23
34
|
|
24
35
|
* add jeweler rake task for gemability
|
25
36
|
|
26
|
-
2009-12-24 Dan DeLeo <
|
37
|
+
2009-12-24 Dan DeLeo <dan@kallistec.com>
|
27
38
|
|
28
39
|
* VERSION: Version bump to 0.0.1
|
29
40
|
|
data/README.md
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
DeepMerge Overview
|
2
|
+
==================
|
3
|
+
|
4
|
+
Deep Merge is a simple set of utility functions for Hash. It permits you to merge elements inside a hash together recursively. The manner by which it does this is somewhat arbitrary (since there is no defining standard for this) but it should end up being pretty intuitive and do what you expect.
|
5
|
+
|
6
|
+
You can learn a lot more about this by reading the test file. It's pretty well documented and has many examples of various merges from very simple to pretty complex.
|
7
|
+
|
8
|
+
The primary need that caused me to write this library is the merging of elements coming from HTTP parameters and related stored parameters in session. This lets a user build up a set of parameters over time, modifying individual items.
|
9
|
+
|
10
|
+
Deep Merge Core Documentation
|
11
|
+
=============================
|
12
|
+
|
13
|
+
`deep_merge!` method permits merging of arbitrary child elements. The two top level elements must be hashes. These hashes can contain unlimited (to stack limit) levels of child elements. These child elements to not have to be of the same types. Where child elements are of the same type, `deep_merge` will attempt to merge them together. Where child elements are not of the same type, `deep_merge` will skip or optionally overwrite the destination element with the contents of the source element at that level. So if you have two hashes like this:
|
14
|
+
|
15
|
+
source = {:x => [1,2,3], :y => 2}
|
16
|
+
dest = {:x => [4,5,'6'], :y => [7,8,9]}
|
17
|
+
dest.deep_merge!(source)
|
18
|
+
Results: {:x => [1,2,3,4,5,'6'], :y => 2}
|
19
|
+
|
20
|
+
By default, `deep_merge!` will overwrite any unmergeables and merge everything else. To avoid this, use `deep_merge` (no bang/exclamation mark)
|
21
|
+
|
22
|
+
Options
|
23
|
+
-------
|
24
|
+
|
25
|
+
Options are specified in the last parameter passed, which should be in hash format:
|
26
|
+
|
27
|
+
hash.deep_merge!({:x => [1,2]}, {:knockout_prefix => '--'})
|
28
|
+
:preserve_unmergeables DEFAULT: false
|
29
|
+
Set to true to skip any unmergeable elements from source
|
30
|
+
:knockout_prefix DEFAULT: nil
|
31
|
+
Set to string value to signify prefix which deletes elements from existing element
|
32
|
+
:sort_merged_arrays DEFAULT: false
|
33
|
+
Set to true to sort all arrays that are merged together
|
34
|
+
:unpack_arrays DEFAULT: nil
|
35
|
+
Set to string value to run "Array::join" then "String::split" against all arrays
|
36
|
+
:merge_hash_arrays DEFAULT: false
|
37
|
+
Set to true to merge hashes within arrays
|
38
|
+
:merge_debug DEFAULT: false
|
39
|
+
Set to true to get console output of merge process for debugging
|
40
|
+
|
41
|
+
Selected Options Details
|
42
|
+
------------------------
|
43
|
+
|
44
|
+
**:knockout_prefix**
|
45
|
+
|
46
|
+
The purpose of this is to provide a way to remove elements from existing Hash by specifying them in a special way in incoming hash
|
47
|
+
|
48
|
+
source = {:x => ['--1', '2']}
|
49
|
+
dest = {:x => ['1', '3']}
|
50
|
+
dest.ko_deep_merge!(source)
|
51
|
+
Results: {:x => ['2','3']}
|
52
|
+
|
53
|
+
Additionally, if the knockout_prefix is passed alone as a string, it will cause the entire element to be removed:
|
54
|
+
|
55
|
+
source = {:x => '--'}
|
56
|
+
dest = {:x => [1,2,3]}
|
57
|
+
dest.ko_deep_merge!(source)
|
58
|
+
Results: {:x => ""}
|
59
|
+
|
60
|
+
**:unpack_arrays**
|
61
|
+
|
62
|
+
The purpose of this is to permit compound elements to be passed in as strings and to be converted into discrete array elements
|
63
|
+
|
64
|
+
irsource = {:x => ['1,2,3', '4']}
|
65
|
+
dest = {:x => ['5','6','7,8']}
|
66
|
+
dest.deep_merge!(source, {:unpack_arrays => ','})
|
67
|
+
Results: {:x => ['1','2','3','4','5','6','7','8'}
|
68
|
+
|
69
|
+
Why: If receiving data from an HTML form, this makes it easy for a checkbox to pass multiple values from within a single HTML element
|
70
|
+
|
71
|
+
**:merge_hash_arrays**
|
72
|
+
|
73
|
+
merge hashes within arrays
|
74
|
+
|
75
|
+
source = {:x => [{:y => 1}]}
|
76
|
+
dest = {:x => [{:z => 2}]}
|
77
|
+
dest.deep_merge!(source, {:merge_hash_arrays => true})
|
78
|
+
Results: {:x => [{:y => 1, :z => 2}]}
|
79
|
+
|
80
|
+
There are many tests for this library - and you can learn more about the features and usages of deep_merge! by just browsing the test examples.
|
81
|
+
|
82
|
+
Using deep_merge in Rails
|
83
|
+
=========================
|
84
|
+
|
85
|
+
To avoid conflict with ActiveSupport, load deep_merge via:
|
86
|
+
|
87
|
+
require 'deep_merge/rails_compat'
|
88
|
+
|
89
|
+
In a Gemfile:
|
90
|
+
|
91
|
+
gem "deep_merge", :require => 'deep_merge/rails_compat'
|
92
|
+
|
93
|
+
The deep_merge methods will then be defined as
|
94
|
+
|
95
|
+
Hash#deeper_merge
|
96
|
+
Hash#deeper_merge!
|
97
|
+
Hash#ko_deeper_merge!
|
98
|
+
|
99
|
+
Simple Example Code
|
100
|
+
===================
|
101
|
+
|
102
|
+
require 'deep_merge'
|
103
|
+
x = {:x => [3,4,5]}
|
104
|
+
y = {:x => [1,2,3]}
|
105
|
+
y.deep_merge!(x)
|
106
|
+
# results: y = {:x => [1,2,3,4,5]}
|
107
|
+
|
108
|
+
Availablility
|
109
|
+
=============
|
110
|
+
|
111
|
+
`deep_merge` was written by Steve Midgley, and is now maintained by Daniel DeLeo. The official home of `deep_merge` on the internet is now https://github.com/danielsdeleo/deep_merge
|
112
|
+
|
113
|
+
Copyright (c) 2008 Steve Midgley, released under the MIT license
|
data/lib/deep_merge/core.rb
CHANGED
@@ -123,7 +123,7 @@ module DeepMerge
|
|
123
123
|
end
|
124
124
|
end
|
125
125
|
# if there's a naked knockout_prefix in source, that means we are to truncate dest
|
126
|
-
if source.index(knockout_prefix)
|
126
|
+
if knockout_prefix && source.index(knockout_prefix)
|
127
127
|
dest = clear_or_nil(dest); source.delete(knockout_prefix)
|
128
128
|
end
|
129
129
|
if dest.kind_of?(Array)
|
data/test/test_deep_merge.rb
CHANGED
@@ -201,6 +201,18 @@ class TestDeepMerge < Test::Unit::TestCase
|
|
201
201
|
DeepMerge::deep_merge!(hash_src, hash_dst)
|
202
202
|
assert_equal({"property" => {"bedroom_count" => {2=>3, "king_bed" => [3]}, "bathroom_count" => ["1"]}}, hash_dst)
|
203
203
|
|
204
|
+
# 3 hash layers holding arrays of int, but source includes a nil in the array
|
205
|
+
hash_src = {"property" => {"bedroom_count" => {"king_bed" => [nil], "queen_bed" => [1, nil]}, "bathroom_count" => [nil, "1"]}}
|
206
|
+
hash_dst = {"property" => {"bedroom_count" => {"king_bed" => [2], "queen_bed" => [4]}, "bathroom_count" => ["2"]}}
|
207
|
+
DeepMerge::deep_merge!(hash_src, hash_dst)
|
208
|
+
assert_equal({"property" => {"bedroom_count" => {"king_bed" => [2,nil], "queen_bed" => [4, 1, nil]}, "bathroom_count" => ["2", nil, "1"]}}, hash_dst)
|
209
|
+
|
210
|
+
# 3 hash layers holding arrays of int, but destination includes a nil in the array
|
211
|
+
hash_src = {"property" => {"bedroom_count" => {"king_bed" => [3], "queen_bed" => [1]}, "bathroom_count" => ["1"]}}
|
212
|
+
hash_dst = {"property" => {"bedroom_count" => {"king_bed" => [nil], "queen_bed" => [4, nil]}, "bathroom_count" => [nil,"2"]}}
|
213
|
+
DeepMerge::deep_merge!(hash_src, hash_dst)
|
214
|
+
assert_equal({"property" => {"bedroom_count" => {"king_bed" => [nil, 3], "queen_bed" => [4, nil, 1]}, "bathroom_count" => [nil, "2", "1"]}}, hash_dst)
|
215
|
+
|
204
216
|
# test parameter management for knockout_prefix and overwrite unmergable
|
205
217
|
assert_raise(DeepMerge::InvalidParameter) {DeepMerge::deep_merge!(hash_src, hash_dst, {:knockout_prefix => ""})}
|
206
218
|
assert_raise(DeepMerge::InvalidParameter) {DeepMerge::deep_merge!(hash_src, hash_dst, {:preserve_unmergeables => true, :knockout_prefix => ""})}
|
metadata
CHANGED
@@ -1,64 +1,67 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: deep_merge
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 1.0.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
6
5
|
platform: ruby
|
7
|
-
authors:
|
6
|
+
authors:
|
8
7
|
- Steve Midgley
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
date: 2011-07-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.1'
|
17
27
|
description: Recursively merge hashes. Now works with Ruby 1.9 and ActiveSupport
|
18
28
|
email: dan@kallistec.com
|
19
29
|
executables: []
|
20
|
-
|
21
30
|
extensions: []
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
files:
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.md
|
33
|
+
files:
|
26
34
|
- CHANGELOG
|
27
|
-
- README
|
35
|
+
- README.md
|
28
36
|
- Rakefile
|
29
|
-
- VERSION
|
30
37
|
- lib/deep_merge.rb
|
31
38
|
- lib/deep_merge/core.rb
|
32
39
|
- lib/deep_merge/deep_merge_hash.rb
|
33
40
|
- lib/deep_merge/rails_compat.rb
|
34
41
|
- test/test_deep_merge.rb
|
35
|
-
has_rdoc: true
|
36
42
|
homepage: http://github.com/danielsdeleo/deep_merge
|
37
|
-
licenses:
|
38
|
-
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata: {}
|
39
46
|
post_install_message:
|
40
47
|
rdoc_options: []
|
41
|
-
|
42
|
-
require_paths:
|
48
|
+
require_paths:
|
43
49
|
- lib
|
44
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
-
|
46
|
-
requirements:
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
47
52
|
- - ">="
|
48
|
-
- !ruby/object:Gem::Version
|
49
|
-
version:
|
50
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
-
|
52
|
-
requirements:
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
53
57
|
- - ">="
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
version:
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
56
60
|
requirements: []
|
57
|
-
|
58
61
|
rubyforge_project:
|
59
|
-
rubygems_version: 1.
|
62
|
+
rubygems_version: 2.1.11
|
60
63
|
signing_key:
|
61
|
-
specification_version:
|
64
|
+
specification_version: 4
|
62
65
|
summary: Merge Deeply Nested Hashes
|
63
|
-
test_files:
|
66
|
+
test_files:
|
64
67
|
- test/test_deep_merge.rb
|
data/README
DELETED
@@ -1,95 +0,0 @@
|
|
1
|
-
DeepMerge Overview
|
2
|
-
==================
|
3
|
-
|
4
|
-
Deep Merge is a simple set of utility functions for Hash. It permits
|
5
|
-
you to merge elements inside a hash together recursively. The manner
|
6
|
-
by which it does this is somewhat arbitrary (since there is no defining
|
7
|
-
standard for this) but it should end up being pretty intuitive and do what
|
8
|
-
you expect.
|
9
|
-
|
10
|
-
You can learn a lot more about this by reading the test file. It's pretty
|
11
|
-
well documented and has many examples of various merges from very simple
|
12
|
-
to pretty complex.
|
13
|
-
|
14
|
-
The primary need that caused me to write this library is the merging of elements
|
15
|
-
coming from HTTP parameters and related stored parameters in session. This lets
|
16
|
-
a user build up a set of parameters over time, modifying individual items.
|
17
|
-
|
18
|
-
Deep Merge Core Documentation
|
19
|
-
=============================
|
20
|
-
deep_merge! method permits merging of arbitrary child elements. The two top level
|
21
|
-
elements must be hashes. These hashes can contain unlimited (to stack limit) levels
|
22
|
-
of child elements. These child elements to not have to be of the same types.
|
23
|
-
Where child elements are of the same type, deep_merge will attempt to merge them together.
|
24
|
-
Where child elements are not of the same type, deep_merge will skip or optionally overwrite
|
25
|
-
the destination element with the contents of the source element at that level.
|
26
|
-
So if you have two hashes like this:
|
27
|
-
source = {:x => [1,2,3], :y => 2}
|
28
|
-
dest = {:x => [4,5,'6'], :y => [7,8,9]}
|
29
|
-
dest.deep_merge!(source)
|
30
|
-
Results: {:x => [1,2,3,4,5,'6'], :y => 2}
|
31
|
-
By default, "deep_merge!" will overwrite any unmergeables and merge everything else.
|
32
|
-
To avoid this, use "deep_merge" (no bang/exclamation mark)
|
33
|
-
|
34
|
-
Options:
|
35
|
-
Options are specified in the last parameter passed, which should be in hash format:
|
36
|
-
hash.deep_merge!({:x => [1,2]}, {:knockout_prefix => '--'})
|
37
|
-
:preserve_unmergeables DEFAULT: false
|
38
|
-
Set to true to skip any unmergeable elements from source
|
39
|
-
:knockout_prefix DEFAULT: nil
|
40
|
-
Set to string value to signify prefix which deletes elements from existing element
|
41
|
-
:sort_merged_arrays DEFAULT: false
|
42
|
-
Set to true to sort all arrays that are merged together
|
43
|
-
:unpack_arrays DEFAULT: nil
|
44
|
-
Set to string value to run "Array::join" then "String::split" against all arrays
|
45
|
-
:merge_hash_arrays DEFAULT: false
|
46
|
-
Set to true to merge hashes within arrays
|
47
|
-
:merge_debug DEFAULT: false
|
48
|
-
Set to true to get console output of merge process for debugging
|
49
|
-
|
50
|
-
Selected Options Details:
|
51
|
-
:knockout_prefix => The purpose of this is to provide a way to remove elements
|
52
|
-
from existing Hash by specifying them in a special way in incoming hash
|
53
|
-
source = {:x => ['--1', '2']}
|
54
|
-
dest = {:x => ['1', '3']}
|
55
|
-
dest.ko_deep_merge!(source)
|
56
|
-
Results: {:x => ['2','3']}
|
57
|
-
Additionally, if the knockout_prefix is passed alone as a string, it will cause
|
58
|
-
the entire element to be removed:
|
59
|
-
source = {:x => '--'}
|
60
|
-
dest = {:x => [1,2,3]}
|
61
|
-
dest.ko_deep_merge!(source)
|
62
|
-
Results: {:x => ""}
|
63
|
-
:unpack_arrays => The purpose of this is to permit compound elements to be passed
|
64
|
-
in as strings and to be converted into discrete array elements
|
65
|
-
irsource = {:x => ['1,2,3', '4']}
|
66
|
-
dest = {:x => ['5','6','7,8']}
|
67
|
-
dest.deep_merge!(source, {:unpack_arrays => ','})
|
68
|
-
Results: {:x => ['1','2','3','4','5','6','7','8'}
|
69
|
-
Why: If receiving data from an HTML form, this makes it easy for a checkbox
|
70
|
-
to pass multiple values from within a single HTML element
|
71
|
-
:merge_hash_arrays => merge hashes within arrays
|
72
|
-
source = {:x => [{:y => 1}]}
|
73
|
-
dest = {:x => [{:z => 2}]}
|
74
|
-
dest.deep_merge!(source, {:merge_hash_arrays => true})
|
75
|
-
Results: {:x => [{:y => 1, :z => 2}]}
|
76
|
-
|
77
|
-
There are many tests for this library - and you can learn more about the features
|
78
|
-
and usages of deep_merge! by just browsing the test examples
|
79
|
-
|
80
|
-
|
81
|
-
Simple Example Code
|
82
|
-
===================
|
83
|
-
|
84
|
-
require 'deep_merge'
|
85
|
-
x = {:x => [3,4,5]}
|
86
|
-
y = {:x => [1,2,3]}
|
87
|
-
y.deep_merge!(x)
|
88
|
-
# results: y = {:x => [1,2,3,4,5]}
|
89
|
-
|
90
|
-
Availablility
|
91
|
-
=============
|
92
|
-
SVN Repo here: http://trac.misuse.org/science/wiki/DeepMerge
|
93
|
-
Contact author: http://www.misuse.org/science
|
94
|
-
|
95
|
-
Copyright (c) 2008 Steve Midgley, released under the MIT license
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.0.1
|