This repository has been archived by the owner on May 29, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
415 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,89 @@ | ||
require "virtus" | ||
require "cassandra-cql/1.2" | ||
require 'virtus' | ||
require 'cql' | ||
require 'simple_uuid' | ||
require 'forwardable' | ||
|
||
|
||
require "virsandra/version" | ||
require 'virsandra/errors' | ||
require "virsandra/configuration" | ||
require "virsandra/connection" | ||
require "virsandra/cql_value" | ||
require "virsandra/query" | ||
require "virsandra/model_query" | ||
require "virsandra/model" | ||
|
||
module Virsandra | ||
|
||
extend Configuration | ||
|
||
class << self | ||
extend Forwardable | ||
def_delegator :connection, :execute | ||
def configuration | ||
Thread.current[:configuration] ||= Virsandra::Configuration.new | ||
end | ||
|
||
def connection | ||
@connection = Connection.new(self) if dirty? | ||
@connection | ||
def configure | ||
yield configuration | ||
end | ||
|
||
def dirty? | ||
return true if @connection.nil? | ||
@connection.options != self.to_hash | ||
def connection | ||
if dirty? | ||
disconnect! | ||
Thread.current[:connection] = Virsandra::Connection.new(configuration) | ||
configuration.accept_changes | ||
end | ||
Thread.current[:connection] | ||
end | ||
|
||
def disconnect! | ||
if @connection && @connection.handle | ||
@connection.disconnect! | ||
if Thread.current[:connection].respond_to?(:disconnect!) | ||
Thread.current[:connection].disconnect! | ||
end | ||
@connection = nil | ||
Thread.current[:connection] = nil | ||
end | ||
|
||
def reset! | ||
configuration.reset! | ||
end | ||
|
||
def reset_configuration! | ||
Thread.current[:configuration] = nil | ||
end | ||
|
||
def consistency | ||
configuration.consistency | ||
end | ||
|
||
def keyspace | ||
configuration.keyspace | ||
end | ||
|
||
def servers | ||
configuration.servers | ||
end | ||
|
||
def consistency=(value) | ||
configuration.consistency = value | ||
end | ||
|
||
def keyspace=(value) | ||
configuration.keyspace = value | ||
end | ||
|
||
def servers=(value) | ||
configuration.servers = value | ||
end | ||
|
||
def execute(query) | ||
connection.execute(query) | ||
end | ||
|
||
private | ||
|
||
def dirty? | ||
Thread.current[:connection].nil? || configuration.changed? | ||
end | ||
|
||
end | ||
end | ||
|
||
|
||
|
||
require "virsandra/connection" | ||
require "virsandra/cql_value" | ||
require "virsandra/query" | ||
require "virsandra/model_query" | ||
require "virsandra/model" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Virsandra | ||
class ConfigurationError < ArgumentError; ; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Virsandra | ||
VERSION = "0.0.1" | ||
VERSION = "0.5.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'spec_helper' | ||
|
||
TEST_KEYSPACE = "virtest" | ||
|
||
module IntegrationTestHelper | ||
def create_keyspace | ||
Virsandra.keyspace = 'system' | ||
Virsandra.execute("CREATE KEYSPACE #{TEST_KEYSPACE} WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1}") | ||
Virsandra.reset! | ||
end | ||
|
||
def create_companies_table | ||
cql = <<-CQL | ||
CREATE TABLE companies ( | ||
id uuid, | ||
name text, | ||
score int, | ||
founder text, | ||
founded int, | ||
PRIMARY KEY (id, score)) | ||
CQL | ||
Virsandra.keyspace = TEST_KEYSPACE | ||
Virsandra.execute(cql) | ||
end | ||
|
||
def drop_keyspace | ||
Virsandra.reset! | ||
Virsandra.keyspace = 'system' | ||
Virsandra.execute("DROP KEYSPACE #{TEST_KEYSPACE}") | ||
end | ||
|
||
def build_up | ||
begin | ||
create_keyspace | ||
create_companies_table | ||
rescue Cql::QueryError | ||
drop_keyspace | ||
|
||
if defined?(retried) | ||
raise $! | ||
else | ||
retried = true | ||
retry | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
||
|
||
RSpec.configure do |config| | ||
|
||
config.include(IntegrationTestHelper) | ||
|
||
config.before do | ||
if example.metadata[:integration] | ||
build_up | ||
Virsandra.reset! | ||
Virsandra.keyspace = TEST_KEYSPACE | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'feature_helper' | ||
|
||
describe "Virsandra", integration: true do | ||
|
||
it "return connection to server" do | ||
Virsandra.connection.should_not be_nil | ||
end | ||
|
||
it "allows to disconnect" do | ||
Virsandra.connection.should_receive(:disconnect!).and_call_original | ||
Virsandra.disconnect! | ||
end | ||
end |
Oops, something went wrong.