Class: Cassandra::DynamicComposite

Inherits:
Composite
  • Object
show all
Defined in:
lib/cassandra/dynamic_composite.rb

Instance Attribute Summary collapse

Attributes inherited from Composite

#column_slice, #parts

Instance Method Summary collapse

Methods inherited from Composite

#<=>, #[], #inspect, new_from_packed, #slice_end_of_component, #to_s

Constructor Details

#initialize(*parts) ⇒ DynamicComposite

Returns a new instance of DynamicComposite.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cassandra/dynamic_composite.rb', line 5

def initialize(*parts)
  return if parts.empty?

  options = {}
  if parts.last.is_a?(Hash)
    options = parts.pop
  end
  @column_slice = options[:slice]
  raise ArgumentError if @column_slice != nil && ![:before, :after].include?(@column_slice)

  if parts.length == 1 && parts[0].instance_of?(self.class)
    @column_slice = parts[0].column_slice
    @parts = parts[0].parts
    @types = parts[0].types
  elsif parts.length == 1 && parts[0].instance_of?(String) && @column_slice.nil? && try_packed_composite(parts[0])
    @hash = parts[0].hash
  else
    @types, @parts = parts.transpose
  end
end

Instance Attribute Details

#typesObject

Returns the value of attribute types.



3
4
5
# File 'lib/cassandra/dynamic_composite.rb', line 3

def types
  @types
end

Instance Method Details

#fast_unpack(packed_string) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
# File 'lib/cassandra/dynamic_composite.rb', line 47

def fast_unpack(packed_string)
  result = try_packed_composite(packed_string)
  raise ArgumentError.new("Invalid DynamicComposite column") if !result
  @hash = packed_string.hash
end

#packObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cassandra/dynamic_composite.rb', line 26

def pack
  packed_parts = @parts.map do |part|
    [part.length].pack('n') + part + "\x00"
  end

  if @column_slice
    part = @parts[-1]
    packed_parts[-1] = [part.length].pack('n') + part + slice_end_of_component
  end

  packed_types = @types.map do |type|
    if type.length == 1
      [0x8000 | type[0].ord].pack('n')
    else
      [type.length].pack('n') + type
    end
  end

  return packed_types.zip(packed_parts).flatten.join('')
end