Class: BigRecord::BrAssociations::AssociationCollection
- Inherits:
-
AssociationProxy
- Object
- AssociationProxy
- BigRecord::BrAssociations::AssociationCollection
- Defined in:
- lib/big_record/br_associations/association_collection.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Method Summary
-
- (Object) <<(*records)
(also: #push)
Add records to this association.
-
- (Object) clear
Removes all records from this association.
- - (Object) create(attributes = {})
-
- (Object) delete(*records)
Remove records from this association.
- - (Object) destroy_all
- - (Boolean) empty?
-
- (Object) length
Returns the size of the collection by loading it and calling size on the array.
-
- (Object) replace(other_array)
Replace this collection with other_array This will perform a diff and delete/add only records that have changed.
- - (Object) reset
-
- (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.
-
- (Object) sum(*args, &block)
Calculate sum using SQL, not Enumerable.
- - (Object) to_ary
- - (Object) uniq(collection = self)
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.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/big_record/br_associations/association_collection.rb', line 18 def <<(*records) result = true load_target # HbaseAdapter doesn't support transactions @owner.transaction do flatten_deeper(records).each do |record| raise_on_type_mismatch(record) callback(:before_add, record) result &&= insert_record(record) unless @owner.new_record? @target << record callback(:after_add, record) end end result && self end |
- (Object) clear
Removes all records from this association. Returns self so method calls may be chained.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/big_record/br_associations/association_collection.rb', line 70 def clear return self if length.zero? # forces load_target if hasn't happened already if @reflection.[:dependent] && @reflection.[:dependent] == :delete_all destroy_all else delete_all end self end |
- (Object) create(attributes = {})
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/big_record/br_associations/association_collection.rb', line 91 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.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/big_record/br_associations/association_collection.rb', line 52 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? # HbaseAdapter doesn't support transactions @owner.transaction do records.each { |record| callback(:before_remove, record) } delete_records(records) records.each do |record| @target.delete(record) callback(:after_remove, record) end end end |
- (Object) destroy_all
82 83 84 85 86 87 88 89 |
# File 'lib/big_record/br_associations/association_collection.rb', line 82 def destroy_all # HbaseAdapter doesn't support transactions @owner.transaction do each { |record| record.destroy } end reset_target! end |
- (Boolean) empty?
122 123 124 |
# File 'lib/big_record/br_associations/association_collection.rb', line 122 def empty? size.zero? 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?
118 119 120 |
# File 'lib/big_record/br_associations/association_collection.rb', line 118 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.
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/big_record/br_associations/association_collection.rb', line 139 def replace(other_array) 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 # HbaseAdapter doesn't support transactions @owner.transaction do delete(@target.select { |v| !other.include?(v) }) concat(other_array.select { |v| !current.include?(v) }) end end |
- (Object) reset
11 12 13 14 |
# File 'lib/big_record/br_associations/association_collection.rb', line 11 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.
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/big_record/br_associations/association_collection.rb', line 105 def size if loaded? && !@reflection.[:uniq] @target.size elsif !loaded? && !@reflection.[:uniq] && @target.is_a?(Array) unsaved_records = Array(@target.detect { |r| r.new_record? }) unsaved_records.size + count_records else count_records end end |
- (Object) sum(*args, &block)
Calculate sum using SQL, not Enumerable
47 48 49 |
# File 'lib/big_record/br_associations/association_collection.rb', line 47 def sum(*args, &block) calculate(:sum, *args, &block) end |
- (Object) to_ary
6 7 8 9 |
# File 'lib/big_record/br_associations/association_collection.rb', line 6 def to_ary load_target @target.to_ary end |
- (Object) uniq(collection = self)
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/big_record/br_associations/association_collection.rb', line 126 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 |