Class: Bigamy::HasOne
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Proxy
#me, #methods_added, #name, #options, #primary_key
Instance Method Summary collapse
Methods inherited from Proxy
#create_accessors, #divorce_everyone, #initialize, #root_klass, #root_klass_name, #serialize_foreign_key, #target_klass, #target_klass_name
Constructor Details
This class inherits a constructor from Bigamy::Proxy
Instance Method Details
#add_getter ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/bigamy/proxy.rb', line 74 def add_getter me.class_eval <<-EOF def #{name} self.#{primary_key}.nil? ? nil : #{target_klass}.first(:conditions => {:#{foreign_key} => export_id_val(self.#{primary_key})}) end EOF end |
#add_setter ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/bigamy/proxy.rb', line 82 def add_setter me.class_eval <<-EOF def #{name}= v if v.nil? new_id = nil else raise NewRecordAssignment.new('Child must be saved') if v.new_record? raise NewRecordAssignment.new('Parent must be saved') if self.new_record? raise TypeError unless v.is_a? #{klass} new_id = export_id_val(self.#{primary_key}) end if #{name} #{name}.update_attributes :#{foreign_key} => nil end v.#{foreign_key} = new_id v.save! end EOF end |
#foreign_key ⇒ Object
70 71 72 |
# File 'lib/bigamy/proxy.rb', line 70 def foreign_key [:foreign_key] || :"#{root_klass_name}_id" end |