Module: DataMapper::Associations::ManyToOne
- Extended by:
- DataMapper::Assertions
- Defined in:
- lib/dm-core/associations/many_to_one.rb
Defined Under Namespace
Classes: Proxy
Class Method Summary collapse
-
.setup(name, model, options = {}) ⇒ Object
private
Setup many 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 many 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 |
# File 'lib/dm-core/associations/many_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.nil? ? nil : \#{name}_association\n end\n\n def \#{name}=(parent)\n \#{name}_association.replace(parent)\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 = Proxy.new(relationship, self)\n child_associations << association\n association\n end\n end\n EOS\n\n model.relationships(repository_name)[name] = Relationship.new(\n name,\n repository_name,\n model,\n options.fetch(:class_name, Extlib::Inflection.classify(name)),\n options\n )\nend\n", __FILE__, __LINE__ |