Class: MassiveRecord::ORM::Relations::Proxy::ReferencesMany

Inherits:
MassiveRecord::ORM::Relations::ProxyCollection show all
Defined in:
lib/massive_record/orm/relations/proxy/references_many.rb

Defined Under Namespace

Classes: UnsupportedFinderOption

Instance Attribute Summary

Attributes inherited from MassiveRecord::ORM::Relations::Proxy

#metadata, #proxy_owner, #proxy_target

Instance Method Summary collapse

Methods inherited from MassiveRecord::ORM::Relations::ProxyCollection

#delete, #delete_all, #destroy, #destroy_all, #empty?, #first, #load_proxy_target, #replace, #reset

Methods inherited from MassiveRecord::ORM::Relations::Proxy

#blank?, #initialize, #inspect, #load_proxy_target, #loaded!, #loaded?, #method_missing, #reload, #replace, #reset, #respond_to?, #to_param

Constructor Details

This class inherits a constructor from MassiveRecord::ORM::Relations::Proxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MassiveRecord::ORM::Relations::Proxy

Instance Method Details

#<<(*records) ⇒ Object Also known as: push, concat

Adding record(s) to the collection.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/massive_record/orm/relations/proxy/references_many.rb', line 25

def <<(*records)
  save_records = proxy_owner.persisted?

  if records.flatten.all? &:valid?
    records.flatten.each do |record|
      unless include? record
        raise_if_type_mismatch(record)
        add_foreign_key_in_proxy_owner(record.id)
        proxy_target << record
        record.save if save_records
      end
    end

    proxy_owner.save if save_records

    self
  end
end

#all(options = {}) ⇒ Object



103
104
105
106
107
# File 'lib/massive_record/orm/relations/proxy/references_many.rb', line 103

def all(options = {})
  options = MassiveRecord::Adapters::Thrift::Table.warn_and_change_deprecated_finder_options(options)

  load_proxy_target(options)
end

#any?Boolean Also known as: present?

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/massive_record/orm/relations/proxy/references_many.rb', line 77

def any?
  if !loaded? && find_with_proc?
    !!first
  else
    !empty?
  end
end

#find(id) ⇒ Object

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/massive_record/orm/relations/proxy/references_many.rb', line 87

def find(id)
  if loaded? || proxy_owner.new_record?
    record = proxy_target.find { |record| record.id == id }
  elsif find_with_proc?
    if id.starts_with? proxy_owner.send(.records_starts_from)
      record = proxy_target_class.find(id)
    end
  elsif foreign_key_in_proxy_owner_exists?(id)
    record = proxy_target_class.find(id)
  end

  raise RecordNotFound.new("Could not find #{proxy_target_class.model_name} with id=#{id}") if record.nil?

  record
end

#find_each(options = {}) ⇒ Object

Fetches records in batches of 1000 (by default), iterates over each batch and yields one and one record in to given block. See find_in_batches for options.



144
145
146
147
148
# File 'lib/massive_record/orm/relations/proxy/references_many.rb', line 144

def find_each(options = {})
  find_in_batches(options) do |batch|
    batch.each { |record| yield record }
  end
end

#find_in_batches(options = {}, &block) ⇒ Object

Find records in batches, yields batch into your block

Options:

<tt>:batch_size</tt>    The number of records you want per batch. Defaults to 1000
<tt>:starts_with</tt>         The ids starts with this


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/massive_record/orm/relations/proxy/references_many.rb', line 116

def find_in_batches(options = {}, &block)
  options = MassiveRecord::Adapters::Thrift::Table.warn_and_change_deprecated_finder_options(options)

  options[:batch_size] ||= 1000

  if loaded?
    collection =  if options[:starts_with]
                    proxy_target.select { |r| r.id.starts_with? options[:starts_with] }
                  else
                    proxy_target
                  end
    collection.in_groups_of(options[:batch_size], false, &block)
  elsif find_with_proc?
    find_proxy_target_with_proc(options.merge(:finder_method => :find_in_batches), &block)
  else
    all_ids = proxy_owner.send(.foreign_key)
    all_ids = all_ids.select { |id| id.starts_with? options[:starts_with] } if options[:starts_with]
    all_ids.in_groups_of(options[:batch_size]).each do |ids_in_batch|
      yield Array(find_proxy_target(:ids => ids_in_batch))
    end
  end
end

#include?(record_or_id) ⇒ Boolean

Checks if record is included in collection

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/massive_record/orm/relations/proxy/references_many.rb', line 50

def include?(record_or_id)
  id = record_or_id.respond_to?(:id) ? record_or_id.id : record_or_id

  if loaded? || find_with_proc?
    !!find(id)
  else
    foreign_key_in_proxy_owner_exists? id
  end
rescue RecordNotFound
  false
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/massive_record/orm/relations/proxy/references_many.rb', line 170

def is_a?(klass)
  klass == Array
end

#lengthObject Also known as: count, size

Returns the length of targes



65
66
67
68
69
70
71
72
73
# File 'lib/massive_record/orm/relations/proxy/references_many.rb', line 65

def length
  if loaded?
    proxy_target.length
  elsif find_with_proc?
    load_proxy_target.length
  else
    foreign_keys_in_proxy_owner.length
  end
end

#limit(limit) ⇒ Object

Returns a limited result set of target records.

TODO If we know all our foreign keys (basically we also know our length)

we can then mark our self as loaded if limit is equal to or greater
than foreign keys length.


157
158
159
160
161
162
163
164
165
166
167
# File 'lib/massive_record/orm/relations/proxy/references_many.rb', line 157

def limit(limit)
  if loaded? || proxy_owner.new_record?
    proxy_target.slice(0, limit)
  elsif find_with_proc?
    find_proxy_target_with_proc(:limit => limit)
  else
    ids = proxy_owner.send(.foreign_key).slice(0, limit)
    ids = ids.first if ids.length == 1
    Array(find_proxy_target(:ids => ids))
  end
end