y_support 2.5.2 → 2.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/y_support/core_ext/object/misc.rb +53 -27
- data/lib/y_support/version.rb +1 -1
- metadata +1 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45dd9eb80e6817e7e37732c09782c38da5de8ab0
|
4
|
+
data.tar.gz: 66d6e1d35bc5d809506d39a79513498ffce83e68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9226a106d85dfbc113bad61619b2a8dddb9af15957e8e1283cbc74ecb54a5af12d5016c73e434f9b23d32868c2527751fb58f2217af31675fece6f0e0e3d0a48
|
7
|
+
data.tar.gz: dd21b15c30466bce72b0a7715194574862b4179fd080fa7085a9ded206ff37c3843d234bfd0dfea82f47bb240b5af8dd35240f98599135ac7bda937073e1f969
|
@@ -4,49 +4,52 @@ require File.dirname( __FILE__ ) + '/../module'
|
|
4
4
|
require File.dirname( __FILE__ ) + '/../class'
|
5
5
|
|
6
6
|
class Object
|
7
|
-
# Assigns
|
8
|
-
#
|
9
|
-
#
|
7
|
+
# Assigns atrributes to the receiver object and makes them
|
8
|
+
# accessible via reader methods (aka. getter or selector
|
9
|
+
# methods). Raises `NameError` in case of name collisions with
|
10
|
+
# existing methods of the receiver object.
|
10
11
|
#
|
11
12
|
def set_attr_with_readers hash
|
12
13
|
hash.each_pair { |ß, value|
|
13
|
-
fail NameError, "Method \##{ß} already defined
|
14
|
+
fail NameError, "Method \##{ß} already defined on " \
|
15
|
+
"#{self}! Use #set_attr_with_readers! to overload " \
|
16
|
+
"preexisting methods." if methods.include? ß
|
14
17
|
set_attr_with_readers! ß => value
|
15
18
|
}
|
16
19
|
end
|
20
|
+
alias set_attr_with_selectors set_attr_with_readers
|
17
21
|
|
18
|
-
# Like +#set_attr_with_readers+, but
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
# instance variable regardless of what the behavior of the shadowed method
|
28
|
-
# might have been. However, if the method message is sent with arguments
|
29
|
-
# or block, the original method is invoked (via +super+) and its result
|
30
|
-
# returned.
|
22
|
+
# Like +#set_attr_with_readers+, but overloads existing methods
|
23
|
+
# if their names collied with the attributes to be set.
|
24
|
+
# Overloading is performed in the following way: If the attribute
|
25
|
+
# reader is called without parameters, it acts as an attribute
|
26
|
+
# reader (as expected). If the user tries to invoke the attribute
|
27
|
+
# method with parameters (or a block), the message falls through
|
28
|
+
# to the colliding instance method. In this way, both methods
|
29
|
+
# are preserved -- collision only happens if the colliding
|
30
|
+
# method took no parameters.
|
31
31
|
#
|
32
32
|
def set_attr_with_readers! hash
|
33
33
|
hash.each_pair { |ß, value|
|
34
|
+
# puts "Setting @#{ß} of #{self} to #{value}."
|
34
35
|
instance_variable_set "@#{ß}", value
|
35
36
|
singleton_class.class_exec do
|
36
37
|
define_method ß do |*args, &block|
|
37
|
-
return instance_variable_get "@#{ß}" if
|
38
|
+
return instance_variable_get "@#{ß}" if
|
39
|
+
args.empty? && block.nil?
|
38
40
|
super *args, &block
|
39
41
|
end
|
40
42
|
end
|
41
43
|
}
|
42
44
|
end
|
43
45
|
|
44
|
-
# Constructs heir classes (parametrized subclasses) of the
|
45
|
-
# (classes) and makes them available under
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
46
|
+
# Constructs heir classes (parametrized subclasses) of the
|
47
|
+
# supplied modules (classes) and makes them available under
|
48
|
+
# specified getters. Expects a hash of pairs { symbol: class },
|
49
|
+
# and a hash of parameters with which to parametrize the modules
|
50
|
+
# (classes). The methods guards against collisions in the
|
51
|
+
# subclass getter symbols, rasing +NameError+ should these shadow
|
52
|
+
# or overwrite existing methods.
|
50
53
|
#
|
51
54
|
def param_class( hash, with: {} )
|
52
55
|
hash.each { |ß, m|
|
@@ -63,9 +66,10 @@ class Object
|
|
63
66
|
return nil
|
64
67
|
end
|
65
68
|
|
66
|
-
# Like +#param_class+, but it shadows or overwrites existing
|
67
|
-
# with the getters of the parametrized
|
68
|
-
# for full explanation of
|
69
|
+
# Like +#param_class+, but it shadows or overwrites existing
|
70
|
+
# methods colliding with the getters of the parametrized
|
71
|
+
# classes. See +#set_attr_with_readers!" for full explanation of
|
72
|
+
# the shadowing / overwriting behavior.
|
69
73
|
#
|
70
74
|
def param_class!( hash, with: {} )
|
71
75
|
hash.each { |ß, m|
|
@@ -81,4 +85,26 @@ class Object
|
|
81
85
|
}
|
82
86
|
return nil
|
83
87
|
end
|
88
|
+
|
89
|
+
# New syntax for #param_class method for creating parametrized
|
90
|
+
# subclasses.
|
91
|
+
#
|
92
|
+
def owns_subclass( name, of:, parametrized_by: {},
|
93
|
+
named: proc do |**params|
|
94
|
+
param_str = params.empty? ? "" :
|
95
|
+
"[#{params.pretty_print}]"
|
96
|
+
"#{name}#{param_str}>"
|
97
|
+
end,
|
98
|
+
overwrite_existing_methods: true )
|
99
|
+
if overwrite_existing_methods then
|
100
|
+
param_class!( { name => of }, with: parametrized_by )
|
101
|
+
else
|
102
|
+
param_class( { name => of }, with: parametrized_by )
|
103
|
+
end
|
104
|
+
# FIXME: This method cannot stay like this. It is not possible
|
105
|
+
# for has_subclass method expecting a single class to subclass
|
106
|
+
# to rely on param_class method which is intended for multiple
|
107
|
+
# classes. Simplify simplify simplify. And change that SO
|
108
|
+
# answer when done.
|
109
|
+
end
|
84
110
|
end
|
data/lib/y_support/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: y_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.5.
|
4
|
+
version: 2.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- boris
|
@@ -163,4 +163,3 @@ test_files:
|
|
163
163
|
- test/name_magic_test.rb
|
164
164
|
- test/typing_test.rb
|
165
165
|
- test/unicode_test.rb
|
166
|
-
has_rdoc:
|