Class: DataMapper::Associations::BelongsToAssociation
- Inherits:
-
Object
- Object
- DataMapper::Associations::BelongsToAssociation
- Defined in:
- lib/data_mapper/associations/belongs_to_association.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build(options = {}) ⇒ Object
- #create(options = {}) ⇒ Object
- #find ⇒ Object
- #foreign_key ⇒ Object
-
#initialize(instance, association_name, options) ⇒ BelongsToAssociation
constructor
A new instance of BelongsToAssociation.
- #set(val) ⇒ Object
Constructor Details
#initialize(instance, association_name, options) ⇒ BelongsToAssociation
Returns a new instance of BelongsToAssociation.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/data_mapper/associations/belongs_to_association.rb', line 6 def initialize(instance, association_name, ) @instance = instance @association_name = association_name @options = @associated_class = if .has_key?(:class) || .has_key?(:class_name) associated_class_name = ([:class] || [:class_name]) if associated_class_name.kind_of?(String) Kernel.const_get(Inflector.classify(associated_class_name)) else associated_class_name end else Kernel.const_get(Inflector.classify(association_name)) end end |
Class Method Details
.setup(klass, association_name, options) ⇒ Object
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 |
# File 'lib/data_mapper/associations/belongs_to_association.rb', line 23 def self.setup(klass, association_name, ) foreign_key = [:foreign_key] || ("#{association_name}_id") klass.property foreign_key.to_sym, :integer # Define the association instance method (i.e. Exhibit#zoo) klass.class_eval <<-EOS def create_#{association_name}(options = {}) #{association_name}_association.create(options) end def build_#{association_name}(options = {}) #{association_name}_association.build(options) end def #{association_name} # Let the BelongsToAssociation do the finding, just to keep things neat around here... #{association_name}_association.find end def #{association_name}=(value) #{association_name}_association.set(value) end private def #{association_name}_association @#{association_name} || (@#{association_name} = BelongsToAssociation.new(self, "#{association_name}", #{.inspect})) end EOS end |
Instance Method Details
#build(options = {}) ⇒ Object
84 85 86 |
# File 'lib/data_mapper/associations/belongs_to_association.rb', line 84 def build( = {}) @result = @associated_class.new() end |
#create(options = {}) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/data_mapper/associations/belongs_to_association.rb', line 76 def create( = {}) associated = @associated_class.new() if associated.save @instance.send("#{@associated_class.foreign_key}=", associated.id) @result = associated end end |
#find ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/data_mapper/associations/belongs_to_association.rb', line 54 def find return @result unless @result.nil? unless @instance.loaded_set.nil? # Temp variable for the instance variable name. setter_method = "#{@association_name}=".to_sym instance_variable_name = "@#{foreign_key}".to_sym set = @instance.loaded_set.instances.group_by { |instance| instance.instance_variable_get(instance_variable_name) } # Fetch the foreign objects for all instances in the current object's loaded-set. @instance.session.find(@associated_class, :all, :id => set.keys).each do |owner| set[owner.key].each do |instance| instance.send(setter_method, owner) end end end return @result end |
#foreign_key ⇒ Object
92 93 94 |
# File 'lib/data_mapper/associations/belongs_to_association.rb', line 92 def foreign_key @foreign_key ||= (@options[:foreign_key] || @instance.session.schema[@associated_class].default_foreign_key) end |
#set(val) ⇒ Object
88 89 90 |
# File 'lib/data_mapper/associations/belongs_to_association.rb', line 88 def set(val) @result = val end |