class OceanDynamo::Associations::CollectionProxy
Collection proxies in OceanDynamo
are middlemen between the object that holds the association, known as the @owner
, and the actual associated object, known as the @target
. The kind of association any proxy is about is available in @reflection
. That's an instance of the class OceanDynamo::Reflection::AssociationReflection.
For example, given
class Blog < OceanDynamo::Table has_many :posts end blog = Blog.first
the collection proxy in blog.posts
has the object in blog
as @owner
, the collection of its posts as @target
, and the @reflection
object represents a :has_many
macro.
This class delegates unknown methods to @target
through explicit proxy methods for each separate operation.
The @target
object is not loaded until needed. As it turns out, the key to this lazy loading scheme is to_ary
.
Inheritance chain:
Relation (@klass, @loaded) CollectionProxy (@association)
Public Class Methods
new(klass, association)
click to toggle source
Calls superclass method
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 40 def initialize(klass, association) @association = association super klass end
Public Instance Methods
<<(*records)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 190 def <<(*records) proxy_association.concat(records) && self end
==(other)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 179 def ==(other) load_target == other end
any?(&block)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 164 def any?(&block) @association.any?(&block) end
build(attributes = {}, &block)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 86 def build(attributes = {}, &block) @association.build(attributes, &block) end
Also aliased as: new
clear()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 117 def clear delete_all self end
concat(*records)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 102 def concat(*records) @association.concat(*records) end
count(column_name = nil, options = {})
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 144 def count(column_name = nil, options = {}) @association.count(column_name, options) end
create(attributes = {}, &block)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 92 def create(attributes = {}, &block) @association.create(attributes, &block) end
create!(attributes = {}, &block)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 97 def create!(attributes = {}, &block) @association.create!(attributes, &block) end
delete(*records)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 128 def delete(*records) @association.delete(*records) end
delete_all()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 112 def delete_all @association.delete_all end
destroy(*records)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 133 def destroy(*records) @association.destroy(*records) end
destroy_all()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 123 def destroy_all @association.destroy_all end
distinct()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 138 def distinct @association.distinct end
Also aliased as: uniq
empty?()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 159 def empty? @association.empty? end
find(*args, &block)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 71 def find(*args, &block) @association.find(*args, &block) end
first(*args)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 76 def first(*args) @association.first(*args) end
include?(record)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 174 def include?(record) @association.include?(record) end
last(*args)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 81 def last(*args) @association.last(*args) end
length()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 154 def length @association.length end
load_target()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 56 def load_target @association.load_target end
loaded?()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 61 def loaded? @association.loaded? end
many?(&block)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 169 def many?(&block) @association.many?(&block) end
prepend(*args)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 197 def prepend(*args) raise NoMethodError, "prepend on association is not defined. Please use << or append" end
proxy_association()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 46 def proxy_association @association end
reload()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 202 def reload proxy_association.reload self end
replace(other_array)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 107 def replace(other_array) @association.replace(other_array) end
select(select = nil, &block)
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 66 def select(select = nil, &block) @association.select(select, &block) end
size()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 149 def size @association.size end
target()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 51 def target @association.target end
to_ary()
click to toggle source
# File lib/ocean-dynamo/associations/collection_proxy.rb, line 184 def to_ary load_target.dup end
Also aliased as: to_a