Class: DataMapper::Associations::HasOneAssociation
- Defined in:
- lib/data_mapper/associations/has_one_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) ⇒ HasOneAssociation
constructor
A new instance of HasOneAssociation.
- #set(val) ⇒ Object
Constructor Details
#initialize(instance, association_name, options) ⇒ HasOneAssociation
Returns a new instance of HasOneAssociation.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/data_mapper/associations/has_one_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 |
# File 'lib/data_mapper/associations/has_one_association.rb', line 23 def self.setup(klass, association_name, ) # 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 HasOneAssociation 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} = HasOneAssociation.new(self, "#{association_name}", #{.inspect})) end EOS end |
Instance Method Details
#build(options = {}) ⇒ Object
80 81 82 |
# File 'lib/data_mapper/associations/has_one_association.rb', line 80 def build( = {}) @result = @associated_class.new() end |
#create(options = {}) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/data_mapper/associations/has_one_association.rb', line 72 def create( = {}) associated = @associated_class.new() if associated.save @instance.send("#{@associated_class.foreign_key}=", associated.id) @result = associated end end |
#find ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/data_mapper/associations/has_one_association.rb', line 52 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.group_by { |instance| instance.key } # Fetch the foreign objects for all instances in the current object's loaded-set. @instance.session.all(@associated_class, foreign_key => set.keys).each do |association| set[association.instance_variable_get(instance_variable_name)].first.send(setter_method, association) end end return @result end |
#foreign_key ⇒ Object
88 89 90 |
# File 'lib/data_mapper/associations/has_one_association.rb', line 88 def foreign_key @foreign_key ||= (@options[:foreign_key] || @instance.session.mappings[@instance.class].default_foreign_key) end |
#set(val) ⇒ Object
84 85 86 |
# File 'lib/data_mapper/associations/has_one_association.rb', line 84 def set(val) @result = val end |