y_support 2.1.17 → 2.1.18
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 +2 -0
- data/lib/y_support/core_ext/object/misc.rb +31 -15
- data/lib/y_support/version.rb +1 -1
- data/test/misc_test.rb +15 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 120a7bb4137ae4605604f405ed60fe3cc3f927b4
|
4
|
+
data.tar.gz: e2ba99d2c42fc89f0750262f42d6d8b7572a6c47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8eabfab794856a0b786ba8861d97a1909618279a55861ea96f6bd40d18f6267d511945429fa63a52d078f5675e78f3ea00032ec1e22d439935a2322a84a38f60
|
7
|
+
data.tar.gz: 9434baf3adbd88fc9c0161bc193cbc14db37cebc2518c2a934149a4e9a7bb1c455c050f79024952e795092e6f7efe9736db39d83c82f86cd4138e8efa427d780
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
require File.dirname( __FILE__ ) + '/../module'
|
3
4
|
require File.dirname( __FILE__ ) + '/../class'
|
4
5
|
|
5
6
|
class Object
|
@@ -29,27 +30,35 @@ class Object
|
|
29
30
|
# returned.
|
30
31
|
#
|
31
32
|
def set_attr_with_readers! hash
|
32
|
-
hash.each_pair {
|
33
|
-
instance_variable_set "@#{
|
33
|
+
hash.each_pair { |ß, value|
|
34
|
+
instance_variable_set "@#{ß}", value
|
34
35
|
singleton_class.class_exec do
|
35
|
-
define_method
|
36
|
-
return instance_variable_get "@#{
|
36
|
+
define_method ß do |*args, &block|
|
37
|
+
return instance_variable_get "@#{ß}" if args.empty? && block.nil?
|
37
38
|
super *args, &block
|
38
39
|
end
|
39
40
|
end
|
40
41
|
}
|
41
42
|
end
|
42
43
|
|
43
|
-
# Constructs parametrized subclasses of the supplied
|
44
|
-
# available under specified getters. Expects a hash of
|
45
|
-
# and a hash of parameters with which to parametrize
|
46
|
-
# against collisions in the subclass
|
47
|
-
# these shadow or overwrite existing
|
44
|
+
# Constructs heir classes (parametrized subclasses) of the supplied modules
|
45
|
+
# (classes) and makes them available under specified getters. Expects a hash of
|
46
|
+
# pairs { symbol: class }, and a hash of parameters with which to parametrize
|
47
|
+
# the modules (classes). The methods guards against collisions in the subclass
|
48
|
+
# getter symbols, rasing +NameError+ should these shadow or overwrite existing
|
49
|
+
# methods.
|
48
50
|
#
|
49
51
|
def param_class( hash, with: {} )
|
50
|
-
hash.each { |ß,
|
51
|
-
|
52
|
-
|
52
|
+
hash.each { |ß, m|
|
53
|
+
case m
|
54
|
+
when Class then
|
55
|
+
parametrized_subclass = m.parametrize( with )
|
56
|
+
set_attr_with_readers( ß => parametrized_subclass )
|
57
|
+
when Module then
|
58
|
+
heir_class = m.heir_class( with )
|
59
|
+
set_attr_with_readers( ß => heir_class )
|
60
|
+
else fail TypeError, "#{m} must be a module or a class!"
|
61
|
+
end
|
53
62
|
}
|
54
63
|
return nil
|
55
64
|
end
|
@@ -59,9 +68,16 @@ class Object
|
|
59
68
|
# for full explanation of the shadowing / overwriting behavior.
|
60
69
|
#
|
61
70
|
def param_class!( hash, with: {} )
|
62
|
-
hash.each { |ß,
|
63
|
-
|
64
|
-
|
71
|
+
hash.each { |ß, m|
|
72
|
+
case m
|
73
|
+
when Class then
|
74
|
+
parametrized_subclass = m.parametrize( with )
|
75
|
+
set_attr_with_readers!( ß => parametrized_subclass )
|
76
|
+
when Module then
|
77
|
+
heir_class = m.heir_class( with )
|
78
|
+
set_attr_with_readers!( ß => heir_class )
|
79
|
+
else fail TypeError, "#{m} must be a module or a class!"
|
80
|
+
end
|
65
81
|
}
|
66
82
|
return nil
|
67
83
|
end
|
data/lib/y_support/version.rb
CHANGED
data/test/misc_test.rb
CHANGED
@@ -10,10 +10,24 @@ describe Object do
|
|
10
10
|
|
11
11
|
it "should have #param_class" do
|
12
12
|
o = Object.new
|
13
|
-
|
13
|
+
m = Module.new
|
14
|
+
o.param_class( { Array: Array, foo: Hash, bar: m }, with: { mother: o } )
|
15
|
+
assert o.Array < Array
|
16
|
+
o.Array.mother.must_equal( o )
|
17
|
+
o.foo.mother.must_equal( o )
|
18
|
+
o.bar.ancestors[1].must_equal( m )
|
19
|
+
o.bar.mother.must_equal( o )
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should have #param_class!" do
|
23
|
+
o = Object.new
|
24
|
+
m = Module.new
|
25
|
+
o.param_class!( { Array: Array, foo: Hash, bar: m }, with: { mother: o } )
|
14
26
|
assert o.Array < Array
|
15
27
|
o.Array.mother.must_equal( o )
|
16
28
|
o.foo.mother.must_equal( o )
|
29
|
+
o.bar.ancestors[1].must_equal( m )
|
30
|
+
o.bar.mother.must_equal( o )
|
17
31
|
end
|
18
32
|
end
|
19
33
|
|