Module: Cql::Model::InstanceMethods

Defined in:
lib/cql/model/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object

Read a property. Property names are column names, and can therefore take any data type that a column name can take (integer, UUID, etc).

Parameters:

  • name (Object)

Returns:

  • (Object)

    the value of the specified column, or nil



16
17
18
# File 'lib/cql/model/instance_methods.rb', line 16

def [](name)
  @cql_properties[name]
end

#initialize(cql_properties = nil) ⇒ Object

Instantiate a new instance of this model. Do not validate the contents of cql_properties; it may contain properties that aren’t declared by this model, that have a missing CQL column, or an improper name/value for their column type.

Parameters:

  • cql_properties (Hash) (defaults to: nil)

    typed hash of all properties associated with this model



7
8
9
# File 'lib/cql/model/instance_methods.rb', line 7

def initialize(cql_properties=nil)
  @cql_properties = cql_properties || {}
end

#update(values = {}) ⇒ Cql::Model::Query::InsertStatement

Start an INSERT CQL statement to update model

Examples:

joe.update(:age => 35).execute
joe.update.ttl(3600).execute
joe.update(:age => 36).ttl(7200).consistency('ONE').execute

Parameters:

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

    Hash of column values indexed by column name, optional

Returns:

See Also:



30
31
32
33
# File 'lib/cql/model/instance_methods.rb', line 30

def update(values={})
  key_vals = self.class.primary_key.inject({}) { |h, k| h[k] = @cql_properties[k]; h }
  Cql::Model::Query::UpdateStatement.new(self.class).update(values.merge(key_vals))
end

#update_all_by(key, values = {}) ⇒ Cql::Model::Query::UpdateStatement

Start an UPDATE CQL statement to update all models with given key This can update multiple models if the key is part of a composite key Updating all models with given (different) key values can be done using the ‘.update’ class method

Examples:

joe.update_all_by(:name, :age => 25).execute # Set all joe's age to 25
joe.update_all_by(:name).ttl(3600).execute # Set all joe's TTL to one hour

Parameters:

  • key (Symbol|String)

    Name of key used to select models to be updated

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

    Hash of column values indexed by column names, optional

Returns:

See Also:



47
48
49
# File 'lib/cql/model/instance_methods.rb', line 47

def update_all_by(key, values={})
  Cql::Model::Query::UpdateStatement.new(self.class).update(values.merge({ key => @cql_properties[key.to_s] }))
end