y_support 2.0.7 → 2.0.8
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/name_magic.rb +31 -30
- data/lib/y_support/version.rb +1 -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: d492494b1cf15742c2b407f187e650850633102c
|
4
|
+
data.tar.gz: e6c2136adbdd92e76f0b7231e73745536d903690
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b48bb642dfd998952c56e5156ee432de0326847b35073a13f5fddde7666ba8d50932a22d10183b4c9a39b46648a43d73357133a3464763733a0f20c3efa481d1
|
7
|
+
data.tar.gz: 3564cbb4e43fd0a91d3aebf95336f9ff4b21392d341c99aa92d192406e443cbb597e1757eee97f69abdd1ca3f76a2cf46407d963c62fa0bcea293417d9e08c38
|
data/lib/y_support/name_magic.rb
CHANGED
@@ -33,42 +33,29 @@ require 'y_support'
|
|
33
33
|
module NameMagic
|
34
34
|
DEBUG = false
|
35
35
|
|
36
|
-
def self.included
|
37
|
-
case
|
36
|
+
def self.included ɱ
|
37
|
+
case ɱ
|
38
38
|
when Class then # we will decorate its #new method
|
39
|
-
class <<
|
39
|
+
class << ɱ
|
40
40
|
alias :original_method_new :new # Make space to decorate #new
|
41
41
|
end
|
42
42
|
# Attach the decorators etc.
|
43
|
-
|
44
|
-
|
43
|
+
ɱ.extend ::NameMagic::ClassMethods
|
44
|
+
ɱ.extend ::NameMagic::NamespaceMethods
|
45
45
|
# Attach namespace methods also to the namespace, if given.
|
46
46
|
begin
|
47
|
-
if
|
48
|
-
|
47
|
+
if ɱ.namespace == ɱ then
|
48
|
+
ɱ.define_singleton_method :namespace do ɱ end
|
49
49
|
else
|
50
|
-
|
50
|
+
ɱ.namespace.extend ::NameMagic::NamespaceMethods
|
51
51
|
end
|
52
52
|
rescue NoMethodError
|
53
53
|
end
|
54
|
-
else # it is a Module; we'll infect it with
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
rescue NameError
|
60
|
-
end
|
61
|
-
if target_pre_included then # target has #pre_included hook
|
62
|
-
target.define_singleton_method :included do |ç|
|
63
|
-
target_pre_included.( ç )
|
64
|
-
this_included.( ç )
|
65
|
-
target_included.( ç )
|
66
|
-
end
|
67
|
-
else # target has no #pre_included hook
|
68
|
-
target.define_singleton_method :included do |ç|
|
69
|
-
this_included.( ç )
|
70
|
-
target_included.( ç )
|
71
|
-
end
|
54
|
+
else # it is a Module; we'll infect it with our #included method
|
55
|
+
ɱ_included, this_included = ɱ.method( :included ), method( :included )
|
56
|
+
ɱ.define_singleton_method :included do |ç|
|
57
|
+
this_included.( ç )
|
58
|
+
ɱ_included.( ç )
|
72
59
|
end
|
73
60
|
end
|
74
61
|
end # self.included
|
@@ -95,12 +82,14 @@ module NameMagic
|
|
95
82
|
# Names an instance, cautiously (ie. no overwriting of existing names).
|
96
83
|
#
|
97
84
|
def name=( ɴ )
|
85
|
+
puts "NameMagic: Naming with argument #{ɴ}." if DEBUG
|
98
86
|
# get previous name of this instance, if any
|
99
87
|
old_ɴ = self.class.__instances__[ self ]
|
100
88
|
# honor the hook
|
101
89
|
name_set_closure = self.class.instance_variable_get :@name_set_closure
|
102
90
|
ɴ = name_set_closure.call( ɴ, self, old_ɴ ) if name_set_closure
|
103
91
|
ɴ = self.class.send( :validate_capitalization, ɴ ).to_sym
|
92
|
+
puts "NameMagic: Name adjusted to #{ɴ}." if DEBUG
|
104
93
|
return if old_ɴ == ɴ # already named as required; nothing to do
|
105
94
|
# otherwise, be cautious about name collision
|
106
95
|
raise NameError, "Name '#{ɴ}' already exists in " +
|
@@ -114,11 +103,13 @@ module NameMagic
|
|
114
103
|
# Names an instance, aggresively (overwrites existing names).
|
115
104
|
#
|
116
105
|
def name!( ɴ )
|
106
|
+
puts "NameMagic: Rudely naming with argument #{ɴ}." if DEBUG
|
117
107
|
old_ɴ = self.class.__instances__[ self ] # get instance's old name, if any
|
118
108
|
# honor the hook
|
119
109
|
name_set_closure = self.class.instance_variable_get :@name_set_closure
|
120
110
|
ɴ = name_set_closure.( ɴ, self, old_ɴ ) if name_set_closure
|
121
111
|
ɴ = self.class.send( :validate_capitalization, ɴ ).to_sym
|
112
|
+
puts "NameMagic: Name adjusted to #{ɴ}." if DEBUG
|
122
113
|
return false if old_ɴ == ɴ # already named as required; nothing to do
|
123
114
|
# otherwise, rudely remove the collider, if any
|
124
115
|
pair = self.class.__instances__.rassoc( ɴ )
|
@@ -168,10 +159,12 @@ module NameMagic
|
|
168
159
|
end
|
169
160
|
|
170
161
|
# Makes the class use the namespace supplied as the argument. If no argument
|
171
|
-
# is given,
|
162
|
+
# is given, the class/module itself will be made its own namespace. Returns
|
163
|
+
# self.
|
172
164
|
#
|
173
165
|
def namespace! namespc=self
|
174
|
-
namespc.
|
166
|
+
namespc.extend ::NameMagic::NamespaceMethods unless namespc == self
|
167
|
+
tap { define_singleton_method :namespace do namespc end }
|
175
168
|
end
|
176
169
|
|
177
170
|
# Returns the instance identified by the argument. NameError is raised, if
|
@@ -181,6 +174,7 @@ module NameMagic
|
|
181
174
|
#
|
182
175
|
def instance arg
|
183
176
|
# In @instances hash, name 'nil' means nameless!
|
177
|
+
puts "NameMagic: #instance called with argument #{arg}." if DEBUG
|
184
178
|
msg = "'nil' is not a valid argument type for NameMagic#instance method!"
|
185
179
|
fail TypeError, msg if arg.nil?
|
186
180
|
# if the argument is an actual instance, just return it
|
@@ -283,31 +277,38 @@ module NameMagic
|
|
283
277
|
def serve_all_modules
|
284
278
|
todo = ( nameless_instances + __avid_instances__ ).map( &:object_id ).uniq
|
285
279
|
ObjectSpace.each_object Module do |ɱ| # for all the modules...
|
286
|
-
puts ɱ if
|
280
|
+
# ( puts ɱ if DEBUG ) rescue
|
287
281
|
ɱ.constants( false ).each do |const_ß| # and all the constants...
|
288
282
|
begin # insurance against constant dynamic loading fails
|
289
283
|
◉ = ɱ.const_get( const_ß )
|
290
284
|
rescue LoadError, StandardError
|
291
285
|
next
|
292
286
|
end
|
293
|
-
if todo.include? ◉.object_id then #
|
287
|
+
if todo.include? ◉.object_id then # we found a wanted object
|
288
|
+
puts "NameMagic: Wanted object found under #{const_ß}." if DEBUG
|
294
289
|
if __avid_instances__.map( &:object_id ).include? ◉.object_id # avid
|
290
|
+
puts "NameMagic: It is avid." if DEBUG
|
295
291
|
__avid_instances__ # 1. remove from avid list
|
296
292
|
.delete_if { |instance| instance.object_id == ◉.object_id }
|
297
293
|
◉.name! const_ß # 2. name rudely
|
298
294
|
else # not avid
|
295
|
+
puts "NameMagic: It is not avid." if DEBUG
|
299
296
|
ɴ = if @name_set_closure then # honor name_set_closure
|
300
297
|
@name_set_closure.( const_ß, ◉, nil )
|
298
|
+
.tap { |r| puts "The resulting name is #{r}." }
|
301
299
|
else const_ß end
|
302
300
|
ɴ = validate_capitalization( ɴ ).to_sym
|
301
|
+
puts "NameMagic: Name adjusted to #{ɴ}." if DEBUG
|
303
302
|
conflicter = begin # be cautious
|
304
303
|
namespace.const_get( ɴ )
|
305
304
|
rescue NameError
|
306
305
|
end
|
307
306
|
if conflicter then
|
307
|
+
puts "NameMagic: Conflicter exists named #{ɴ}." if DEBUG
|
308
308
|
raise NameError, "Another #{self} named '#{ɴ}' already " +
|
309
309
|
"exists!" unless conflicter == ◉
|
310
310
|
else
|
311
|
+
puts "NameMagic: No conflicter named #{ɴ}, about to use it." if DEBUG
|
311
312
|
__instances__[ ◉ ] = ɴ # add the instance to the namespace
|
312
313
|
namespace.const_set ɴ, ◉ # add the instance to the namespace
|
313
314
|
end
|
data/lib/y_support/version.rb
CHANGED