6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/params_ready/extensions/class_reader_writer.rb', line 6
def class_reader_writer(method_name)
ivar = :"@#{method_name}"
define_singleton_method method_name do |*args|
if args.length == 0
value = instance_variable_get(ivar)
if value.nil?
if superclass.respond_to? method_name
superclass.send method_name
else
raise ParamsReadyError, "Class variable '#{ivar}' not set for '#{name}'"
end
else
value
end
elsif args.length == 1
if instance_variable_get(ivar).nil?
instance_variable_set(ivar, args[0])
else
raise ParamsReadyError, "Class variable '#{ivar}' already set for '#{name}'"
end
else
raise ParamsReadyError, "Unexpected parameters to '#{method_name}': #{args}"
end
end
end
|