Class: Bigamy::HasOne

Inherits:
Proxy
  • Object
show all
Defined in:
lib/bigamy/proxy.rb

Direct Known Subclasses

ARHasOne, MongoHasOne

Instance Attribute Summary

Attributes inherited from Proxy

#klass, #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_getterObject



75
76
77
78
79
80
81
# File 'lib/bigamy/proxy.rb', line 75

def add_getter 
  me.class_eval "    def \#{name}\n      self.id.nil? ? nil : \#{target_klass}.first(:conditions => {:\#{foreign_key} => export_id_val(self.id)})\n    end\n  EOF\nend\n"

#add_setterObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bigamy/proxy.rb', line 83

def add_setter
  me.class_eval "    def \#{name}= v\n      raise NewRecordAssignment.new('Child must be saved') if v.new_record?\n      raise NewRecordAssignment.new('Parent must be saved') if self.new_record?\n      raise TypeError unless v.is_a? \#{klass}\n\n      v.\#{foreign_key} = export_id_val(self.id)\n      v.save!\n    end\n  EOF\nend\n"

#foreign_keyObject



71
72
73
# File 'lib/bigamy/proxy.rb', line 71

def foreign_key
  options[:foreign_key] || :"#{root_klass_name}_id"
end