named_value_class 0.1.0 → 0.2.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 +11 -4
  2. metadata +2 -2
@@ -1,16 +1,23 @@
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 initialize(name,value)
10
+ def initialize(name,value,attrs = {})
11
11
  @name = name.to_s
12
12
  super(value)
13
13
 
14
+ attrs.each do |(attr,val)|
15
+ self.class.instance_eval do
16
+ attr_accessor attr
17
+ end
18
+ instance_variable_set "@#{attr}", val
19
+ end
20
+
14
21
  # THINK is this making it harder than it needs to be?
15
22
  named_values_module = self.class.const_get('NamedValues')
16
23
  named_values_collection = named_values_module.const_get('Collection')
@@ -44,8 +51,8 @@ def NamedValueClass(klass_name,superclass,&block)
44
51
  end
45
52
  EVAL
46
53
 
47
- define_singleton_method klass_name do |name,value|
48
- klass.const_get(name) rescue klass.new(name,value)
54
+ define_singleton_method klass_name do |name,value,attrs={}|
55
+ klass.const_get(name) rescue klass.new(name,value,attrs)
49
56
  end
50
57
 
51
58
  klass
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.1.0
4
+ version: 0.2.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-15 00:00:00.000000000Z
12
+ date: 2011-11-16 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: mgarriss@gmail.com