class Believer::Column

Represents a Cassandra table column

Constants

CQL_TYPES
RUBY_TYPES

Supported Ruby ‘types’

Attributes

cql_type[R]
element_type[R]
key_type[R]
name[R]
ruby_type[R]
value_type[R]

Public Class Methods

new(opts) click to toggle source

Creates a new instance. @param opts [Hash] values options @option opts :name the column name @option opts :type the Ruby type. Can be :integer, :string, :time, :timestamp, :float @option opts :cql_type the CQL type. See Cassandra CQL documentation and {#CQL_TYPES} for supported types

# File lib/believer/column.rb, line 61
def initialize(opts)
  raise "Must specify either a :type and/or a :cql_type" if opts[:type].nil? && opts[:cql_type].nil?
  raise "Invalid CQL column type #{opts[:cql_type]}" if opts[:cql_type] && !CQL_TYPES.has_key?(opts[:cql_type])
  raise "Invalid type #{opts[:type]}" unless RUBY_TYPES.has_key?(opts[:type])

  @name = opts[:name]
  @ruby_type = opts[:type].nil? ? CQL_TYPES[opts[:cql_type]][:ruby_type] : opts[:type]
  @cql_type = opts[:cql_type].nil? ? RUBY_TYPES[opts[:type]][:default_cql_type] : opts[:cql_type]

  @element_type = opts[:element_type]
  @key_type = opts[:key_type]
  @value_type = opts[:value_type]

  @default_value = opts[:default_value]

  @apply_cql_result_row_conversion = opts[:apply_cql_result_row_conversion] || RUBY_TYPES[@ruby_type][:apply_cql_result_row_conversion]
end

Public Instance Methods

apply_cql_result_row_conversion?() click to toggle source
# File lib/believer/column.rb, line 119
def apply_cql_result_row_conversion?
  @apply_cql_result_row_conversion == true
end
convert_to_type(v) click to toggle source

Converts the value to a one that conforms to the type of this column @param v [Object] the value

# File lib/believer/column.rb, line 81
def convert_to_type(v)
  # TODO: kind of a dirty hack, this...
  case @ruby_type
    when :array
      return convert_to_array(v, element_type)
    when :set
      return convert_to_set(v, element_type)
    when :hash
      return convert_to_hash(v, key_type, value_type)
  end
  convert_value_to_type(v, @ruby_type)
end
default_value() click to toggle source
# File lib/believer/column.rb, line 110
def default_value
  def_val = @default_value || RUBY_TYPES[ruby_type][:default_value]
  unless def_val.nil?
    return def_val.call if def_val.is_a?(Proc)
    return def_val
  end
  nil
end
has_default_value?() click to toggle source
# File lib/believer/column.rb, line 106
def has_default_value?
  (@default_value != nil) || RUBY_TYPES[ruby_type][:default_value] != nil
end
to_cql() click to toggle source
# File lib/believer/column.rb, line 94
def to_cql
  col_stmt = "#{name} #{cql_type}"
  if cql_type == :list
    col_stmt << "<#{to_cql_type(element_type)}>"
  elsif cql_type == :set
    col_stmt << "<#{to_cql_type(element_type)}>"
  elsif cql_type == :map
    col_stmt << "<#{to_cql_type(key_type)},#{to_cql_type(value_type)}>"
  end
  col_stmt
end

Private Instance Methods

to_cql_type(t) click to toggle source
# File lib/believer/column.rb, line 124
def to_cql_type(t)
  return t if CQL_TYPES.has_key?(t)
  return RUBY_TYPES[t][:default_cql_type] if RUBY_TYPES.has_key?(t)
  nil
end
to_ruby_type(t) click to toggle source
# File lib/believer/column.rb, line 130
def to_ruby_type(t)
  return t if RUBY_TYPES.has_key?(t)
  return CQL_TYPES[t][:ruby_type] if CQL_TYPES.has_key?(t)
  nil
end