Module: SimpleMapper::Associations::AssociationMacros

Defined in:
lib/simple_mapper/default_plugins/associations.rb

Overview

AssociationMacros

This class is automatically extended into your model class when you include SimpleMapper::Associations, and therefore gives all its methods to your model class.

There are currently only two types of associations: :single and :many. These can be customized by applying primary or foreign association options (see Association). Different from ActiveRecord or DataMapper, these methods ONLY define an association mapping between models, they don’t rely on an accessor method. By default, association accessor methods are created, but this can be disabled by specifying :accessor => false, or simply by overwriting the accessor method after creating the association. For :single type associations, a setter method is created, of the style ‘association=’. For example, if a Person.has_many :pets, then a pet would by default have a person= method.

These macros are the only place that assume a method ‘key’ in your model class in order to automatically assume assocation attributes. For example, if a Door.has_many :doorknobs and Door.key == :id, then Doorknob will be assumed to have methods ‘door_id’ and ‘door_id=’. To change that up a little, if a Door.has_many(:doorknobs).as(:owner) and Door.key == :name, then Doorknob will be assumed to have methods ‘owner_name’ and ‘owner_name=’.

See Association for more information on assumptions that are made about your model class.

Instance Method Summary collapse

Instance Method Details

#association(name, type, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/simple_mapper/default_plugins/associations.rb', line 60

def association(name, type, options={})
  accessor = options.has_key?(:accessor) ? options.delete(:accessor) : true
  raise ArgumentError, "type must be :single or :many" unless [:single, :many].include?(type)
  associations[name] = Association.new(self, type, {:class_name => name}.merge(options))
  define_method(name.to_s+'=') do |object|
    association_set(name).associate(object)
  end if type == :single
  define_method(name) do
    association_set(name)
  end if accessor
  associations[name]
end

#associationsObject



57
58
59
# File 'lib/simple_mapper/default_plugins/associations.rb', line 57

def associations
  @associations ||= {}
end

#belongs_to(name, options = {}) ⇒ Object



79
80
81
82
83
# File 'lib/simple_mapper/default_plugins/associations.rb', line 79

def belongs_to(name, options={})
  assoc = association(name, :single, options)
  assoc.dynamic {|assoc,obj| assoc.primary(assoc.associated_klass.key => Inflector.underscore(name.to_s) + '_' + assoc.associated_klass.key.to_s)} unless self.name.underscore == assoc.instance_variable_get(:@options)[:class_name].to_s
  assoc
end

#has_many(name, options = {}) ⇒ Object



85
86
87
88
89
# File 'lib/simple_mapper/default_plugins/associations.rb', line 85

def has_many(name, options={})
  assoc = association(name, :many, {:class_name => Inflector.singularize(name)}.merge(options))
  assoc.dynamic {|assoc,obj| assoc.foreign(Inflector.underscore(assoc.act_as.to_s) + '_' + self.key.to_s => self.key) if assoc.act_as} unless self.name.underscore == assoc.instance_variable_get(:@options)[:class_name].to_s
  assoc
end

#has_one(name, options = {}) ⇒ Object



73
74
75
76
77
# File 'lib/simple_mapper/default_plugins/associations.rb', line 73

def has_one(name, options={})
  assoc = association(name, :single, options)
  assoc.dynamic {|assoc,obj| assoc.foreign(Inflector.underscore(assoc.act_as.to_s) + '_' + self.key.to_s => self.key) if assoc.act_as} unless self.name.underscore == assoc.instance_variable_get(:@options)[:class_name].to_s
  assoc
end