Skip to content

Commit

Permalink
Allow method level options.
Browse files Browse the repository at this point in the history
Method level options override adapter options, but like adapter options
will not override the specific things necessary for the adapter to work
properly.
  • Loading branch information
jnunemaker committed Nov 16, 2012
1 parent f857da0 commit c681ab1
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 88 deletions.
62 changes: 33 additions & 29 deletions lib/adapter/cassanity.rb
Expand Up @@ -8,43 +8,35 @@ module Cassanity

def_delegator :@client, :schema

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

if (read_options = options[:read_options])
filtered_options = without_keys(read_options, select_arguments.keys)
select_arguments.update(filtered_options)
end

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

def write(key, attributes)
update_arguments = {
set: encode(attributes),
where: where(key)
}

if (write_options = options[:write_options])
filtered_options = without_keys(write_options, update_arguments.keys)
update_arguments.update(filtered_options)
end
# Public
def write(key, attributes, args = nil)
operation_options = {set: encode(attributes), where: where(key)}
adapter_options = options[:write_options]
arguments = update_arguments(operation_options, adapter_options, args)

client.update(update_arguments)
client.update(arguments)
end

def delete(key)
delete_arguments = {where: where(key)}

if (delete_options = options[:delete_options])
filtered_options = without_keys(delete_options, delete_arguments.keys)
delete_arguments.update(filtered_options)
end
# Public
def delete(key, args = nil)
operation_options = {where: where(key)}
adapter_options = options[:delete_options]
arguments = update_arguments(operation_options, adapter_options, args)

client.delete(delete_arguments)
client.delete(arguments)
end

# Public
def clear
client.truncate
end
Expand All @@ -60,8 +52,20 @@ def where(criteria)
end

# Private
def without_keys(options, keys)
options.reject { |key| keys.include?(key) }
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
Expand Down
132 changes: 73 additions & 59 deletions spec/cassanity_spec.rb
Expand Up @@ -18,135 +18,149 @@

context "with adapter read options" do
it "uses read options for read method" do
client = COLUMN_FAMILIES[:single]
primary_key = client.schema.primary_keys.first
key = 'foo'
where = {primary_key => key}
using = {consistency: :quorum}

client = COLUMN_FAMILIES[:single]
options = {read_options: {
using: {consistency: :quorum},
}}
adapter = Adapter[adapter_name].new(client, options)

client.should_receive(:select).with({
where: where,
using: using,
where: {:some_key => 'foo'},
using: {consistency: :quorum},
}).and_return([])

adapter.read(key)
adapter.read('foo')
end

it "does not override where" do
client = COLUMN_FAMILIES[:single]
primary_key = client.schema.primary_keys.first
key = 'foo'
where = {primary_key => key}
using = {consistency: :quorum}
client = COLUMN_FAMILIES[:single]
options = {read_options: {
where: {:some_key => 'bar'},
using: {consistency: :quorum},
}}
adapter = Adapter[adapter_name].new(client, options)

client.should_receive(:select).with({
where: {:some_key => 'foo'},
using: {consistency: :quorum},
}).and_return([])

adapter.read('foo')
end

it "can be overriden by read method options" do
client = COLUMN_FAMILIES[:single]
options = {read_options: {
where: {primary_key => 'bar'},
using: {consistency: :quorum},
}}
adapter = Adapter[adapter_name].new(client, options)

client.should_receive(:select).with({
where: where,
using: using,
where: {:some_key => 'foo'},
using: {consistency: :one},
}).and_return([])

adapter.read(key)
adapter.read('foo', using: {consistency: :one})
end
end

context "with adapter write options" do
it "uses write options for write method" do
client = COLUMN_FAMILIES[:single]
primary_key = client.schema.primary_keys.first
key = 'foo'
set = {'name' => 'New Name'}
where = {primary_key => key}
using = {consistency: :quorum}

client = COLUMN_FAMILIES[:single]
options = {write_options: {
using: {consistency: :quorum},
}}
adapter = Adapter[adapter_name].new(client, options)

client.should_receive(:update).with({
set: set,
where: where,
using: using,
set: {'name' => 'New Name'},
where: {:some_key => 'foo'},
using: {consistency: :quorum},
})

adapter.write(key, set)
adapter.write('foo', {'name' => 'New Name'})
end

it "does not override where or set" do
client = COLUMN_FAMILIES[:single]
primary_key = client.schema.primary_keys.first
key = 'foo'
set = {'name' => 'New Name'}
where = {primary_key => key}
using = {consistency: :quorum}

client = COLUMN_FAMILIES[:single]
options = {write_options: {
where: {primary_key => 'should not use this'},
where: {:some_key => 'should not use this'},
set: {'name' => 'should not use this'},
using: {consistency: :quorum,
}}}
adapter = Adapter[adapter_name].new(client, options)

client.should_receive(:update).with({
set: set,
where: where,
using: using,
set: {'name' => 'New Name'},
where: {:some_key => 'foo'},
using: {consistency: :quorum},
})

adapter.write('foo', {'name' => 'New Name'})
end

it "can be overriden by write method options" do
client = COLUMN_FAMILIES[:single]
options = {write_options: {
using: {consistency: :quorum},
}}
adapter = Adapter[adapter_name].new(client, options)

client.should_receive(:update).with({
set: {'name' => 'New Name'},
where: {:some_key => 'foo'},
using: {consistency: :one},
})

adapter.write(key, set)
adapter.write('foo', {'name' => 'New Name'}, using: {consistency: :one})
end
end

context "with adapter delete options" do
it "uses delete options for delete method" do
client = COLUMN_FAMILIES[:single]
primary_key = client.schema.primary_keys.first
key = 'foo'
where = {primary_key => key}
using = {consistency: :quorum}

client = COLUMN_FAMILIES[:single]
options = {delete_options: {
using: {consistency: :quorum},
}}
adapter = Adapter[adapter_name].new(client, options)

client.should_receive(:delete).with({
where: where,
using: using,
where: {:some_key => 'foo'},
using: {consistency: :quorum},
})

adapter.delete(key)
adapter.delete('foo')
end

it "does not override where" do
client = COLUMN_FAMILIES[:single]
primary_key = client.schema.primary_keys.first
key = 'foo'
where = {primary_key => key}
using = {consistency: :quorum}
client = COLUMN_FAMILIES[:single]
options = {delete_options: {
where: {:some_key => 'bar'},
using: {consistency: :quorum},
}}
adapter = Adapter[adapter_name].new(client, options)

client.should_receive(:delete).with({
where: {:some_key => 'foo'},
using: {consistency: :quorum},
})

adapter.delete('foo')
end

it "can be overriden by delete method options" do
client = COLUMN_FAMILIES[:single]
options = {delete_options: {
where: {primary_key => 'bar'},
using: {consistency: :quorum},
}}
adapter = Adapter[adapter_name].new(client, options)

client.should_receive(:delete).with({
where: where,
using: using,
where: {:some_key => 'foo'},
using: {consistency: :one},
})

adapter.delete(key)
adapter.delete('foo', using: {consistency: :one})
end
end

Expand Down

0 comments on commit c681ab1

Please sign in to comment.