This repository has been archived by the owner on Apr 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
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
8 changed files
with
129 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/tags | ||
*.gem | ||
/*.db |
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,13 @@ | ||
module Curator | ||
module Sql | ||
class Configuration | ||
include Curator::Configuration | ||
|
||
attr_accessor :uri | ||
|
||
def data_store | ||
Curator::Sql::DataStore | ||
end | ||
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,48 @@ | ||
require 'sequel' | ||
require 'active_support/core_ext/hash/except' | ||
|
||
module Curator | ||
module Sql | ||
class DataStore | ||
def self.client | ||
@client ||= Sequel.connect(Curator.config.uri) | ||
end | ||
|
||
def self.save(options) | ||
table_name = options[:collection_name] | ||
key = options[:key] | ||
value = options[:value] | ||
table = client[table_name.to_sym] | ||
if table.filter(:key => key).empty? | ||
table.insert(value.merge(:key => key)) | ||
else | ||
table.filter(:key => key).update(value) | ||
end | ||
key | ||
end | ||
|
||
def self.delete(table_name, key) | ||
client[table_name.to_sym].filter(:key => key).delete | ||
end | ||
|
||
def self.find_by_key(table_name, key) | ||
_map_row client[table_name.to_sym][:key => key] | ||
end | ||
|
||
def self.find_by_index(table_name, column, value) | ||
client[table_name.to_sym].filter(column.to_sym => value).all.map(&method(:_map_row)) | ||
end | ||
|
||
def self.reset! | ||
|
||
end | ||
|
||
def self._map_row(row) | ||
return unless row | ||
value = row.except(:id, :key).map { |k,v| [k.to_s, v] }.reject {|(_,v)| v.nil? } | ||
{ :key => row[:key], :data => Hash[value] } | ||
end | ||
|
||
end | ||
end | ||
end |
Empty file.
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,12 @@ | ||
require 'rails/generators/named_base' | ||
|
||
module Curator | ||
module Generators | ||
class Base < Rails::Generators::NamedBase | ||
|
||
def self.source_root | ||
@_curator_source_root ||= File.expand_path("../#{base_name}/#{generator_name}/templates", __FILE__) | ||
end | ||
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,48 @@ | ||
require 'spec_helper' | ||
require 'curator/sql/data_store' | ||
require 'curator/shared_data_store_specs' | ||
|
||
module Curator | ||
module Sql | ||
describe Curator::Sql::DataStore do | ||
describe "sqlite" do | ||
include_examples "data_store", DataStore | ||
|
||
around :each do |example| | ||
db = Sequel.connect "sqlite://test.db" | ||
db.transaction(:rollback => :always) do | ||
example.run | ||
end | ||
end | ||
|
||
before :all do | ||
File.delete "test.db" | ||
db = Sequel.connect "sqlite://test.db" | ||
db.create_table :test_collection do | ||
primary_key :id | ||
String :key | ||
String :indexed_key | ||
end | ||
db.create_table :fake_things do | ||
primary_key :id | ||
String :key | ||
Integer :foo | ||
String :bar | ||
String :k | ||
end | ||
db.create_table :abyss do | ||
primary_key :id | ||
String :invalid_index | ||
end | ||
end | ||
|
||
with_config do | ||
Curator.configure(:sql) do |config| | ||
config.environment = "test" | ||
config.uri = "sqlite://test.db" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |