y_support 2.0.33 → 2.0.34

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: 053b9bd4ab69ab18a27d4e0cdd2fd1b994a601f0
4
- data.tar.gz: 63f43d3edd19d69ea8a7f7c060096c3b75071fb3
3
+ metadata.gz: b8837638588e1c76d7fea644f117253c4250cb13
4
+ data.tar.gz: 3ede5fb44abe873b9fc068ecb6b3f059a9264e98
5
5
  SHA512:
6
- metadata.gz: 31264bdb2e2e5f61d1d3e7a5d94a1cfd1b0a789b5870b9cab68e4e6729eb07a1d877711e33acfc3adbccdf24f659b5b63fe4d1a9913fc49e35797bcd98051502
7
- data.tar.gz: 2c66e2e58fa7b494292763d3128fb3522444291ba204cecd8d2d81ce338cc135ffdc07f16e6e87c7a14c935607059ec5266bff04dca2acd025c31582a92ed27b
6
+ metadata.gz: 16aec56e99572375aaa49450202c4700247b20ba5b6457935fc2aeb6a100e99699436cafb88618d53c6c6895f860e973b5899b2d13fe926d300fb26a054d0fd6
7
+ data.tar.gz: b10e770494ad0f115b9b2f9f75cdcbb6123a51d6a4ba98dddf2f796062408359ffaa69a4e262a909d7436a2a02cd1cff45d26bc15a427df6f1fae9331c4035a9
@@ -3,6 +3,49 @@
3
3
  require 'active_support/core_ext/hash/reverse_merge'
4
4
 
5
5
  class Hash
6
+ class << self
7
+ # This kluge method guards against overwriting of the #slice method by
8
+ # ActiveSupport.
9
+ #
10
+ def method_added( sym )
11
+ if sym == :slice then
12
+ puts "Method added activated on :slice"
13
+ # Unless it is our method, overwrite it.
14
+ unless instance_method( sym ).source_location.include? 'y_support'
15
+ puts "Self is #{self}"
16
+ puts "Self class is #{self.class}"
17
+ # Let's now make a cache of this very method being called
18
+ ma = singleton_class.instance_method :method_added
19
+ # Let's remove the :method_added hook, or otherwise infinite recursion
20
+ # would ensue.
21
+ singleton_class.class_exec { remove_method :method_added }
22
+ # And let's redefine the +:slice+ method now:
23
+ warn "Attempt to overwrite Hash#slice method has occured, reverting."
24
+
25
+ class_exec do
26
+ # A bit like Array#slice, but only takes 1 argument, which is either
27
+ # a Range, or an Array, and returns the selection of the hash for
28
+ # the keys that match the range or are present in the array.
29
+ #
30
+ define_method sym do |matcher|
31
+ self.class[ case matcher
32
+ when Array then
33
+ select { |key, _| matcher.include? key }
34
+ else
35
+ select { |key, _| matcher === key }
36
+ end ]
37
+ end
38
+ end
39
+
40
+ # Finally, let's bind the +:method_added+ method to self again.
41
+ singleton_class.class_exec do
42
+ define_method :method_added do |sym| ma.bind( self ).call( sym ) end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
6
49
  # reversed merge!: defaults.merge( self! )
7
50
  alias :default! :reverse_merge!
8
51
 
@@ -63,7 +106,7 @@ class Hash
63
106
 
64
107
  # Checking mainly against the collision with ActiveSupport's Hash#slice.
65
108
  if Hash.instance_methods.include? :slice then
66
- fail LoadError, "Collision: Method #slice already defined on Hash! (%s)" %
109
+ warn "Collision: Method #slice already defined on Hash! (%s)" %
67
110
  Hash.instance_method( :slice ).source_location
68
111
  end
69
112
 
@@ -111,12 +154,4 @@ class Hash
111
154
  end.each { |line| puts line }
112
155
  return nil
113
156
  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
122
157
  end
@@ -1,3 +1,3 @@
1
1
  module YSupport
2
- VERSION = "2.0.33"
2
+ VERSION = "2.0.34"
3
3
  end
data/test/misc_test.rb CHANGED
@@ -169,7 +169,9 @@ describe Hash do
169
169
  end
170
170
 
171
171
  it "should be safeguarded against redefining #slice" do
172
- -> { class Hash; def slice; end end }.must_raise LoadError
172
+ m = Hash.instance_method :slice
173
+ class Hash; def slice( *args ); fail "This should not happen!" end end
174
+ {}.slice( :a )
173
175
  end
174
176
  end
175
177
 
data/test/unicode_test.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #! /usr/bin/ruby
2
- #encoding: utf-8
2
+ # encoding: utf-8
3
3
 
4
4
  require 'minitest/autorun'
5
5
 
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.33
4
+ version: 2.0.34
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-16 00:00:00.000000000 Z
11
+ date: 2013-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport