Sha256: 6dad66356633b7156dd410932e13d165a5562e3c83002cd7d9eb48a223c7c545

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'adapter'
require 'forwardable'
require 'cassanity'

module Adapter
  module Cassanity
    extend Forwardable

    def_delegator :@client, :schema

    # Public
    def read(key, options = nil)
      operation_options = {where: where(key)}
      adapter_options = @options[:read]
      arguments = update_arguments(operation_options, adapter_options, options)

      rows = @client.select(arguments)
      rows.empty? ? nil : decode(rows.first)
    end

    # Public
    def write(key, attributes, options = nil)
      operation_options = {set: encode(attributes), where: where(key)}
      adapter_options = @options[:write]
      arguments = update_arguments(operation_options, adapter_options, options)

      @client.update(arguments)
    end

    # Public
    def delete(key, options = nil)
      operation_options = {where: where(key)}
      adapter_options = @options[:delete]
      arguments = update_arguments(operation_options, adapter_options, options)

      @client.delete(arguments)
    end

    # Public
    def clear
      @client.truncate
    end

    # Private
    def where(criteria)
      if schema.composite_primary_key?
        criteria
      else
        primary_key = schema.primary_keys.first
        {primary_key => criteria}
      end
    end

    # Private
    def update_arguments(operation_options, adapter_options, method_options)
      keys = operation_options.keys

      if !adapter_options.nil? && !adapter_options.empty?
        filtered_options = adapter_options.reject { |key| keys.include?(key) }
        operation_options.update(filtered_options)
      end

      if !method_options.nil? && !method_options.empty?
        filtered_options = method_options.reject { |key| keys.include?(key) }
        operation_options.update(filtered_options)
      end

      operation_options
    end
  end
end

Adapter.define(:cassanity, Adapter::Cassanity)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
adapter-cassanity-0.2.0 lib/adapter/cassanity.rb