module Cql::Model::ClassMethods

Public Class Methods

extended(klass) click to toggle source
# File lib/cql/model/class_methods.rb, line 2
def self.extended(klass)
  klass.instance_eval do
    # The mutex is shared by all Cql::Model inheritors
    @@cql_model_mutex            ||= Mutex.new

    # Other attributes are tracked per-class
    @cql_table_name              ||= klass.name.split('::').last
    @cql_model_properties        ||= {}
    @cql_model_keys              ||= []
    @cql_model_read_consistency  ||= 'LOCAL_QUORUM'
    @cql_model_write_consistency ||= 'LOCAL_QUORUM'
  end
end

Public Instance Methods

cql_client(new_client=nil) click to toggle source

Get or set the client connection used by this class.

@param [optional, Cql::Client] new_client the new client to set @return [Cql::Client] the current client

# File lib/cql/model/class_methods.rb, line 20
def cql_client(new_client=nil)
  if new_client
    @@cql_model_mutex.synchronize do
      @cql_client = new_client
    end
  end

  @cql_client || ::Cql::Model.cql_client
end
create(values)
Alias for: insert
each(&block) click to toggle source

@TODO docs

# File lib/cql/model/class_methods.rb, line 175
def each(&block)
  Cql::Model::Query::SelectStatement.new(self).each(&block)
end
each_row(&block) click to toggle source

@TODO docs

# File lib/cql/model/class_methods.rb, line 170
def each_row(&block)
  Cql::Model::Query::SelectStatement.new(self).each_row(&block)
end
insert(values) click to toggle source

Begin building a CQL INSERT statement. @see Cql::Model::Query::InsertStatement

@param [Hash] values Hash of column values indexed by column name @return [Cql::Model::Query::InsertStatement] a query object to customize (timestamp, ttl, etc) or execute

@example

Person.create(:name => 'Joe', :age => 25).ttl(3600).execute
# File lib/cql/model/class_methods.rb, line 149
def insert(values)
  Cql::Model::Query::InsertStatement.new(self).insert(values)
end
Also aliased as: create
primary_key(*keys) click to toggle source

Specify or get a primary key or a composite primary key

@param key_vals [Symbol|Array<Symbol>] single key name or composite key names

@return [Cql::Model] self

# File lib/cql/model/class_methods.rb, line 66
def primary_key(*keys)
  if keys.empty?
    @cql_model_keys
  else
    @@cql_model_mutex.synchronize do
      @cql_model_keys = keys
    end
    self
  end
end
property(name, type, opts={}) click to toggle source

@TODO docs

# File lib/cql/model/class_methods.rb, line 78
def property(name, type, opts={})
  definition = {}

  # If the user specified the name as a symbol, then they automatically get
  # a reader and writer because the property has a predictable, fixed column
  # name.
  if name.is_a?(Symbol)
    definition[:reader] = opts[:reader] || name
    definition[:writer] = opts[:writer] || "#{definition[:reader]}=".to_sym
    name                = name.to_s
  end

  @@cql_model_mutex.synchronize do
    definition[:type] = type

    if @cql_model_properties.key?(name) && (@cql_model_properties[name] != definition)
      raise ArgumentError, "Property #{name} is already defined"
    end

    unless @cql_model_properties.key?(name)
      @cql_model_properties[name] = definition

      __send__(:define_method, definition[:reader]) do
        self[name]
      end if definition[:reader]

      __send__(:define_method, definition[:writer]) do |value|
        self[name] = value
      end if definition[:writer]
    end
  end

  self
end
read_consistency(new_consistency=nil) click to toggle source

@TODO docs

# File lib/cql/model/class_methods.rb, line 44
def read_consistency(new_consistency=nil)
  if new_consistency
    @cql_model_read_consistency = new_consistency
  else
    @cql_model_read_consistency
  end
end
scope(name, &block) click to toggle source

@TODO docs

# File lib/cql/model/class_methods.rb, line 114
def scope(name, &block)
  @@cql_model_mutex.synchronize do
    eigenclass = class <<self
      self
    end

    eigenclass.instance_eval do
      define_method(name.to_sym) do |*params|
        # @TODO use a prepared statement for speed
        self.select.where(*params, &block)
      end
    end
  end

  self
end
select(*params) click to toggle source

Begin building a CQL SELECT statement.

@param [Object] *params list of yield parameters for the block

@example tell us how old Joe is

Person.select.where { name == 'Joe' }.each { |person| puts person.age }
# File lib/cql/model/class_methods.rb, line 137
def select(*params)
  Cql::Model::Query::SelectStatement.new(self).select(*params)
end
table_name(new_name=nil) click to toggle source

@TODO docs

# File lib/cql/model/class_methods.rb, line 31
def table_name(new_name=nil)
  if new_name
    @@cql_model_mutex.synchronize do
      # Set the table name
      @cql_table_name = new_name
    end
  else
    # Get the table name
    @cql_table_name
  end
end
update(values={}) click to toggle source

Start an UPDATE CQL statement The method keys must be called on the result before execute @see Cql::Model::Query::UpdateStatement

@param [Hash] values Hash of column values indexed by column name, optional @return [Cql::Model::Query::UpdateStatement] a query object to customize (keys, ttl, timestamp etc) then execute

@example

Person.update(:updated_at => Time.now.utc).keys(:name => ['joe', 'john', 'jane'])
Person.update.ttl(3600).keys(:name => 'joe')
# File lib/cql/model/class_methods.rb, line 165
def update(values={})
  Cql::Model::Query::UpdateStatement.new(self).update(values)
end
write_consistency(new_consistency=nil) click to toggle source

@TODO docs

# File lib/cql/model/class_methods.rb, line 53
def write_consistency(new_consistency=nil)
  if new_consistency
    @cql_model_write_consistency = new_consistency
  else
    @cql_model_write_consistency
  end
end