y_support 2.0.31 → 2.0.33

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa1ba220ac52fd36bcd0fba5106b33eee1d470b0
4
- data.tar.gz: 93f146f185b63649778152e187fd5c7ff1fc67de
3
+ metadata.gz: 053b9bd4ab69ab18a27d4e0cdd2fd1b994a601f0
4
+ data.tar.gz: 63f43d3edd19d69ea8a7f7c060096c3b75071fb3
5
5
  SHA512:
6
- metadata.gz: 667df88a4ae9191048b275ccd9f69a2846f442c362afcc0c296a6208fd8c64400bcb33ec22d7778cf0e0ce972e176c88359268a23718bd07913650f06b164987
7
- data.tar.gz: ded0a7e8a14163e6971f90b1965a9c6aa4fdf862ca79267ec62401f2f0537f3dc2fc00ebf011a7017e91b14decdfc007a1d135a19c673abf4df4711a1aade66d
6
+ metadata.gz: 31264bdb2e2e5f61d1d3e7a5d94a1cfd1b0a789b5870b9cab68e4e6729eb07a1d877711e33acfc3adbccdf24f659b5b63fe4d1a9913fc49e35797bcd98051502
7
+ data.tar.gz: 2c66e2e58fa7b494292763d3128fb3522444291ba204cecd8d2d81ce338cc135ffdc07f16e6e87c7a14c935607059ec5266bff04dca2acd025c31582a92ed27b
@@ -11,6 +11,7 @@ class Class
11
11
  parameters.each_pair { |symbol, value|
12
12
  subclass.define_singleton_method symbol do value end
13
13
  }
14
+ subclass.define_singleton_method inspect do subclass.superclass.inspect + "<" end
14
15
  end
15
16
  end
16
17
  end
@@ -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
- when Array then select { |key, _| matcher.include? key }
71
- else self.class[ select { |key, _| matcher === key } ] end
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
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # encoding: utf-8
2
2
 
3
3
  require 'y_support'
4
4
  require 'y_support/core_ext/hash/misc'
@@ -1,3 +1,3 @@
1
1
  module YSupport
2
- VERSION = "2.0.31"
2
+ VERSION = "2.0.33"
3
3
  end
data/test/misc_test.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #! /usr/bin/ruby
2
- # -*- coding: utf-8 -*-
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.31
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-09 00:00:00.000000000 Z
11
+ date: 2013-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport