Sha256: f228afc916b4f9f761713da3d41c39dfc63860d8c1407719e176a606290b9832

Contents?: true

Size: 948 Bytes

Versions: 2

Compression:

Stored size: 948 Bytes

Contents

class Cassandra::Mapper::Data
  class Response
    attr_reader :config, :key_values, :columns

    def initialize(config, key_values, columns)
      @config     = config
      @key_values = key_values
      @columns    = columns
    end

    def keys
      Hash[config.key.zip key_values]
    end

    def subkeys(values)
      Hash[config.subkey.zip values]
    end

    def unpack
      return [] if columns.empty?
      records = columns.group_by {|composite, _| composite[0..-2] }
      records.map do |subkey_values, fields|
        record = keys.merge subkeys(subkey_values)
        fields.each do |composite, value|
          field = composite[-1]
          record[field.to_sym] = value unless field.empty?
        end
        convert! record
      end
    end

    private

    def convert!(data)
      data.each do |field, value|
        data[field] = Cassandra::Mapper::Convert.from config.types[field], value
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cassandra-mapper-0.2 lib/cassandra/mapper/data/response.rb
cassandra-mapper-0.1 lib/cassandra/mapper/data/response.rb