Class: Believer::Column

Inherits:
Object
  • Object
show all
Includes:
Values
Defined in:
lib/believer/column.rb

Overview

Represents a Cassandra table column

Constant Summary collapse

CQL_TYPES =
{
    :ascii => {:ruby_type => :string}, # strings US-ASCII character string
    :bigint => {:ruby_type => :integer}, # integers 64-bit signed long
    :blob => {:ruby_type => :string}, # blobs Arbitrary bytes (no validation), expressed as hexadecimal
    :boolean => {:ruby_type => :boolean}, # booleans true or false
    :counter => {:ruby_type => :counter}, # integers Distributed counter value (64-bit long)
    :decimal => {:ruby_type => :float}, # integers, floats Variable-precision decimal
    :double => {:ruby_type => :float}, # integers 64-bit IEEE-754 floating point
    :float => {:ruby_type => :float}, # integers, floats 32-bit IEEE-754 floating point
    :inet => {:ruby_type => :string}, # strings IP address string in IPv4 or IPv6 format*
    :int => {:ruby_type => :integer}, # integers 32-bit signed integer
    :list => {:ruby_type => :array}, # n/a A collection of one or more ordered elements
    :map => {:ruby_type => :hash}, # n/a A JSON-style array of literals: { literal : literal, literal : literal ... }
    :set => {:ruby_type => :set}, # n/a A collection of one or more elements
    :text => {:ruby_type => :string}, # strings UTF-8 encoded string
    :timestamp => {:ruby_type => :time}, # integers, strings Date plus time, encoded as 8 bytes since epoch
    :uuid => {:ruby_type => :string}, # uuids A UUID in standard UUID format
    :timeuuid => {:ruby_type => :integer}, # uuids Type 1 UUID only (CQL 3)
    :varchar => {:ruby_type => :string}, # strings UTF-8 encoded string
    :varint => {:ruby_type => :integer} # integers Arbitrary-precision integer
}
RUBY_TYPES =

Supported Ruby ‘types’

{
    :symbol => {:default_cql_type => :varchar},
    :integer => {:default_cql_type => :int},
    :string => {:default_cql_type => :varchar},
    :time => {:default_cql_type => :timestamp},
    :timestamp => {:default_cql_type => :timestamp},
    :float => {:default_cql_type => :float},
    :array => {:default_cql_type => :list},
    :set => {:default_cql_type => :set, :apply_cql_result_row_conversion => true},
    :hash => {:default_cql_type => :map},
    :counter => {
        :default_cql_type => :counter,
        :default_value => lambda { Counter.new },
        :apply_cql_result_row_conversion => true
    }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Values

#convert_collection_elements, #convert_method, #convert_to_array, #convert_to_boolean, #convert_to_counter, #convert_to_float, #convert_to_hash, #convert_to_integer, #convert_to_set, #convert_to_string, #convert_to_symbol, #convert_to_time, #convert_value_to_type

Constructor Details

#initialize(opts) ⇒ Column

Creates a new instance.

Parameters:

  • opts (Hash)

    values options

Options Hash (opts):

  • :name (Object)

    the column name

  • :type (Object)

    the Ruby type. Can be :integer, :string, :time, :timestamp, :float

  • :cql_type (Object)

    the CQL type. See Cassandra CQL documentation and #CQL_TYPES for supported types



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

Instance Attribute Details

#cql_typeObject (readonly)

Returns the value of attribute cql_type.



49
50
51
# File 'lib/believer/column.rb', line 49

def cql_type
  @cql_type
end

#element_typeObject (readonly)

Returns the value of attribute element_type.



49
50
51
# File 'lib/believer/column.rb', line 49

def element_type
  @element_type
end

#key_typeObject (readonly)

Returns the value of attribute key_type.



49
50
51
# File 'lib/believer/column.rb', line 49

def key_type
  @key_type
end

#nameObject (readonly)

Returns the value of attribute name.



49
50
51
# File 'lib/believer/column.rb', line 49

def name
  @name
end

#ruby_typeObject (readonly)

Returns the value of attribute ruby_type.



49
50
51
# File 'lib/believer/column.rb', line 49

def ruby_type
  @ruby_type
end

#value_typeObject (readonly)

Returns the value of attribute value_type.



49
50
51
# File 'lib/believer/column.rb', line 49

def value_type
  @value_type
end

Instance Method Details

#apply_cql_result_row_conversion?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/believer/column.rb', line 119

def apply_cql_result_row_conversion?
  @apply_cql_result_row_conversion == true
end

#convert_to_type(v) ⇒ Object

Converts the value to a one that conforms to the type of this column

Parameters:

  • v (Object)

    the value



81
82
83
84
85
86
87
88
89
90
91
92
# 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_valueObject



110
111
112
113
114
115
116
117
# 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?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/believer/column.rb', line 106

def has_default_value?
  (@default_value != nil) || RUBY_TYPES[ruby_type][:default_value] != nil
end

#to_cqlObject



94
95
96
97
98
99
100
101
102
103
104
# 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