26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/bj/attributes.rb', line 26
def attributes *a, &b
unless a.empty?
returned = Hash.new
hashes, names = a.partition{|x| Hash === x}
names_and_defaults = {}
hashes.each{|h| names_and_defaults.update h}
names.flatten.compact.each{|name| names_and_defaults.update name => nil}
initializers = __attributes__.initializers
names_and_defaults.each do |name, default|
raise NameError, "bad instance variable name '@#{ name }'" if "@#{ name }" =~ %r/[!?=]$/o
name = name.to_s
initialize = b || lambda { default }
initializer = lambda do |this|
Object.instance_method('instance_eval').bind(this).call &initialize
end
initializer_id = initializer.object_id
__attributes__.initializers[name] = initializer
module_eval " def \#{ name }=(*value, &block)\n value.unshift block if block\n @\#{ name } = value.first\n end\n code\n\n module_eval <<-code\n def \#{ name }(*value, &block)\n value.unshift block if block\n return self.send('\#{ name }=', value.first) unless value.empty?\n \#{ name }! unless defined? @\#{ name }\n @\#{ name }\n end\n code\n\n module_eval <<-code\n def \#{ name }!\n initializer = ObjectSpace._id2ref \#{ initializer_id }\n self.\#{ name } = initializer.call(self)\n @\#{ name }\n end\n code\n\n module_eval <<-code\n def \#{ name }?\n \#{ name }\n end\n code\n\n attributes << name\n returned[name] = initializer \n end\n\n returned\n else\n begin\n __attribute_list__\n rescue NameError\n singleton_class =\n class << self\n self\n end\n klass = self\n singleton_class.module_eval do\n attribute_list = List.new \n define_method('attribute_list'){ klass == self ? attribute_list : raise(NameError) }\n alias_method '__attribute_list__', 'attribute_list'\n end\n __attribute_list__\n end\n end\nend\n"
|