class Cequel::Record::OrmAdapter
ORM adapter for Cequel
, the Ruby ORM for Cassandra
Public Instance Methods
@return [Array<Symbol>] names of columns on this model
# File lib/cequel/record/orm_adapter.rb, line 19 def column_names klass.columns.map { |column| column.name } end
Destroy the given record
@param record [Cequel::Record] the record to destroy @return [void]
# File lib/cequel/record/orm_adapter.rb, line 95 def destroy(record) record.destroy end
Find all records with the given conditions and limit
@param options [Options] options for query @option options [Hash] :conditions map of column names to column
values. This can either be a one-element hash specifying a secondary index lookup, or a mapping of primary key columns to values.
@option options [Integer] :limit maximum number of rows to return @return [Array<Cequel::Record>] all records matching the conditions
# File lib/cequel/record/orm_adapter.rb, line 74 def find_all(options = {}) construct_scope(options).to_a end
Find the first record instance with the given conditions
@param options [Options] options for query @option options [Hash] :conditions map of column names to column
values. This can either be a one-element hash specifying a secondary index lookup, or a mapping of primary key columns to values.
@return [Cequel::Record] the first record matching the query, or nil if
none present
# File lib/cequel/record/orm_adapter.rb, line 60 def find_first(options = {}) construct_scope(options).first end
@param key_values [Array] values for each primary key column on this
record
@return [Cequel::Record] a record instance corresponding to this
primary key or nil if not present
@see get
# File lib/cequel/record/orm_adapter.rb, line 44 def get(key_values) get!(key_values) rescue Cequel::Record::RecordNotFound nil end
@param key_values [Array] values for each primary key column on this
record
@return [Cequel::Record] a record instance corresponding to this
primary key
@raise [Cequel::Record::RecordNotFound] if the key is not present
@see get
# File lib/cequel/record/orm_adapter.rb, line 32 def get!(key_values) klass.find(*key_values) end
Private Instance Methods
# File lib/cequel/record/orm_adapter.rb, line 124 def apply_primary_key_scope(scope, conditions) conditions = conditions.dup conditions.assert_valid_keys(*klass.key_column_names) klass.key_column_names.each do |column_name| column_value = conditions.delete(column_name) break unless column_value scope = scope[column_value] end assert_conditions_empty!(conditions) scope end
# File lib/cequel/record/orm_adapter.rb, line 139 def apply_secondary_index_scope(scope, conditions) return unless conditions.one? condition_column = klass.reflect_on_column(conditions.keys.first) if condition_column.data_column? && condition_column.indexed? scope.where(*conditions.first) end end
# File lib/cequel/record/orm_adapter.rb, line 147 def assert_conditions_empty!(conditions) unless conditions.empty? fail ArgumentError, "Invalid columns for conditions: #{conditions.keys.join(', ')}" end end
# File lib/cequel/record/orm_adapter.rb, line 101 def construct_scope(options) conditions, _, limit, _ = extract_conditions!(options.deep_symbolize_keys) scope = klass.all scope = apply_secondary_index_scope(scope, conditions) || apply_primary_key_scope(scope, conditions) scope = scope.limit(limit) if limit scope end
# File lib/cequel/record/orm_adapter.rb, line 111 def extract_conditions!(options) super.tap do |_, order, _, offset| if order.present? fail ArgumentError, "Cassandra does not support ordering of results by " \ "arbitrary columns" end if offset.present? fail ArgumentError, 'Cassandra does not support row offsets' end end end