y_support 2.0.31 → 2.0.33
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/y_support/core_ext/class/misc.rb +1 -0
- data/lib/y_support/core_ext/hash/misc.rb +45 -13
- data/lib/y_support/name_magic.rb +1 -1
- data/lib/y_support/version.rb +1 -1
- data/test/misc_test.rb +5 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 053b9bd4ab69ab18a27d4e0cdd2fd1b994a601f0
|
4
|
+
data.tar.gz: 63f43d3edd19d69ea8a7f7c060096c3b75071fb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31264bdb2e2e5f61d1d3e7a5d94a1cfd1b0a789b5870b9cab68e4e6729eb07a1d877711e33acfc3adbccdf24f659b5b63fe4d1a9913fc49e35797bcd98051502
|
7
|
+
data.tar.gz: 2c66e2e58fa7b494292763d3128fb3522444291ba204cecd8d2d81ce338cc135ffdc07f16e6e87c7a14c935607059ec5266bff04dca2acd025c31582a92ed27b
|
@@ -1,11 +1,11 @@
|
|
1
|
-
#encoding: utf-8
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'active_support/core_ext/hash/reverse_merge'
|
4
4
|
|
5
5
|
class Hash
|
6
6
|
# reversed merge!: defaults.merge( self! )
|
7
7
|
alias :default! :reverse_merge!
|
8
|
-
|
8
|
+
|
9
9
|
# Applies a block as a mapping on all keys, returning a new hash.
|
10
10
|
#
|
11
11
|
def with_keys
|
@@ -13,7 +13,7 @@ class Hash
|
|
13
13
|
hsh[ yield hash_key ] = self[ hash_key ]
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
# The difference from #with_keys is that modify_keys expects block that takes
|
18
18
|
# 2 arguments (key: value pair) and returns the new key.
|
19
19
|
#
|
@@ -22,19 +22,19 @@ class Hash
|
|
22
22
|
hsh[ yield( hash_pair ) ] = self[ hash_pair[0] ]
|
23
23
|
end
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
# Applies a block as a mapping on all values, returning a new hash.
|
27
27
|
#
|
28
28
|
def with_values
|
29
29
|
each_with_object self.class.new do |(k, v), hsh| hsh[ k ] = yield v end
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
# Like #do_with_values, but modifies the receiver.
|
33
33
|
#
|
34
34
|
def with_values!
|
35
35
|
each_with_object self do |(k, v), hsh| hsh[ k ] = yield v end
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
# The difference from #do_with_values is that modify_values expects block
|
39
39
|
# that takes 2 arguments (key: value pair) and returns the new value.
|
40
40
|
#
|
@@ -43,7 +43,7 @@ class Hash
|
|
43
43
|
ꜧ[ hash_pair[0] ] = yield( hash_pair )
|
44
44
|
end
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
# Like #modify_values, but modifies the receiver.
|
48
48
|
#
|
49
49
|
def modify_values!
|
@@ -51,7 +51,7 @@ class Hash
|
|
51
51
|
ꜧ[ hash_pair[0] ] = yield( hash_pair )
|
52
52
|
end
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
# Like #map that returns a hash.
|
56
56
|
#
|
57
57
|
def modify
|
@@ -61,16 +61,25 @@ class Hash
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
+
# Checking mainly against the collision with ActiveSupport's Hash#slice.
|
65
|
+
if Hash.instance_methods.include? :slice then
|
66
|
+
fail LoadError, "Collision: Method #slice already defined on Hash! (%s)" %
|
67
|
+
Hash.instance_method( :slice ).source_location
|
68
|
+
end
|
69
|
+
|
64
70
|
# A bit like Array#slice, but only takes 1 argument, which is either a Range,
|
65
71
|
# or an Array, and returns the selection of the hash for the keys that match
|
66
|
-
# the range or are present in the array.
|
72
|
+
# the range or are present in the array.
|
67
73
|
#
|
68
74
|
def slice matcher
|
69
|
-
case matcher
|
70
|
-
|
71
|
-
|
75
|
+
self.class[ case matcher
|
76
|
+
when Array then
|
77
|
+
select { |key, _| matcher.include? key }
|
78
|
+
else
|
79
|
+
select { |key, _| matcher === key }
|
80
|
+
end ]
|
72
81
|
end
|
73
|
-
|
82
|
+
|
74
83
|
# Makes hash keys accessible as methods. If the hash keys collide with
|
75
84
|
# its methods, ArgumentError is raised, unless :overwrite_methods
|
76
85
|
# option == true.
|
@@ -87,4 +96,27 @@ class Hash
|
|
87
96
|
end
|
88
97
|
return self
|
89
98
|
end
|
99
|
+
|
100
|
+
# Pretty-prints the hash consisting of names as keys, and numeric values.
|
101
|
+
# Takes 2 named arguments: +:gap+ and +:precision+.
|
102
|
+
#
|
103
|
+
def pretty_print_numeric_values gap: 0, precision: 2
|
104
|
+
key_strings = key.map &:to_s
|
105
|
+
value_strings = values.map do |n| "%.#{precision}e" % n rescue "%s" % s end
|
106
|
+
lmax, rmax = keys_strings.map( &:size ).max, values_strings.map( &:size ).max
|
107
|
+
lgap = gap / 2
|
108
|
+
rgap = gap - lgap
|
109
|
+
key_strings.zip( value_strings ).map do |kς, vς|
|
110
|
+
"%- #{lmax+lgap+1}s%#{rmax+rgap+1}.#{precision}e" % [ kς, vς ]
|
111
|
+
end.each { |line| puts line }
|
112
|
+
return nil
|
113
|
+
end
|
114
|
+
|
115
|
+
class << self
|
116
|
+
def method_added( sym )
|
117
|
+
if sym == :slice then
|
118
|
+
fail LoadError, "Attempt to overwrite YSupport's Hash##{sym} method!"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
90
122
|
end
|
data/lib/y_support/name_magic.rb
CHANGED
data/lib/y_support/version.rb
CHANGED
data/test/misc_test.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#! /usr/bin/ruby
|
2
|
-
#
|
2
|
+
# encoding: utf-8
|
3
3
|
|
4
4
|
require 'minitest/autorun'
|
5
5
|
|
@@ -167,6 +167,10 @@ describe Hash do
|
|
167
167
|
h.dot!( overwrite_methods: true ) # instead of #assert_nothing_raised
|
168
168
|
assert_equal( {aaa: 1}, {aaa: 1}.dot! )
|
169
169
|
end
|
170
|
+
|
171
|
+
it "should be safeguarded against redefining #slice" do
|
172
|
+
-> { class Hash; def slice; end end }.must_raise LoadError
|
173
|
+
end
|
170
174
|
end
|
171
175
|
|
172
176
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: y_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.33
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- boris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|