Class: CassandraObject::Associations::OneToManyAssociationProxy
- Inherits:
-
Object
- Object
- CassandraObject::Associations::OneToManyAssociationProxy
- Includes:
- Enumerable
- Defined in:
- lib/cassandra_object/associations/one_to_many.rb
Instance Method Summary collapse
- #<<(record) ⇒ Object
- #[](index) ⇒ Object
-
#all(options = {}) ⇒ Array<CassandraObject::Base>
Get the targets of this association proxy.
-
#create(attributes) ⇒ CassandraObject::Base
Create a record of the associated type with the supplied attributes and add it to this association.
- #create!(attributes) ⇒ Object
- #each ⇒ Object
-
#initialize(association, owner) ⇒ OneToManyAssociationProxy
constructor
A new instance of OneToManyAssociationProxy.
- #loaded? ⇒ Boolean
- #target ⇒ Object (also: #to_a)
Constructor Details
#initialize(association, owner) ⇒ OneToManyAssociationProxy
Returns a new instance of OneToManyAssociationProxy.
78 79 80 81 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 78 def initialize(association, owner) @association = association @owner = owner end |
Instance Method Details
#<<(record) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 94 def <<(record) @association.add(@owner, record) if loaded? @target << record end end |
#[](index) ⇒ Object
90 91 92 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 90 def [](index) to_a[index] end |
#all(options = {}) ⇒ Array<CassandraObject::Base>
Get the targets of this association proxy
109 110 111 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 109 def all( = {}) @association.find(@owner, ) end |
#create(attributes) ⇒ CassandraObject::Base
Create a record of the associated type with the supplied attributes and add it to this association
120 121 122 123 124 125 126 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 120 def create(attributes) @association.target_class.create(attributes).tap do |record| if record.valid? self << record end end end |
#create!(attributes) ⇒ Object
128 129 130 131 132 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 128 def create!(attributes) @association.target_class.create!(attributes).tap do |record| self << record end end |
#each ⇒ Object
84 85 86 87 88 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 84 def each target.each do |i| yield i end end |
#loaded? ⇒ Boolean
143 144 145 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 143 def loaded? defined?(@loaded) && @loaded end |
#target ⇒ Object Also known as: to_a
134 135 136 137 138 139 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 134 def target @target ||= begin @loaded = true @association.find(@owner) end end |