classy 1.0.0 → 1.0.1
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.
- data/VERSION +1 -1
- data/lib/classy/aliasable.rb +35 -10
- data/lib/classy/subclass_aware.rb +0 -2
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/lib/classy/aliasable.rb
CHANGED
@@ -1,11 +1,39 @@
|
|
1
|
+
# Aliasable allows you to assign aliases to a class (probably symbols, but any
|
2
|
+
# unique objects would work) and look it up again later with that alias. This
|
3
|
+
# alias-to-class hash is kept in a class variable, so each mapping is unique to
|
4
|
+
# a given class hierarchy. Possible uses for this include friendlier DSLs or
|
5
|
+
# additional layers of dynamic abstraction when specifying classes.
|
6
|
+
#
|
7
|
+
# Note: As mentioned, this module keeps its identity map in a class variable,
|
8
|
+
# @@classy_aliases, on the extending class. This could concievably lead to
|
9
|
+
# namespace conflicts and strange bugs in the unlikely event that this variable
|
10
|
+
# is used for anything else. Later versions may implement a hash of identity
|
11
|
+
# maps as a class variable on the Aliasble module itself, but for reasons of
|
12
|
+
# complexity and performance, that has not been done at this time.
|
13
|
+
#
|
14
|
+
# Example:
|
15
|
+
#
|
16
|
+
# class Parent
|
17
|
+
# extend Aliasable
|
18
|
+
# aka :pop
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# class Child < Parent
|
22
|
+
# aka :kid
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# Parent.find(:pop) # => Parent
|
26
|
+
# Parent.find(:kid) # => Child
|
27
|
+
#
|
28
|
+
# More complex usage examples can be found in the aliasable_spec.rb file.
|
29
|
+
#
|
1
30
|
module Aliasable
|
2
31
|
def self.extended (klass) #nodoc;
|
3
32
|
klass.class_exec do
|
4
|
-
class_variable_set(:@@
|
33
|
+
class_variable_set(:@@classy_aliases, {})
|
5
34
|
end
|
6
35
|
end
|
7
36
|
|
8
|
-
##
|
9
37
|
# When passed a class, just returns it. When passed a symbol that is an
|
10
38
|
# alias for a class, returns that class.
|
11
39
|
#
|
@@ -13,17 +41,15 @@ module Aliasable
|
|
13
41
|
#
|
14
42
|
def find (klass)
|
15
43
|
return klass if klass.kind_of? Class
|
16
|
-
class_variable_get(:@@
|
44
|
+
class_variable_get(:@@classy_aliases)[klass] or raise ArgumentError, "Could not find alias #{klass}"
|
17
45
|
end
|
18
46
|
|
19
|
-
##
|
20
47
|
# Forget all known aliases.
|
21
48
|
#
|
22
49
|
def forget_aliases
|
23
|
-
class_variable_get(:@@
|
50
|
+
class_variable_get(:@@classy_aliases).clear
|
24
51
|
end
|
25
52
|
|
26
|
-
##
|
27
53
|
# Specifies a symbol (or several) that a given framework might be known
|
28
54
|
# by. For example, if you wanted to refer to RSpec by :rspec or :spec,
|
29
55
|
# you might do this:
|
@@ -35,16 +61,15 @@ module Aliasable
|
|
35
61
|
#
|
36
62
|
def aka (*names)
|
37
63
|
names.each do |name|
|
38
|
-
raise ArgumentError, "Called aka with an alias that is already taken." if class_variable_get(:@@
|
39
|
-
class_variable_get(:@@
|
64
|
+
raise ArgumentError, "Called aka with an alias that is already taken." if class_variable_get(:@@classy_aliases).include? name
|
65
|
+
class_variable_get(:@@classy_aliases)[name] = self
|
40
66
|
end
|
41
67
|
end
|
42
68
|
|
43
|
-
##
|
44
69
|
# Return a hash of known aliases to Class objects
|
45
70
|
#
|
46
71
|
def aliases
|
47
|
-
class_variable_get(:@@
|
72
|
+
class_variable_get(:@@classy_aliases).dup
|
48
73
|
end
|
49
74
|
|
50
75
|
end
|
@@ -13,7 +13,6 @@ module SubclassAware
|
|
13
13
|
class_exec { class_variable_get(:@@subclasses).add sub }
|
14
14
|
end
|
15
15
|
|
16
|
-
##
|
17
16
|
# Return an array of all known subclasses (and sub-subclasses, etc) of this
|
18
17
|
# class.
|
19
18
|
#
|
@@ -21,7 +20,6 @@ module SubclassAware
|
|
21
20
|
class_exec { class_variable_get(:@@subclasses).to_a }
|
22
21
|
end
|
23
22
|
|
24
|
-
##
|
25
23
|
# Clear all info about known subclasses. Usefull for testing, but it is
|
26
24
|
# unlikely you would use it for much else.
|
27
25
|
#
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: classy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Hyland
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-25 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|