named_value_class 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/named_value_class.rb +31 -3
  2. metadata +2 -2
@@ -1,12 +1,26 @@
1
1
  require 'delegate'
2
2
 
3
- def NamedValueClass(klass_name,superclass, &block)
3
+ def NamedValueClass(klass_name,superclass,&block)
4
4
  target = (self.class == Object ? Kernel : self)
5
5
 
6
6
  target.module_eval "class #{klass_name} < DelegateClass(superclass); end"
7
7
  klass = target.const_get(klass_name)
8
8
 
9
9
  klass.module_eval do
10
+ def self.inherited(child)
11
+ super
12
+ this = self
13
+ const_get(self.to_s.sub(/::.+/,'')).instance_eval do
14
+ define_method child.to_s do |name,value,attrs={}|
15
+ begin
16
+ const_get(name) || const_get("NameError_#{name}")
17
+ rescue NameError #=> e
18
+ child.new(name,value,attrs)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
10
24
  def initialize(name,value,attrs = {})
11
25
  @name = name.to_s
12
26
  super(value)
@@ -20,6 +34,7 @@ def NamedValueClass(klass_name,superclass, &block)
20
34
 
21
35
  named_values_module = self.class.const_get('NamedValues')
22
36
  named_values_collection = named_values_module.const_get('Collection')
37
+
23
38
  begin
24
39
  self.class.const_set(@name, self)
25
40
  named_values_module.const_set(@name, self)
@@ -36,8 +51,13 @@ def NamedValueClass(klass_name,superclass, &block)
36
51
  end
37
52
  EVAL
38
53
  named_values_module.module_eval <<-EVAL
39
- def self.#{@name}()
40
- #{name_error_name}
54
+ module NameErrors
55
+ def self.#{@name}()
56
+ #{name_error_name}
57
+ end
58
+ def #{@name}()
59
+ #{name_error_name}
60
+ end
41
61
  end
42
62
  def #{@name}()
43
63
  #{name_error_name}
@@ -83,6 +103,14 @@ def NamedValueClass(klass_name,superclass, &block)
83
103
  klass.class_eval <<-EVAL
84
104
  module NamedValues
85
105
  Collection = []
106
+ def self.included(othermod)
107
+ othermod.class_eval do
108
+ extend NameErrors if defined? NameErrors
109
+ end
110
+ self.class_eval do
111
+ extend NameErrors if defined? NameErrors
112
+ end
113
+ end
86
114
  end
87
115
  EVAL
88
116
 
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.2
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-17 00:00:00.000000000Z
12
+ date: 2011-11-18 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Quickly add class delegate constants which output their names, not their
15
15
  values. This may be desirable for some DSLs.