Skip to content
This repository has been archived by the owner on Apr 18, 2018. It is now read-only.

Commit

Permalink
start at a sql data store
Browse files Browse the repository at this point in the history
  • Loading branch information
pitluga committed Mar 20, 2012
1 parent 452c057 commit f445da8
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/tags
*.gem
/*.db
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ source 'http://rubygems.org'
gemspec

gem 'rake', '0.9.2.2'
gem 'sequel'
gem 'sqlite3'


group :development do
gem 'rake_commit', '0.12.0'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ GEM
activesupport (>= 3.0)
railties (>= 3.0)
rspec (~> 2.8.0)
sequel (3.33.0)
sprockets (2.1.2)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.5)
thor (0.14.6)
tilt (1.3.3)
timecop (0.3.5)
Expand All @@ -131,4 +133,6 @@ DEPENDENCIES
rake (= 0.9.2.2)
rake_commit (= 0.12.0)
rspec (= 2.8.0)
sequel
sqlite3
timecop (= 0.3.5)
13 changes: 13 additions & 0 deletions lib/curator/sql/configuration.rb
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
48 changes: 48 additions & 0 deletions lib/curator/sql/data_store.rb
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.
12 changes: 12 additions & 0 deletions lib/rails/generators/curator_generator.rb
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
48 changes: 48 additions & 0 deletions spec/curator/sql/data_store_spec.rb
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

0 comments on commit f445da8

Please sign in to comment.