y_support 2.1.5 → 2.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +675 -0
- data/Rakefile +1 -1
- data/lib/y_support/abstract_algebra.rb +1 -0
- data/lib/y_support/all.rb +1 -0
- data/lib/y_support/core_ext/array.rb +2 -2
- data/lib/y_support/core_ext/class/misc.rb +2 -3
- data/lib/y_support/core_ext/class.rb +2 -2
- data/lib/y_support/core_ext/enumerable/misc.rb +1 -1
- data/lib/y_support/core_ext/enumerable.rb +2 -2
- data/lib/y_support/core_ext/hash/misc.rb +26 -39
- data/lib/y_support/core_ext/hash.rb +2 -2
- data/lib/y_support/core_ext/module/misc.rb +39 -1
- data/lib/y_support/core_ext/module.rb +2 -2
- data/lib/y_support/core_ext/numeric/misc.rb +0 -2
- data/lib/y_support/core_ext/numeric.rb +2 -2
- data/lib/y_support/core_ext/object/inspection.rb +0 -2
- data/lib/y_support/core_ext/object/misc.rb +31 -16
- data/lib/y_support/core_ext/object.rb +3 -3
- data/lib/y_support/core_ext/string/misc.rb +7 -7
- data/lib/y_support/core_ext/string.rb +2 -2
- data/lib/y_support/core_ext/symbol/misc.rb +2 -3
- data/lib/y_support/core_ext/symbol.rb +2 -2
- data/lib/y_support/core_ext.rb +0 -2
- data/lib/y_support/inert_recorder.rb +1 -1
- data/lib/y_support/kde.rb +1 -0
- data/lib/y_support/name_magic/array.rb +27 -7
- data/lib/y_support/name_magic/class_methods.rb +52 -58
- data/lib/y_support/name_magic/hash.rb +18 -4
- data/lib/y_support/name_magic/namespace_methods.rb +241 -174
- data/lib/y_support/name_magic.rb +82 -38
- data/lib/y_support/null_object.rb +8 -7
- data/lib/y_support/respond_to.rb +1 -2
- data/lib/y_support/stdlib_ext/matrix/misc.rb +0 -2
- data/lib/y_support/stdlib_ext/matrix.rb +2 -2
- data/lib/y_support/stdlib_ext.rb +1 -0
- data/lib/y_support/try.rb +1 -1
- data/lib/y_support/typing/array/typing.rb +45 -4
- data/lib/y_support/typing/array.rb +1 -1
- data/lib/y_support/typing/enumerable/typing.rb +26 -28
- data/lib/y_support/typing/enumerable.rb +1 -1
- data/lib/y_support/typing/hash/typing.rb +47 -29
- data/lib/y_support/typing/hash.rb +1 -1
- data/lib/y_support/typing/module/typing.rb +4 -6
- data/lib/y_support/typing/module.rb +1 -1
- data/lib/y_support/typing/object/typing.rb +64 -61
- data/lib/y_support/typing/object.rb +1 -1
- data/lib/y_support/typing.rb +4 -26
- data/lib/y_support/unicode.rb +5 -7
- data/lib/y_support/version.rb +1 -1
- data/lib/y_support/x.rb +11 -8
- data/lib/y_support.rb +2 -2
- data/test/abstract_algebra_test.rb +44 -55
- data/test/inert_recorder_test.rb +13 -18
- data/test/local_object_test.rb +13 -18
- data/test/misc_test.rb +15 -10
- data/test/name_magic_test.rb +1 -1
- data/test/null_object_test.rb +19 -26
- data/test/respond_to_test.rb +15 -16
- data/test/typing_test.rb +100 -103
- data/test/x_test.rb +16 -19
- data/y_support.gemspec +21 -17
- metadata +37 -8
- data/LICENSE +0 -22
@@ -1,11 +1,14 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
# Class methods for the classes that include NameMagic.
|
4
|
+
#
|
3
5
|
module NameMagic::ClassMethods
|
4
6
|
# Presents the instances registered by the namespace. Takes one optional
|
5
7
|
# argument. If set to _false_, the method returns all the instances
|
6
8
|
# registered by the namespace. If set to _true_ (default), only returns
|
7
9
|
# those instances registered by the namespace, which are of exactly the
|
8
|
-
# same class as the receiver.
|
10
|
+
# same class as the receiver (ie. excluding the instances of the subclasses
|
11
|
+
# of this class). Example:
|
9
12
|
#
|
10
13
|
# <code>
|
11
14
|
# class Animal; include NameMagic end
|
@@ -23,10 +26,12 @@ module NameMagic::ClassMethods
|
|
23
26
|
option ? ii.select { |i| i.kind_of? self } : ii
|
24
27
|
end
|
25
28
|
|
26
|
-
#
|
27
|
-
#
|
29
|
+
# Deprecated method to get full names of the named instances. Use
|
30
|
+
# <code>instances.names</code> instead. Takes one optional argument,
|
31
|
+
# same as +#instances+ method.
|
28
32
|
#
|
29
33
|
def instance_names option=true
|
34
|
+
warn "Method #instance_names is deprecated. Use 'instances._names_' or 'instances.names' instead!"
|
30
35
|
instances( option ).names( false )
|
31
36
|
end
|
32
37
|
|
@@ -36,8 +41,7 @@ module NameMagic::ClassMethods
|
|
36
41
|
# +#const_magic+.)
|
37
42
|
#
|
38
43
|
def __instances__
|
39
|
-
|
40
|
-
namespace.__instances__
|
44
|
+
namespace == self ? super : namespace.__instances__
|
41
45
|
end
|
42
46
|
|
43
47
|
# Presents namespace-owned +@avid_instances+ (array of avid instances). "Avid"
|
@@ -45,8 +49,7 @@ module NameMagic::ClassMethods
|
|
45
49
|
# registered instance. (This method does not trigger +#const_magic+.)
|
46
50
|
#
|
47
51
|
def __avid_instances__
|
48
|
-
|
49
|
-
namespace.__avid_instances__
|
52
|
+
namespace == self ? super : namespace.__avid_instances__
|
50
53
|
end
|
51
54
|
|
52
55
|
# Returns the instance identified by the first argument, which can typically
|
@@ -54,21 +57,21 @@ module NameMagic::ClassMethods
|
|
54
57
|
# returned without change. The second argument is optional, with the same
|
55
58
|
# meaning as in +NameMagic::ClassMethods#instances+ method.
|
56
59
|
#
|
57
|
-
def instance
|
60
|
+
def instance instance, option=true
|
58
61
|
return super if namespace == self
|
59
|
-
namespace.instance(
|
60
|
-
|
61
|
-
|
62
|
+
return namespace.instance( instance ) unless option
|
63
|
+
namespace.instance( instance ).tap { |instance|
|
64
|
+
fail NameError, "No #{self} instance #{instance} registered in " +
|
65
|
+
"#{namespace}!" unless instance.kind_of? self
|
62
66
|
}
|
63
67
|
end
|
64
68
|
|
65
|
-
# Searches all the modules in the the object space for constants
|
66
|
-
#
|
67
|
-
#
|
69
|
+
# Searches all the modules in the the object space for constants pointing to
|
70
|
+
# anonymous instances of this class, and names them accordingly. The number
|
71
|
+
# of remaining anonymous instances is returned.
|
68
72
|
#
|
69
73
|
def const_magic
|
70
|
-
|
71
|
-
namespace.const_magic
|
74
|
+
namespace == self ? super : namespace.const_magic
|
72
75
|
end
|
73
76
|
|
74
77
|
# Returns the nameless instances. The optional argument has the same meaning
|
@@ -76,23 +79,23 @@ module NameMagic::ClassMethods
|
|
76
79
|
#
|
77
80
|
def nameless_instances option=true
|
78
81
|
return super if namespace == self
|
79
|
-
|
80
|
-
|
82
|
+
return namespace.nameless_instances unless option
|
83
|
+
namespace.nameless_instances.select { |i| i.kind_of? self }
|
81
84
|
end
|
82
85
|
|
83
86
|
# Clears namespace-owned references to a specified instance. (This is
|
84
|
-
# different from
|
87
|
+
# different from de-naming an instance by setting <code>inst.name =
|
85
88
|
# nil</code>, which makes the instance anonymous, but still registered.)
|
86
89
|
#
|
87
|
-
def forget
|
88
|
-
|
89
|
-
namespace.forget instance( instance_identifier )
|
90
|
-
end
|
90
|
+
def forget instance, option=true
|
91
|
+
namespace == self || ! option ? super : namespace.forget( instance instance )
|
91
92
|
end
|
92
93
|
|
93
94
|
# Clears namespace-owned references to an instance, without performing
|
94
95
|
# #const_magic first. The argument should be a registered instance. Returns
|
95
|
-
# the instance name
|
96
|
+
# the instance's name for named instances, _nil_ for anonymous instances,
|
97
|
+
# and _false_ if there was no such registered instance. The optional second
|
98
|
+
# argument has the same meaning as in +NameMaic::ClassMethods#instances+.
|
96
99
|
#
|
97
100
|
def __forget__( instance, option=true )
|
98
101
|
return super if namespace == self
|
@@ -104,15 +107,13 @@ module NameMagic::ClassMethods
|
|
104
107
|
# Clears namespace-owned references to all the anonymous instances.
|
105
108
|
#
|
106
109
|
def forget_nameless_instances
|
107
|
-
|
108
|
-
namespace.forget_nameless_instances
|
110
|
+
namespace == self ? super : namespace.forget_nameless_instances
|
109
111
|
end
|
110
112
|
|
111
113
|
# Clears namespace-owned references to all the instances.
|
112
114
|
#
|
113
115
|
def forget_all_instances
|
114
|
-
|
115
|
-
namespace.forget_all_instances
|
116
|
+
namespace == self ? super : namespace.forget_all_instances
|
116
117
|
end
|
117
118
|
|
118
119
|
# Registers a hook to execute upon instantiation. Expects a unary block, whose
|
@@ -120,8 +121,7 @@ module NameMagic::ClassMethods
|
|
120
121
|
# but before naming the instance.
|
121
122
|
#
|
122
123
|
def new_instance_hook &block
|
123
|
-
|
124
|
-
namespace.new_instance_hook &block
|
124
|
+
namespace == self ? super : namespace.new_instance_hook( &block )
|
125
125
|
end
|
126
126
|
|
127
127
|
# Registers a hook to execute upon instance naming. Expects a ternary block,
|
@@ -134,8 +134,7 @@ module NameMagic::ClassMethods
|
|
134
134
|
# constant name.
|
135
135
|
#
|
136
136
|
def name_set_hook &block
|
137
|
-
|
138
|
-
namespace.name_set_hook &block
|
137
|
+
namespace == self ? super : namespace.name_set_hook( &block )
|
139
138
|
end
|
140
139
|
|
141
140
|
# Registers a hook to execute whenever the instance is asked its name. The
|
@@ -146,11 +145,10 @@ module NameMagic::ClassMethods
|
|
146
145
|
# returned.
|
147
146
|
#
|
148
147
|
def name_get_hook &block
|
149
|
-
|
150
|
-
namespace.name_get_hook &block
|
148
|
+
namespace == self ? super : namespace.name_get_hook( &block )
|
151
149
|
end
|
152
150
|
|
153
|
-
# Sets the namespace
|
151
|
+
# Sets the namespace for the class.
|
154
152
|
#
|
155
153
|
def namespace= modul
|
156
154
|
puts "Assigning #{modul} as the namespace of #{self}." if ::NameMagic::DEBUG
|
@@ -164,17 +162,16 @@ module NameMagic::ClassMethods
|
|
164
162
|
nil.tap { self.namespace = self }
|
165
163
|
end
|
166
164
|
|
167
|
-
# In addition the ability to name objects
|
168
|
-
#
|
169
|
-
#
|
170
|
-
#
|
171
|
-
#
|
172
|
-
# another object) if set to _true_.
|
165
|
+
# In addition the ability to name objects by constant assignment, +NameMagic+
|
166
|
+
# redefines #new method so as to swallow name argument +:name+ (alias :ɴ), and
|
167
|
+
# naming the constructed instance by it. Also, +:name_avid+ option may be
|
168
|
+
# supplied, which, if _true_, makes the instance capable of avid naming:
|
169
|
+
# Overwriting (stealing) a name already given to another instance.
|
173
170
|
#
|
174
171
|
def new *args, &block
|
175
172
|
oo = if args[-1].is_a? Hash then args.pop else {} end # extract hash
|
176
173
|
nm = oo.delete( :name ) || oo.delete( :ɴ ) # consume :name / :ɴ if given
|
177
|
-
avid =
|
174
|
+
avid = oo.delete( :name_avid )
|
178
175
|
# Avoid overwriting existing names unless avid:
|
179
176
|
fail NameError, "#{self} instance #{nm} already exists!" if
|
180
177
|
__instances__.keys.include? nm unless avid
|
@@ -185,19 +182,24 @@ module NameMagic::ClassMethods
|
|
185
182
|
λ.( inst ) if λ
|
186
183
|
if nm then # Name supplied, name the instance.
|
187
184
|
avid ? inst.name!( nm ) : inst.name = nm
|
188
|
-
else # Name not
|
189
|
-
__avid_instances__ << inst
|
185
|
+
else # Name not given, make the inst. avid unless expressly prohibited.
|
186
|
+
__avid_instances__ << inst unless avid == false
|
190
187
|
end
|
191
188
|
}
|
192
189
|
end
|
193
190
|
end
|
194
191
|
|
195
|
-
# Calls #new in _avid_ _mode_ (name_avid: true); see #new method for
|
196
|
-
# explanation.
|
192
|
+
# Calls #new in _avid_ _mode_ (<tt>name_avid: true</tt>); see #new method for
|
193
|
+
# avid mode explanation.
|
194
|
+
#
|
195
|
+
def avid *ordered_args, **named_args, &block
|
196
|
+
new *ordered_args, **named_args.update( name_avid: true ), &block
|
197
|
+
end
|
198
|
+
|
199
|
+
# Performs general name validation.
|
197
200
|
#
|
198
|
-
def
|
199
|
-
|
200
|
-
new *args, oo.update( name_avid: true ), &block
|
201
|
+
def validate_name name
|
202
|
+
namespace == self ? super : namespace.validate_name( name )
|
201
203
|
end
|
202
204
|
|
203
205
|
private
|
@@ -205,14 +207,6 @@ module NameMagic::ClassMethods
|
|
205
207
|
# Checks all the constants in some module's namespace, recursively.
|
206
208
|
#
|
207
209
|
def serve_all_modules
|
208
|
-
|
209
|
-
namespace.serve_all_modules
|
210
|
-
end
|
211
|
-
|
212
|
-
# Performs general name validation.
|
213
|
-
#
|
214
|
-
def validate_name name
|
215
|
-
return super if namespace == self
|
216
|
-
namespace.validate_name name
|
210
|
+
namespace == self ? super : namespace.serve_all_modules
|
217
211
|
end
|
218
212
|
end # module NameMagic::ClassMethods
|
@@ -1,17 +1,31 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
class Hash
|
4
|
+
# Maps the hash into one whose keys have been replaced with full names of
|
5
|
+
# the keys (which are assumed to be objects responding to +#full_name+ method).
|
6
|
+
#
|
7
|
+
def keys_to_names # FIXME: Change to keys_to_full_names
|
8
|
+
with_keys do |key| key.name || key end # FIXME: Change name to full_name
|
9
|
+
end
|
10
|
+
|
11
|
+
# Modifies a hash in place so that the keys are replaced with key names (key
|
12
|
+
# objects are assumed to respond to +#name+ method).
|
13
|
+
#
|
14
|
+
def keys_to_names! # FIXME: Change to keys_to_full_names!
|
15
|
+
with_keys! do |key| key.name || key end # FIXME: Change name to full_name
|
16
|
+
end
|
17
|
+
|
4
18
|
# Maps a hash into a hash, whose keys have been replaced with names of the
|
5
19
|
# key objects (which are assumed to respond to +#name+ method).
|
6
20
|
#
|
7
|
-
def
|
8
|
-
with_keys do |key| key.
|
21
|
+
def keys_to_ɴs
|
22
|
+
with_keys do |key| key._name_ || key end
|
9
23
|
end
|
10
24
|
|
11
25
|
# Modifies a hash in place so that the keys are replaced with key names (key
|
12
26
|
# objects are assumed to respond to +#name+ method).
|
13
27
|
#
|
14
|
-
def
|
15
|
-
with_keys! do |key| key.
|
28
|
+
def keys_to_ɴs!
|
29
|
+
with_keys! do |key| key._name_ || key end
|
16
30
|
end
|
17
31
|
end
|