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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/fattr.rb', line 61
def fattrs(*args, &block)
unless args.empty?
returned = Fattr::Result.new
args.flatten!
args.compact!
all_hashes = args.all?{|arg| Hash===arg}
names_and_configs = {}
if all_hashes
args.each do |hash|
hash.each do |key, val|
name = key.to_s
config = Hash===val ? val : {:default => val}
names_and_configs[name] = config
end
end
else
config = Hash===args.last ? args.pop : {}
names = args.select{|arg| Symbol===arg or String===arg}.map{|arg| arg.to_s}
names.each do |name|
names_and_configs[name] = config
end
end
names_and_configs.each do |name, _config|
raise(NameError, "bad instance variable name '@#{ name }'") if("@#{ name }" =~ %r/[!?=]$/o)
name = name.to_s
default = nil
default = _config[:default] if _config.has_key?(:default)
default = _config['default'] if _config.has_key?('default')
inheritable = false
if Module===self
inheritable = _config[:inheritable] if _config.has_key?(:inheritable)
inheritable = _config['inheritable'] if _config.has_key?('inheritable')
end
initialize = (
if inheritable
lambda do |*ignored|
parents = ancestors[1..-1]
catch(:value) do
parents.each do |parent|
throw(:value, parent.send(name)) if parent.respond_to?(name)
end
block ? block.call : default
end
end
else
block || lambda{|*ignored| default }
end
)
initializer = lambda do |this|
Object.instance_method('instance_eval').bind(this).call(&initialize)
end
__fattrs__.initializers[name] = initializer
compile = lambda do |code|
begin
module_eval(code)
rescue SyntaxError
raise(SyntaxError, "\n#{ code }\n")
end
end
code = <<-code
def #{ name }=(*value, &block)
value.unshift block if block
@#{ name } = value.first
end
code
compile[code]
code = <<-code
def #{ name }(*value, &block)
value.unshift block if block
return self.send('#{ name }=', value.first) unless value.empty?
#SEMAPHORE.synchronize{ #{ name }! unless defined? @#{ name } }
#{ name }! unless defined? @#{ name }
@#{ name }
end
code
compile[code]
=begin
code = <<-code
def #{ name }!
initializer = ObjectSpace._id2ref #{ initializer_id }
self.#{ name } = initializer.call(self)
@#{ name }
end
code
compile[code]
=end
module_eval do
define_method :"#{ name }!" do
self.send :"#{ name }=", initializer.call(self)
self.instance_variable_get :"@#{name}"
end
end
code = <<-code
def #{ name }?
self.#{ name }
end
code
compile[code]
fattrs << name
returned[name] = initializer
end
returned
else
begin
__fattr_list__
rescue NameError, TypeError
singleton_class =
class << self
self
end
klass = self
singleton_class.module_eval do
fattr_list = List.new
define_method('fattr_list'){ klass == self ? fattr_list : raise(NameError) }
alias_method '__fattr_list__', 'fattr_list'
end
__fattr_list__
end
end
end
|