y_support 2.1.5 → 2.1.12

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.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +675 -0
  3. data/Rakefile +1 -1
  4. data/lib/y_support/abstract_algebra.rb +1 -0
  5. data/lib/y_support/all.rb +1 -0
  6. data/lib/y_support/core_ext/array.rb +2 -2
  7. data/lib/y_support/core_ext/class/misc.rb +2 -3
  8. data/lib/y_support/core_ext/class.rb +2 -2
  9. data/lib/y_support/core_ext/enumerable/misc.rb +1 -1
  10. data/lib/y_support/core_ext/enumerable.rb +2 -2
  11. data/lib/y_support/core_ext/hash/misc.rb +26 -39
  12. data/lib/y_support/core_ext/hash.rb +2 -2
  13. data/lib/y_support/core_ext/module/misc.rb +39 -1
  14. data/lib/y_support/core_ext/module.rb +2 -2
  15. data/lib/y_support/core_ext/numeric/misc.rb +0 -2
  16. data/lib/y_support/core_ext/numeric.rb +2 -2
  17. data/lib/y_support/core_ext/object/inspection.rb +0 -2
  18. data/lib/y_support/core_ext/object/misc.rb +31 -16
  19. data/lib/y_support/core_ext/object.rb +3 -3
  20. data/lib/y_support/core_ext/string/misc.rb +7 -7
  21. data/lib/y_support/core_ext/string.rb +2 -2
  22. data/lib/y_support/core_ext/symbol/misc.rb +2 -3
  23. data/lib/y_support/core_ext/symbol.rb +2 -2
  24. data/lib/y_support/core_ext.rb +0 -2
  25. data/lib/y_support/inert_recorder.rb +1 -1
  26. data/lib/y_support/kde.rb +1 -0
  27. data/lib/y_support/name_magic/array.rb +27 -7
  28. data/lib/y_support/name_magic/class_methods.rb +52 -58
  29. data/lib/y_support/name_magic/hash.rb +18 -4
  30. data/lib/y_support/name_magic/namespace_methods.rb +241 -174
  31. data/lib/y_support/name_magic.rb +82 -38
  32. data/lib/y_support/null_object.rb +8 -7
  33. data/lib/y_support/respond_to.rb +1 -2
  34. data/lib/y_support/stdlib_ext/matrix/misc.rb +0 -2
  35. data/lib/y_support/stdlib_ext/matrix.rb +2 -2
  36. data/lib/y_support/stdlib_ext.rb +1 -0
  37. data/lib/y_support/try.rb +1 -1
  38. data/lib/y_support/typing/array/typing.rb +45 -4
  39. data/lib/y_support/typing/array.rb +1 -1
  40. data/lib/y_support/typing/enumerable/typing.rb +26 -28
  41. data/lib/y_support/typing/enumerable.rb +1 -1
  42. data/lib/y_support/typing/hash/typing.rb +47 -29
  43. data/lib/y_support/typing/hash.rb +1 -1
  44. data/lib/y_support/typing/module/typing.rb +4 -6
  45. data/lib/y_support/typing/module.rb +1 -1
  46. data/lib/y_support/typing/object/typing.rb +64 -61
  47. data/lib/y_support/typing/object.rb +1 -1
  48. data/lib/y_support/typing.rb +4 -26
  49. data/lib/y_support/unicode.rb +5 -7
  50. data/lib/y_support/version.rb +1 -1
  51. data/lib/y_support/x.rb +11 -8
  52. data/lib/y_support.rb +2 -2
  53. data/test/abstract_algebra_test.rb +44 -55
  54. data/test/inert_recorder_test.rb +13 -18
  55. data/test/local_object_test.rb +13 -18
  56. data/test/misc_test.rb +15 -10
  57. data/test/name_magic_test.rb +1 -1
  58. data/test/null_object_test.rb +19 -26
  59. data/test/respond_to_test.rb +15 -16
  60. data/test/typing_test.rb +100 -103
  61. data/test/x_test.rb +16 -19
  62. data/y_support.gemspec +21 -17
  63. metadata +37 -8
  64. 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. Example:
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
- # Presents the instance names. Takes one optional argument, same as
27
- # +#instances+ method.
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
- return super if namespace == self
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
- return super if namespace == self
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 id, option=true
60
+ def instance instance, option=true
58
61
  return super if namespace == self
59
- namespace.instance( id ).tap { |inst|
60
- fail NameError, "No #{self} instance #{id} registered in " +
61
- "#{namespace}!" unless inst.kind_of? self if option
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 containing
66
- # receiver class objects, and names the found instances accordingly. The
67
- # number of the remaining nameless instances is returned.
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
- return super if namespace == self
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
- ii = namespace.nameless_instances
80
- option ? ii.select { |i| i.kind_of? self } : ii
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 "unnaming" an instance by setting <code>inst.name =
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 instance_identifier, option=true
88
- if namespace == self || ! option then super else
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, or _false_, if there was no such registered instance.
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
- return super if namespace == self
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
- return super if namespace == self
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
- return super if namespace == self
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
- return super if namespace == self
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
- return super if namespace == self
150
- namespace.name_get_hook &block
148
+ namespace == self ? super : namespace.name_get_hook( &block )
151
149
  end
152
150
 
153
- # Sets the namespace of the class.
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 upon constant assignment, as common
168
- # with eg. Class instances, NameMagic redefines class method #new so that it
169
- # swallows the named argument :name (alias :ɴ), and takes care of naming the
170
- # instance accordingly. Also, :name_avid named argument mey be supplied, which
171
- # makes the naming avid (able to overwrite the name already in use by
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 = !!oo.delete( :name_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 supplied, make the instance avid.
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 avid mode
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 avid *args, &block
199
- oo = args[-1].is_a?( Hash ) ? args.pop : {} # extract options
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
- return super if namespace == self
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 keys_to_names
8
- with_keys do |key| key.name || key end
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 keys_to_names!
15
- with_keys! do |key| key.name || key end
28
+ def keys_to_ɴs!
29
+ with_keys! do |key| key._name_ || key end
16
30
  end
17
31
  end