Class: BigRecord::BrAssociations::BelongsToManyAssociation

Inherits:
AssociationProxy show all
Defined in:
lib/big_record/br_associations/belongs_to_many_association.rb

Overview

:nodoc:

Instance Method Summary

Methods inherited from AssociationProxy

#===, #aliased_table_name, #initialize, #loaded, #loaded?, #proxy_owner, #proxy_reflection, #proxy_target, #reload, #respond_to?, #target, #target=

Constructor Details

This class inherits a constructor from BigRecord::BrAssociations::AssociationProxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BigRecord::BrAssociations::AssociationProxy

Instance Method Details

- (Object) <<(*records) Also known as: push

Add records to this association. Returns self so method calls may be chained. Since << flattens its argument list and inserts each record, push and concat behave identically.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 16

def <<(*records)
  result = true
  load_target

  flatten_deeper(records).each do |record|
    raise_on_type_mismatch(record)
    callback(:before_add, record)
    result &&= insert_record(record)
    @target << record
    callback(:after_add, record)
  end

  result && self
end

- (Object) build(attributes = {})



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 187

def build(attributes = {})
  if attributes.is_a?(Array)
    attributes.collect { |attr| build(attr) }
  else
    record = @reflection.klass.new(attributes)
    set_belongs_to_association_for(record)

    @target ||= [] unless loaded?
    @target << record

    record
  end
end

- (Object) clear

Removes all records from this association. Returns self so method calls may be chained.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 62

def clear
  return self if length.zero? # forces load_target if hasn't happened already

  if @reflection.options[:dependent] && @reflection.options[:dependent] == :delete_all
    destroy_all
  else
    delete_all
  end

  self
end

- (Object) count(*args)

Count the number of associated records. All arguments are optional.



202
203
204
205
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 202

def count(*args)
  load_target
  @target.size
end

- (Object) create(attributes = {})



83
84
85
86
87
88
89
90
91
92
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 83

def create(attributes = {})
  # Can't use Base.create since the foreign key may be a protected attribute.
  if attributes.is_a?(Array)
    attributes.collect { |attr| create(attr) }
  else
    record = build(attributes)
    record.save unless @owner.new_record?
    record
  end
end

- (Object) delete(*records)

Remove records from this association. Does not destroy records.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 47

def delete(*records)
  records = flatten_deeper(records)
  records.each { |record| raise_on_type_mismatch(record) }
  records.reject! { |record| @target.delete(record) if record.new_record? }
  return if records.empty?

  records.each { |record| callback(:before_remove, record) }
  delete_records(records)
  records.each do |record|
    @target.delete(record)
    callback(:after_remove, record)
  end
end

- (Object) destroy_all



74
75
76
77
78
79
80
81
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 74

def destroy_all
  # HbaseAdapter doesn't support transactions
#        @owner.transaction do
    each { |record| record.destroy }
#        end

  reset_target!
end

- (Boolean) empty?

Returns:

  • (Boolean)


107
108
109
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 107

def empty?
  size.zero?
end

- (Object) find(*args)

Raises:

  • (ArgumentError)


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 207

def find(*args)
  raise ArgumentError, "Only find(:all) is supported" unless args == [:all]
  return [] if @owner[@reflection.primary_key_name].blank?

  if @reflection.options[:cache]
    # Create the items proxies using the content that is cached
    records = []
    @owner[@reflection.primary_key_name].each do |id|
      records << CachedItemProxyFactory.instance.create(id, @owner, @reflection)
    end
    records
  else
    # Don't throw an exception when the records are not found
    records = []
    @owner[@reflection.primary_key_name].each do |id|
      begin
        records << @reflection.klass.find(id)
      rescue BigRecord::RecordNotFound
        # do nothing
      end
    end
    records
  end
end

- (Object) length

Returns the size of the collection by loading it and calling size on the array. If you want to use this method to check whether the collection is empty, use collection.length.zero? instead of collection.empty?



103
104
105
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 103

def length
  load_target.size
end

- (Object) replace(other_array)

Replace this collection with other_array This will perform a diff and delete/add only records that have changed.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 124

def replace(other_array)
  if other_array.nil?
    @target = @owner[@reflection.primary_key_name] = nil
  elsif other_array.empty?
    @target = @owner[@reflection.primary_key_name] = []
  else
    other_array.each { |val| raise_on_type_mismatch(val) }

    load_target
    other   = other_array.size < 100 ? other_array : other_array.to_set
    current = @target.size < 100 ? @target : @target.to_set

    delete(@target.select { |v| !other.include?(v) })
    concat(other_array.select { |v| !current.include?(v) })
    @updated = true
  end
  self
end

- (Object) reset



9
10
11
12
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 9

def reset
  @loaded = false
  reset_target!
end

- (Object) size

Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn’t been loaded and calling collection.size if it has. If it’s more likely than not that the collection does have a size larger than zero and you need to fetch that collection afterwards, it’ll take one less SELECT query if you use length.



97
98
99
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 97

def size
  @target.size
end

- (Object) to_ary



4
5
6
7
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 4

def to_ary
  load_target
  @target.to_ary
end

- (Object) uniq(collection = self)



111
112
113
114
115
116
117
118
119
120
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 111

def uniq(collection = self)
  seen = Set.new
  collection.inject([]) do |kept, record|
    unless seen.include?(record.id)
      kept << record
      seen << record.id
    end
    kept
  end
end

- (Boolean) updated?

Returns:

  • (Boolean)


143
144
145
# File 'lib/big_record/br_associations/belongs_to_many_association.rb', line 143

def updated?
  @updated
end