Module: DataMapper::Associations::OneToOne
- Extended by:
- DataMapper::Assertions
- Defined in:
- lib/dm-core/associations/one_to_one.rb
Class Method Summary collapse
-
.setup(name, model, options = {}) ⇒ Object
private
Setup one to one relationship between two models -.
Methods included from DataMapper::Assertions
Class Method Details
.setup(name, model, options = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Setup one to one relationship between two models -
9 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/dm-core/associations/one_to_one.rb', line 9 def self.setup(name, model, = {}) assert_kind_of 'name', name, Symbol assert_kind_of 'model', model, Model assert_kind_of 'options', , Hash repository_name = model.repository.name model.class_eval <<-EOS, __FILE__, __LINE__ def #{name} #{name}_association.first end def #{name}=(child_resource) #{name}_association.replace(child_resource.nil? ? [] : [ child_resource ]) end private def #{name}_association @#{name}_association ||= begin unless relationship = model.relationships(#{repository_name.inspect})[:#{name}] raise ArgumentError, "Relationship #{name.inspect} does not exist in \#{model}" end association = Associations::OneToMany::Proxy.new(relationship, self) parent_associations << association association end end EOS model.relationships(repository_name)[name] = if .has_key?(:through) RelationshipChain.new( :child_model => .fetch(:class_name, Extlib::Inflection.classify(name)), :parent_model => model, :repository_name => repository_name, :near_relationship_name => [:through], :remote_relationship_name => .fetch(:remote_name, name), :parent_key => [:parent_key], :child_key => [:child_key] ) else Relationship.new( name, repository_name, .fetch(:class_name, Extlib::Inflection.classify(name)), model, ) end end |