named_value_class 0.1.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 +52 -0
  2. metadata +46 -0
@@ -0,0 +1,52 @@
1
+ require 'delegate'
2
+
3
+ def NamedValueClass(klass_name,superclass,&block)
4
+ target = (self.class == Object ? Kernel : self)
5
+
6
+ target.module_eval "class #{klass_name} < DelegateClass(superclass); end"
7
+ klass = target.const_get(klass_name)
8
+
9
+ klass.module_eval do
10
+ def initialize(name,value)
11
+ @name = name.to_s
12
+ super(value)
13
+
14
+ # THINK is this making it harder than it needs to be?
15
+ named_values_module = self.class.const_get('NamedValues')
16
+ named_values_collection = named_values_module.const_get('Collection')
17
+ self.class.const_set(@name, self)
18
+ named_values_module.const_set(@name, self)
19
+ named_values_collection << self
20
+ end
21
+
22
+ def to_s # :nodoc:
23
+ @name
24
+ end
25
+
26
+ def value_to_s # :nodoc:
27
+ method_missing :to_s
28
+ end
29
+
30
+ def inspect # :nodoc:
31
+ @name
32
+ end
33
+
34
+ def value_inspect # :nodoc:
35
+ method_missing :inspect
36
+ end
37
+
38
+ class_eval(&block) if block
39
+ end
40
+
41
+ klass.class_eval <<-EVAL
42
+ module NamedValues
43
+ Collection = []
44
+ end
45
+ EVAL
46
+
47
+ define_singleton_method klass_name do |name,value|
48
+ klass.const_get(name) rescue klass.new(name,value)
49
+ end
50
+
51
+ klass
52
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: named_value_class
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Garriss
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-15 00:00:00.000000000Z
13
+ dependencies: []
14
+ description:
15
+ email: mgarriss@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/named_value_class.rb
21
+ homepage: http://github.com/mgarriss/named_value_class
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.11
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Quickly add class delegate constants which output their names, not their
45
+ values. This may be desirable for some DSLs.
46
+ test_files: []