mug 0.5.1 → 0.5.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.
- checksums.yaml +4 -4
- data/lib/mug.rb +1 -0
- data/lib/mug/hash.rb +1 -0
- data/lib/mug/hash/merge.rb +57 -0
- data/test/test-hashmerge.rb +39 -0
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62f380b7208bcca73fc03c01f9e768635cd86db6
|
4
|
+
data.tar.gz: 5c6fda3e95d3b1cf91a433e598bcccbe435647a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecb3583f89035c101ef30645cd188c2d1927901dbb4e0ab6f3e3db8d924e053b28de09ffc82834fa3300b18ff158958515c0c45a6240d039294006abf53c01b5
|
7
|
+
data.tar.gz: 55c48683d37e24b974982e5bd4f039e63df00b04fe7427b172932385f1df94001e829407f87446d29c8272b682fb4dc3946aa253761d85ab0265f1201df647bc
|
data/lib/mug.rb
CHANGED
@@ -9,6 +9,7 @@ require_relative 'mug/clamp'
|
|
9
9
|
require_relative 'mug/counts'
|
10
10
|
require_relative 'mug/fragile-method-chain'
|
11
11
|
require_relative 'mug/hash/map'
|
12
|
+
require_relative 'mug/hash/merge'
|
12
13
|
require_relative 'mug/hash/operations'
|
13
14
|
require_relative 'mug/iterator/for'
|
14
15
|
require_relative 'mug/iterator/method'
|
data/lib/mug/hash.rb
CHANGED
@@ -0,0 +1,57 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright (c) 2016, Matthew Kerwin <matthew@kerwin.net.au>
|
3
|
+
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
purpose with or without fee is hereby granted, provided that the above
|
6
|
+
copyright notice and this permission notice appear in all copies.
|
7
|
+
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
=end
|
16
|
+
|
17
|
+
|
18
|
+
class Hash
|
19
|
+
|
20
|
+
#
|
21
|
+
# Returns a new hash containing the contents of +other_hash+ and the
|
22
|
+
# contents of +hsh+. The value for each duplicate key is the value in
|
23
|
+
# +hsh+ when it exists.
|
24
|
+
#
|
25
|
+
def merge_left other_hash
|
26
|
+
merge(other_hash) {|key, left, right| left.nil? ? right : left }
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# Returns a new hash containing the contents of +other_hash+ and the
|
31
|
+
# contents of +hsh+. The value for each duplicate key is the value in
|
32
|
+
# +other_hash+ when it exists.
|
33
|
+
#
|
34
|
+
def merge_right other_hash
|
35
|
+
merge(other_hash) {|key, left, right| right.nil? ? left : right }
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# Adds the contents of +other_hash+ to +hsh+. Entries with duplicate
|
40
|
+
# keys are overwritten with the values from +other_hash+ if the
|
41
|
+
# values in +hsh+ are +nil+.
|
42
|
+
#
|
43
|
+
def merge_left! other_hash
|
44
|
+
merge!(other_hash) {|key, left, right| left.nil? ? right : left }
|
45
|
+
end
|
46
|
+
|
47
|
+
#
|
48
|
+
# Adds the contents of +other_hash+ to +hsh+. Entries with duplicate
|
49
|
+
# keys are overwritten with the values from +other_hash+ unless the
|
50
|
+
# values in +other_hash+ are +nil+.
|
51
|
+
#
|
52
|
+
def merge_right! other_hash
|
53
|
+
merge!(other_hash) {|key, left, right| right.nil? ? left : right }
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
$coercable = Object.new
|
5
|
+
def $coercable.to_h
|
6
|
+
{:a=>0, :z=>99}
|
7
|
+
end
|
8
|
+
|
9
|
+
require_relative '../lib/mug/hash/merge'
|
10
|
+
class Test_hashop < Test::Unit::TestCase
|
11
|
+
def test_hash_merge_left
|
12
|
+
l = { :a => 1, :b => 1, :d => nil, :e => 1 }
|
13
|
+
r = { :a => 2, :c => 2, :d => 2, :e => nil}
|
14
|
+
x = { :a => 1, :b => 1, :c => 2, :d => 2, :e => 1 }
|
15
|
+
assert_equal( x, l.merge_left(r) )
|
16
|
+
end
|
17
|
+
def test_hash_merge_right
|
18
|
+
l = { :a => 1, :b => 1, :d => nil, :e => 1 }
|
19
|
+
r = { :a => 2, :c => 2, :d => 2, :e => nil}
|
20
|
+
x = { :a => 2, :b => 1, :c => 2, :d => 2, :e => 1 }
|
21
|
+
assert_equal( x, l.merge_right(r) )
|
22
|
+
end
|
23
|
+
def test_hash_merge_left_bang
|
24
|
+
l = { :a => 1, :b => 1, :d => nil, :e => 1 }
|
25
|
+
r = { :a => 2, :c => 2, :d => 2, :e => nil}
|
26
|
+
x = { :a => 1, :b => 1, :c => 2, :d => 2, :e => 1 }
|
27
|
+
l.merge_left! r
|
28
|
+
assert_equal( x, l )
|
29
|
+
end
|
30
|
+
def test_hash_merge_right_bang
|
31
|
+
l = { :a => 1, :b => 1, :d => nil, :e => 1 }
|
32
|
+
r = { :a => 2, :c => 2, :d => 2, :e => nil}
|
33
|
+
x = { :a => 2, :b => 1, :c => 2, :d => 2, :e => 1 }
|
34
|
+
l.merge_right! r
|
35
|
+
assert_equal( x, l )
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
metadata
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Kerwin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
== MUG: Matty's Ultimate Gem
|
15
15
|
|
16
16
|
A collection of wonders to astound the mind!!
|
17
17
|
|
18
|
-
See the documentation at
|
18
|
+
See the documentation at https://github.com/phluid61/mug
|
19
19
|
email:
|
20
20
|
- matthew@kerwin.net.au
|
21
21
|
executables: []
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- lib/mug/fragile-method-chain.rb
|
36
36
|
- lib/mug/hash.rb
|
37
37
|
- lib/mug/hash/map.rb
|
38
|
+
- lib/mug/hash/merge.rb
|
38
39
|
- lib/mug/hash/operations.rb
|
39
40
|
- lib/mug/hashmap.rb
|
40
41
|
- lib/mug/hashop.rb
|
@@ -65,6 +66,7 @@ files:
|
|
65
66
|
- test/test-counts.rb
|
66
67
|
- test/test-fragile-method-chain.rb
|
67
68
|
- test/test-hashmap.rb
|
69
|
+
- test/test-hashmerge.rb
|
68
70
|
- test/test-hashop.rb
|
69
71
|
- test/test-iterator-for.rb
|
70
72
|
- test/test-iterator-method.rb
|
@@ -99,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
101
|
version: '0'
|
100
102
|
requirements: []
|
101
103
|
rubyforge_project:
|
102
|
-
rubygems_version: 2.
|
104
|
+
rubygems_version: 2.6.2
|
103
105
|
signing_key:
|
104
106
|
specification_version: 4
|
105
107
|
summary: 'MUG: Matty''s Ultimate Gem'
|
@@ -114,6 +116,7 @@ test_files:
|
|
114
116
|
- test/test-iterator-for.rb
|
115
117
|
- test/test-top.rb
|
116
118
|
- test/test-and-or.rb
|
119
|
+
- test/test-hashmerge.rb
|
117
120
|
- test/test-any-and-all.rb
|
118
121
|
- test/test-self.rb
|
119
122
|
- test/test-time.rb
|