named_value_class 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/named_value_class.rb +29 -12
- metadata +5 -5
data/lib/named_value_class.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'delegate'
|
2
2
|
|
3
|
-
|
3
|
+
# see README for documention
|
4
|
+
def NamedValueClass(attrs={},&block)
|
5
|
+
klass_name, superclass = attrs.first
|
6
|
+
attrs.delete(klass_name)
|
4
7
|
target = (self.class == Object ? Kernel : self)
|
5
8
|
|
6
9
|
target.module_eval "class #{klass_name} < DelegateClass(superclass); end"
|
@@ -9,20 +12,33 @@ def NamedValueClass(klass_name,superclass,&block)
|
|
9
12
|
klass.module_eval do
|
10
13
|
def self.inherited(child)
|
11
14
|
super
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
code = proc do |attrs={}|
|
16
|
+
name, value = attrs.first
|
17
|
+
begin
|
18
|
+
const_get(name) || const_get("NameError_#{name}")
|
19
|
+
rescue NameError #=> e
|
20
|
+
child.new(attrs)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
if (mod = const_get(self.to_s.sub(/::.+$/,''))) == Kernel
|
24
|
+
mod.instance_eval do
|
25
|
+
define_method child.to_s.sub(/^.+::/,''), &code
|
26
|
+
end
|
27
|
+
else
|
28
|
+
to_s.split(/::/)[0..-2].inject(Kernel) do |m,e|
|
29
|
+
m.const_get(e)
|
30
|
+
end.instance_eval do
|
31
|
+
module_eval do
|
32
|
+
define_singleton_method child.to_s.sub(/^.+::/,''), &code
|
19
33
|
end
|
20
34
|
end
|
21
35
|
end
|
22
36
|
end
|
23
37
|
|
24
|
-
def initialize(
|
25
|
-
@name =
|
38
|
+
def initialize(attrs = {})
|
39
|
+
@name, value = attrs.first
|
40
|
+
attrs.delete(@name)
|
41
|
+
@name = @name.to_s
|
26
42
|
super(value)
|
27
43
|
|
28
44
|
attrs.each do |(attr,val)|
|
@@ -114,11 +130,12 @@ def NamedValueClass(klass_name,superclass,&block)
|
|
114
130
|
end
|
115
131
|
EVAL
|
116
132
|
|
117
|
-
define_singleton_method klass_name do |
|
133
|
+
define_singleton_method klass_name do |attrs={}|
|
134
|
+
name, value = attrs.first
|
118
135
|
begin
|
119
136
|
klass.const_get(name) || klass.const_get("NameError_#{name}")
|
120
137
|
rescue NameError #=> e
|
121
|
-
klass.new(
|
138
|
+
klass.new(attrs)
|
122
139
|
end
|
123
140
|
end
|
124
141
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: named_value_class
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,8 +11,8 @@ bindir: bin
|
|
11
11
|
cert_chain: []
|
12
12
|
date: 2011-11-18 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
|
-
description: Quickly add class delegate constants which output their
|
15
|
-
values. This may be desirable for some DSLs.
|
14
|
+
description: Quickly add customizable class delegate constants which output their
|
15
|
+
names, not their values. This may be desirable for some DSLs.
|
16
16
|
email: mgarriss@gmail.com
|
17
17
|
executables: []
|
18
18
|
extensions: []
|
@@ -42,6 +42,6 @@ rubyforge_project:
|
|
42
42
|
rubygems_version: 1.8.11
|
43
43
|
signing_key:
|
44
44
|
specification_version: 3
|
45
|
-
summary: Quickly add class delegate constants which output their names,
|
46
|
-
values. This may be desirable for some DSLs.
|
45
|
+
summary: Quickly add customizable class delegate constants which output their names,
|
46
|
+
not their values. This may be desirable for some DSLs.
|
47
47
|
test_files: []
|