Class: CassandraObject::Associations::OneToManyAssociationProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cassandra_object/associations/one_to_many.rb

Instance Method Summary collapse

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

Parameters:

  • options (Hash) (defaults to: {})

    the options with which to modify this query

Options Hash (options):

  • :start_after (String)

    the key after which to start returning results

  • :reversed (Boolean) — default: false or association default

    return the results in reverse order

  • :limit (Integer)

    the max number of results to return

Returns:



107
108
109
# File 'lib/cassandra_object/associations/one_to_many.rb', line 107

def all(options = {})
  @association.find(@owner, options)
end

#create(attributes) ⇒ CassandraObject::Base

Create a record of the associated type with the supplied attributes and add it to this association

Parameters:

  • attributes (Hash)

    the attributes with which to create the object

Returns:



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

#eachObject



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

Returns:

  • (Boolean)


141
142
143
# File 'lib/cassandra_object/associations/one_to_many.rb', line 141

def loaded?
  defined?(@loaded) && @loaded
end

#targetObject 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