Class: CassandraCQL::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra-cql/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, schema) ⇒ Row

Returns a new instance of Row.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cassandra-cql/row.rb', line 21

def initialize(row, schema)
  @row, @schema = row, schema
  @value_cache = Hash.new { |h, key|
    # If it's a number and not one of our columns, assume it's an index
    if key.kind_of?(Fixnum) and !column_indices.key?(key)
      column_name = column_names[key]
      column_index = key
    else
      column_name = key
      column_index = column_indices[key]
    end
    
    if column_index.nil?
      # Cache negative hits
      h[column_name] = nil
    else
      h[column_name] = ColumnFamily.cast(@row.columns[column_index].value, @schema.values[@row.columns[column_index].name])
    end
  }
end

Instance Attribute Details

#rowObject (readonly)

Returns the value of attribute row.



19
20
21
# File 'lib/cassandra-cql/row.rb', line 19

def row
  @row
end

Instance Method Details

#[](obj) ⇒ Object



42
43
44
# File 'lib/cassandra-cql/row.rb', line 42

def [](obj)
  @value_cache[obj]
end

#column_indicesObject



52
53
54
# File 'lib/cassandra-cql/row.rb', line 52

def column_indices
  @column_indices ||= Hash[column_names.each_with_index.to_a]
end

#column_namesObject



46
47
48
49
50
# File 'lib/cassandra-cql/row.rb', line 46

def column_names
  @names ||= @row.columns.map do |column|
    ColumnFamily.cast(column.name, @schema.names[column.name])
  end
end

#column_valuesObject



56
57
58
59
60
# File 'lib/cassandra-cql/row.rb', line 56

def column_values
  column_names.map do |name|
    @value_cache[name]
  end
end

#columnsObject



62
63
64
# File 'lib/cassandra-cql/row.rb', line 62

def columns
  @row.columns.size
end

#to_aObject



66
67
68
# File 'lib/cassandra-cql/row.rb', line 66

def to_a
  column_values
end

#to_hashObject

TODO: This should be an ordered hash



71
72
73
# File 'lib/cassandra-cql/row.rb', line 71

def to_hash
  Hash[([column_names, column_values]).transpose]
end