y_support 2.0.2 → 2.0.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/local_object.rb +1 -2
- data/lib/y_support/name_magic.rb +30 -13
- data/lib/y_support/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6e8e25158487612ee9fd0d9b88dd3cbaa588767
|
4
|
+
data.tar.gz: 8e4a78cde0deb07dda57cd9ff93f5f64c4a991be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e9c8826eab83bf22364655f6559c5c11114162bd35dee5d01e268f971216d35e4131d5f50100faf01f7644a952a95dcd6d00edd8acb688cd671ec354e3d3d487
|
7
|
+
data.tar.gz: 0901936832de50bd38831a31deb565af1eaf8f545dec53de62ba54c8a672dfdd0e37602ed796eb6a1cfc871069c001c3bd545ee7e9f6de875c0350f99c0ee767
|
data/lib/y_support/name_magic.rb
CHANGED
@@ -1,26 +1,34 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
require 'y_support'
|
3
3
|
|
4
|
-
#
|
5
|
-
#
|
4
|
+
# This mixin imitates Ruby constant magic and automates the named argument
|
5
|
+
# :name (alias :ɴ). One thus can write:
|
6
6
|
#
|
7
|
+
# <tt>class Someclass; include NameMagic end</tt>
|
7
8
|
# <tt>SomeName = SomeClass.new</tt>
|
8
9
|
#
|
9
10
|
# and the resulting object will know its #name:
|
10
11
|
#
|
11
12
|
# <tt>SomeName.name = "SomeName"</tt>
|
12
13
|
#
|
13
|
-
# This is done by searching the whole Ruby namespace for constants to which
|
14
|
-
# the object
|
15
|
-
#
|
16
|
-
#
|
14
|
+
# This is done by searching the whole Ruby namespace for constants, to which
|
15
|
+
# the object might have been assigned. The search is performed by the method
|
16
|
+
# #const_magic defined by this mixin. Once the object is found to be assigned
|
17
|
+
# to a constant, and named accordingly, its subsequent assignments to other
|
18
|
+
# constants have no additional effect.
|
17
19
|
#
|
18
|
-
#
|
20
|
+
# Alternative way to create a named object is by specifying :name (alias :ɴ)
|
19
21
|
# named argument:
|
20
22
|
#
|
21
|
-
#
|
23
|
+
# <tt>SomeClass.new a, b, ..., name: "SomeName", aa: v1, bb: v2 ...</tt>
|
22
24
|
#
|
23
|
-
#
|
25
|
+
# Lastly, a name can be assigned by #name= accssor, as in
|
26
|
+
#
|
27
|
+
# <tt>o = SomeClass.new</tt>
|
28
|
+
# <tt>o.name = "SomeName"</tt>
|
29
|
+
#
|
30
|
+
# Hook is provided for when the name magic is performed, as well as when the
|
31
|
+
# name is retrieved.
|
24
32
|
#
|
25
33
|
module NameMagic
|
26
34
|
DEBUG = false
|
@@ -69,13 +77,18 @@ module NameMagic
|
|
69
77
|
ɴ = self.class.__instances__[ self ]
|
70
78
|
if ɴ then
|
71
79
|
name_get_closure = self.class.instance_variable_get :@name_get_closure
|
72
|
-
|
73
|
-
else
|
74
|
-
return nil
|
75
|
-
end
|
80
|
+
name_get_closure ? name_get_closure.( ɴ ) : ɴ
|
81
|
+
else nil end
|
76
82
|
end
|
77
83
|
alias ɴ name
|
78
84
|
|
85
|
+
# Retrieves either an instance name (if present), or an object id.
|
86
|
+
#
|
87
|
+
def name_or_object_id
|
88
|
+
name || object_id
|
89
|
+
end
|
90
|
+
alias ɴ_ name_or_object_id
|
91
|
+
|
79
92
|
# Names an instance, cautiously (ie. no overwriting of existing names).
|
80
93
|
#
|
81
94
|
def name=( ɴ )
|
@@ -158,6 +171,10 @@ module NameMagic
|
|
158
171
|
#
|
159
172
|
def instance arg
|
160
173
|
const_magic
|
174
|
+
# Reject nil argument. (In @instances hash, nameless instances are given
|
175
|
+
# name nil, so 'nil' cannot be a real name.)
|
176
|
+
fail TypeError, "'nil' is not a valid argument type for " +
|
177
|
+
"NameMagic#instance method!" if arg.nil?
|
161
178
|
# if the argument is an actual instance, just return it
|
162
179
|
return arg if __instances__.keys.include? arg
|
163
180
|
# otherwise, treat it as name
|
data/lib/y_support/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: y_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- boris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|