10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/signalize/struct.rb', line 10
def signal_accessor(*names)
names.each do |name|
members.push(name.to_sym) unless members.find { _1 == name.to_sym }
signal_getter_name = "#{name}_signal".freeze
ivar_name = "@#{name}".freeze
define_method "#{name}_signal" do
instance_variable_get(ivar_name)
end
define_method name do
send(signal_getter_name)&.value
end
define_method "#{name}=" do |val|
if instance_variable_defined?(ivar_name)
raise Signalize::Error, "Cannot assign a signal to a signal value" if val.is_a?(Signalize::Signal)
sig = instance_variable_get(ivar_name)
if sig.is_a?(Signalize::Computed)
raise Signalize::Error, "Cannot set value of computed signal `#{ivar_name.delete_prefix("@")}'"
end
sig.value = val
else
val = Signalize.signal(val) unless val.is_a?(Signalize::Computed)
instance_variable_set(ivar_name, val)
end
end
end
end
|