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.
76 77 78 79 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 76 def initialize(association, owner) @association = association @owner = owner end |
Instance Method Details
#<<(record) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 92 def <<(record) @association.add(@owner, record) if loaded? @target << record end end |
#[](index) ⇒ Object
88 89 90 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 88 def [](index) to_a[index] end |
#all(options = {}) ⇒ Array<CassandraObject::Base>
Get the targets of this association proxy
107 108 109 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 107 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
118 119 120 121 122 123 124 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 118 def create(attributes) @association.target_class.create(attributes).tap do |record| if record.valid? self << record end end end |
#create!(attributes) ⇒ Object
126 127 128 129 130 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 126 def create!(attributes) @association.target_class.create!(attributes).tap do |record| self << record end end |
#each ⇒ Object
82 83 84 85 86 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 82 def each target.each do |i| yield i end end |
#loaded? ⇒ Boolean
141 142 143 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 141 def loaded? defined?(@loaded) && @loaded end |
#target ⇒ Object Also known as: to_a
132 133 134 135 136 137 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 132 def target @target ||= begin @loaded = true @association.find(@owner) end end |