51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/mongodoc/attributes.rb', line 51
def has_one(*args)
options = args.
assoc_class = if class_name = options.delete(:class_name)
self.class_from_name(class_name)
end
args.each do |name|
_associations << name unless _associations.include?(name)
attr_reader name
define_method("#{name}=") do |value|
association = instance_variable_get("@#{name}")
unless association
association = Associations::DocumentProxy.new(:root => _root || self, :parent => self, :assoc_name => name, :assoc_class => assoc_class || self.class.class_from_name(name))
instance_variable_set("@#{name}", association)
end
association.document = value
end
validates_embedded name, :if => Proc.new { !send(name).nil? }
end
end
|