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 " def \#{name}\n \#{name}_association.first\n end\n\n def \#{name}=(child_resource)\n \#{name}_association.replace(child_resource.nil? ? [] : [ child_resource ])\n end\n\n private\n\n def \#{name}_association\n @\#{name}_association ||= begin\n unless relationship = model.relationships(\#{repository_name.inspect})[:\#{name}]\n raise ArgumentError, \"Relationship \#{name.inspect} does not exist in \\\#{model}\"\n end\n association = Associations::OneToMany::Proxy.new(relationship, self)\n parent_associations << association\n association\n end\n end\n EOS\n\n model.relationships(repository_name)[name] = if options.has_key?(:through)\n RelationshipChain.new(\n :child_model => options.fetch(:class_name, Extlib::Inflection.classify(name)),\n :parent_model => model,\n :repository_name => repository_name,\n :near_relationship_name => options[:through],\n :remote_relationship_name => options.fetch(:remote_name, name),\n :parent_key => options[:parent_key],\n :child_key => options[:child_key]\n )\n else\n Relationship.new(\n name,\n repository_name,\n options.fetch(:class_name, Extlib::Inflection.classify(name)),\n model,\n options\n )\n end\nend\n", __FILE__, __LINE__ |