Class: DataMapper::Associations::HasAndBelongsToManyAssociation
- Includes:
- Enumerable
- Defined in:
- lib/data_mapper/associations/has_and_belongs_to_many_association.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #find ⇒ Object
- #foreign_key ⇒ Object
-
#initialize(instance, association_name, options) ⇒ HasAndBelongsToManyAssociation
constructor
A new instance of HasAndBelongsToManyAssociation.
- #inspect ⇒ Object
- #set(results) ⇒ Object
- #size ⇒ Object (also: #length)
Constructor Details
#initialize(instance, association_name, options) ⇒ HasAndBelongsToManyAssociation
Returns a new instance of HasAndBelongsToManyAssociation.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/data_mapper/associations/has_and_belongs_to_many_association.rb', line 7 def initialize(instance, association_name, ) @instance = instance @association_name = association_name.to_sym @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 @join_table_name = @options.has_key?(:join_table_name) ? @options[:join_table_name] : [Inflector.tableize(@instance.class.name), Inflector.tableize(@associated_class.name)].sort.join('_') end |
Class Method Details
.setup(klass, association_name, options) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/data_mapper/associations/has_and_belongs_to_many_association.rb', line 26 def self.setup(klass, association_name, ) # Define the association instance method (i.e. Project#tasks) klass.class_eval <<-EOS def #{association_name} @#{association_name} || (@#{association_name} = HasAndBelongsToManyAssociation.new(self, "#{association_name}", #{.inspect})) end EOS end |
Instance Method Details
#[](key) ⇒ Object
46 47 48 |
# File 'lib/data_mapper/associations/has_and_belongs_to_many_association.rb', line 46 def [](key) entries[key] end |
#each ⇒ Object
37 38 39 |
# File 'lib/data_mapper/associations/has_and_belongs_to_many_association.rb', line 37 def each find.each { |item| yield item } end |
#empty? ⇒ Boolean
50 51 52 |
# File 'lib/data_mapper/associations/has_and_belongs_to_many_association.rb', line 50 def empty? entries.empty? end |
#find ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/data_mapper/associations/has_and_belongs_to_many_association.rb', line 54 def find return @results unless @results.nil? unless @instance.loaded_set.nil? # Temp variable for the instance variable name. instance_variable_name = "@#{foreign_key}".to_sym @results = @instance.session.find(@associated_class, :all, :id.select => { :table => @join_table_name, foreign_key.to_sym => @instance.key } ) do |animal_id, ref| @instance.load_set.find { |x| x.id == animal_id }.exhibits << ref end end return @results || (@results = []) end |
#foreign_key ⇒ Object
81 82 83 |
# File 'lib/data_mapper/associations/has_and_belongs_to_many_association.rb', line 81 def foreign_key @foreign_key || (@foreign_key = (@options[:foreign_key] || @instance.session.mappings[@instance.class].default_foreign_key)) end |
#inspect ⇒ Object
77 78 79 |
# File 'lib/data_mapper/associations/has_and_belongs_to_many_association.rb', line 77 def inspect @results.inspect end |
#set(results) ⇒ Object
73 74 75 |
# File 'lib/data_mapper/associations/has_and_belongs_to_many_association.rb', line 73 def set(results) @results = results end |
#size ⇒ Object Also known as: length
41 42 43 |
# File 'lib/data_mapper/associations/has_and_belongs_to_many_association.rb', line 41 def size entries.size end |