13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/opto/model/association/has_one/declaration.rb', line 13
def has_one(relation, klass, options = {})
if relations.include?(relation)
raise RuntimeError, "Duplicate has_one relation '#{relation}'"
end
relations << relation
define_method relation do
if instance_variable_defined?("@#{relation}")
instance_variable_get("@#{relation}")
else
instance_variable_set("@#{relation}", Proxy.new(self, klass, relation, nil, options))
end
end
define_method "#{relation}=" do |arg|
if arg.kind_of?(klass) || arg.nil?
instance_variable_set("@#{relation}", Proxy.new(self, klass, relation, arg, options))
elsif arg.kind_of?(Hash)
self.send(relation).new(arg)
else
raise TypeError, "Expected an instance of #{klass} or Hash or nil"
end
end
end
|