Module: Cequel::Record::Persistence

Extended by:
ActiveSupport::Concern, Forwardable
Defined in:
lib/cequel/record/persistence.rb

Overview

This module provides functionality for loading and saving records to the Cassandra database.

See Also:

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroy(options = {}) ⇒ Record

Remove this record from the database

Parameters:

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

    options for deletion

Options Hash (options):

  • :consistency (Symbol) — default: :quorum

    what consistency with which to persist the deletion

  • :timestamp (Time)

    the writetime to use for the deletion

Returns:

Since:

  • 0.1.0



219
220
221
222
223
224
225
# File 'lib/cequel/record/persistence.rb', line 219

def destroy(options = {})
  options.assert_valid_keys(:consistency, :timestamp)
  assert_keys_present!
  metal_scope.delete(options)
  transient!
  self
end

#exists?Boolean Also known as: exist?

Check if an unloaded record exists in the database

Returns:

  • (Boolean)

    ‘true` if the record has a corresponding row in the database

Since:

  • 1.0.0



106
107
108
109
110
111
# File 'lib/cequel/record/persistence.rb', line 106

def exists?
  load!
  true
rescue RecordNotFound
  false
end

#hydrate(row) ⇒ Object

Since:

  • 0.1.0



255
256
257
258
259
# File 'lib/cequel/record/persistence.rb', line 255

def hydrate(row)
  init_attributes(row)
  hydrated!
  self
end

#key_attributesHash

Returns the attributes of this record that make up the primary key.

Examples:

post = Post.new
post.blog_subdomain = 'cassandra'
post.permalink = 'cequel'
post.title = 'Cequel: The Next Generation'
post.key_attributes
  #=> {:blog_subdomain=>'cassandra', :permalink=>'cequel'}

Returns:

  • (Hash)

    the attributes of this record that make up the primary key

Since:

  • 1.0.0



83
84
85
# File 'lib/cequel/record/persistence.rb', line 83

def key_attributes
  @attributes.slice(*self.class.key_column_names)
end

#key_valuesArray Also known as: to_key

Returns the values of the primary key columns for this record.

Returns:

  • (Array)

    the values of the primary key columns for this record

See Also:

Since:

  • 1.0.0



93
94
95
# File 'lib/cequel/record/persistence.rb', line 93

def key_values
  key_attributes.values
end

#loadRecord

Load an unloaded record’s row from the database and hydrate the record’s attributes

Returns:

Since:

  • 1.0.0



122
123
124
125
126
# File 'lib/cequel/record/persistence.rb', line 122

def load
  assert_keys_present!
  record_collection.load! unless loaded?
  self
end

#load!Record

Attempt to load an unloaded record and raise an error if the record does not correspond to a row in the database

Returns:

Raises:

See Also:

Since:

  • 1.0.0



138
139
140
141
142
143
144
145
146
# File 'lib/cequel/record/persistence.rb', line 138

def load!
  load.tap do
    if transient?
      fail RecordNotFound,
           "Couldn't find #{self.class.name} with " \
           "#{key_attributes.inspect}"
    end
  end
end

#loaded?Boolean #loaded?(column) ⇒ Boolean

Overloads:

  • #loaded?Boolean

    Returns true if this record’s attributes have been loaded from the database.

    Returns:

    • (Boolean)

      true if this record’s attributes have been loaded from the database

  • #loaded?(column) ⇒ Boolean

    Returns true if the named column is loaded in memory.

    Parameters:

    • column (Symbol)

      name of column to check if loaded

    Returns:

    • (Boolean)

      true if the named column is loaded in memory

Returns:

  • (Boolean)

Since:

  • 1.0.0



161
162
163
# File 'lib/cequel/record/persistence.rb', line 161

def loaded?(column = nil)
  !!@loaded && (column.nil? || @attributes.key?(column.to_sym))
end

#new_record?Boolean

Returns true if this is a new, unsaved record.

Returns:

  • (Boolean)

    true if this is a new, unsaved record

Since:

  • 1.0.0



232
233
234
# File 'lib/cequel/record/persistence.rb', line 232

def new_record?
  !!@new_record
end

#persisted?Boolean

Returns true if this record is persisted in the database.

Returns:

  • (Boolean)

    true if this record is persisted in the database

See Also:

Since:

  • 0.1.0



241
242
243
# File 'lib/cequel/record/persistence.rb', line 241

def persisted?
  !!@persisted
end

#save(options = {}) ⇒ Boolean

Persist the record to the database. If this is a new record, it will be saved using an INSERT statement. If it is an existing record, it will be persisted using a series of ‘UPDATE` and `DELETE` statements which will persist all changes to the database, including atomic collection modifications.

Parameters:

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

    options for save

Options Hash (options):

  • :validate (Boolean) — default: true

    whether to run validations before saving

  • :consistency (Symbol) — default: :quorum

    what consistency with which to persist the changes

  • :ttl (Integer)

    time-to-live of the updated rows in seconds

  • :timestamp (Time)

    the writetime to use for the column updates

Returns:

  • (Boolean)

    true if record saved successfully, false if invalid

See Also:

Since:

  • 0.1.0



185
186
187
188
189
190
191
192
# File 'lib/cequel/record/persistence.rb', line 185

def save(options = {})
  options.assert_valid_keys(:consistency, :ttl, :timestamp)
  if new_record? then create(options)
  else update(options)
  end
  @new_record = false
  true
end

#transient?Boolean

Returns true if this record is not persisted in the database.

Returns:

  • (Boolean)

    true if this record is not persisted in the database

See Also:

Since:

  • 0.1.0



250
251
252
# File 'lib/cequel/record/persistence.rb', line 250

def transient?
  !persisted?
end

#update_attributes(attributes) ⇒ Boolean

Set attributes and save the record

Parameters:

  • attributes (Hash)

    hash of attributes to update

Returns:

  • (Boolean)

    true if saved successfully

See Also:

Since:

  • 0.1.0



204
205
206
207
# File 'lib/cequel/record/persistence.rb', line 204

def update_attributes(attributes)
  self.attributes = attributes
  save
end