summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2013-07-20 20:23:20 -0600
committermo khan <mo@mokhan.ca>2013-07-20 20:23:20 -0600
commitb52d81a2bd3af2750b3ccc3cdbeb3b3538a88f64 (patch)
treef45248eaf47cf64026b915786034cd90e447c98e
parent7b2613ce668e0ffbb30655b1c2806821ab95377a (diff)
remove the old hand rolled orm. *sniff*
-rw-r--r--lib/boot/bootstrap_container.rb17
-rw-r--r--lib/boot/routes.rb1
-rw-r--r--lib/infrastructure/identity_map.rb4
-rw-r--r--lib/infrastructure/key.rb5
-rw-r--r--lib/infrastructure/simple_context.rb4
-rw-r--r--lib/orm/commands.rb20
-rw-r--r--lib/orm/data_mapper.rb8
-rw-r--r--lib/orm/database_command.rb8
-rw-r--r--lib/orm/database_connection_factory.rb9
-rw-r--r--lib/orm/database_gateway.rb16
-rw-r--r--lib/orm/database_query.rb8
-rw-r--r--lib/orm/queries.rb17
-rw-r--r--lib/orm/repository.rb33
-rw-r--r--lib/orm/sequel_connection_provider.rb7
-rw-r--r--lib/orm/session_factory.rb23
-rw-r--r--lib/web/current_user_interceptor.rb9
-rw-r--r--lib/web/url_builder.rb3
-rw-r--r--spec/integration/orm/database_gateway_spec.rb39
-rw-r--r--spec/integration/orm/repository_spec.rb87
-rw-r--r--spec/integration/orm/test_database_gateway.rb1
-rw-r--r--spec/integration_helper.rb1
-rw-r--r--spec/specs/orm/connection_factory_spec.rb22
-rw-r--r--spec/specs/orm/sequel_connection_provider_spec.rb15
-rw-r--r--spec/specs/orm/unit_of_work_spec.rb1
-rw-r--r--spec/specs/web/url_builder_spec.rb3
25 files changed, 22 insertions, 339 deletions
diff --git a/lib/boot/bootstrap_container.rb b/lib/boot/bootstrap_container.rb
index 93b3892..46a207f 100644
--- a/lib/boot/bootstrap_container.rb
+++ b/lib/boot/bootstrap_container.rb
@@ -17,25 +17,11 @@ class BootstrapContainer
Booty::ViewEngine.new(:root_path => 'lib/commands', :master => 'master')
end
@container.register(:products_repository) do
- #Repository.new(:products, @container.resolve(:database_gateway), DataMapper.new(Product))
HumbleRepository.new(@container.resolve(:context).item_for(@container.resolve(:key)), Product)
end
@container.register(:users_repository) do
- #Repository.new(:users, @container.resolve(:database_gateway), DataMapper.new(User))
HumbleRepository.new(@container.resolve(:context).item_for(@container.resolve(:key)), User)
end
- #@container.register(:database_gateway) do
- #@container.build(DatabaseGateway)
- #end
- #@container.register(:database_connection_factory) do
- #DatabaseConnectionFactory.new(@container.resolve(:database_configuration), @container.resolve(:database_connection_provider))
- #end
- #@container.register(:database_configuration) do
- #DatabaseConfiguration.new
- #end
- #@container.register(:database_connection_provider) do
- #SequelConnectionProvider.new
- #end
@container.register(:unit_of_work_interceptor) do
@container.build(UnitOfWorkInterceptor)
end
@@ -52,9 +38,6 @@ class BootstrapContainer
@container.register(:key) do
Key.new("database.session")
end.as_singleton
- @container.register(:current_user_interceptor) do
- @container.build(CurrentUserInterceptor)
- end
Spank::IOC.bind_to(@container)
end
end
diff --git a/lib/boot/routes.rb b/lib/boot/routes.rb
index 8e5f67f..e57100a 100644
--- a/lib/boot/routes.rb
+++ b/lib/boot/routes.rb
@@ -9,7 +9,6 @@ module Booty
def run
@registry.register(Assets::AssetCommand.new)
- #@registry.register(route_to(Dashboard::IndexCommand, [@container.resolve(:current_user_interceptor)]))
@registry.register(route_to(Dashboard::IndexCommand))
@registry.register(route_to(Products::IndexCommand))
@registry.register(route_to(Products::NewCommand))
diff --git a/lib/infrastructure/identity_map.rb b/lib/infrastructure/identity_map.rb
index 5fa0722..666665f 100644
--- a/lib/infrastructure/identity_map.rb
+++ b/lib/infrastructure/identity_map.rb
@@ -2,15 +2,19 @@ class IdentityMap
def initialize(items = {})
@items = items
end
+
def add(item)
@items[item.id] = item
end
+
def has_item_for?(id)
@items.has_key?(id)
end
+
def item_for(id)
@items[id]
end
+
def evict(item)
@items.reject! { |key, value| key == item.id }
end
diff --git a/lib/infrastructure/key.rb b/lib/infrastructure/key.rb
index bd9dfd4..3bb757b 100644
--- a/lib/infrastructure/key.rb
+++ b/lib/infrastructure/key.rb
@@ -2,18 +2,23 @@ class Key
def initialize(key)
@key = key
end
+
def add_to(store, value)
store[to_sym] = value
end
+
def remove_from(store)
store[to_sym] = nil
end
+
def contained_in?(store)
item_from(store)
end
+
def item_from(store)
store[to_sym]
end
+
def to_sym
@key.to_sym
end
diff --git a/lib/infrastructure/simple_context.rb b/lib/infrastructure/simple_context.rb
index df0fd36..01e1580 100644
--- a/lib/infrastructure/simple_context.rb
+++ b/lib/infrastructure/simple_context.rb
@@ -2,15 +2,19 @@ class SimpleContext
def initialize(store = {})
@store = store
end
+
def add(key, value)
key.add_to(@store, value)
end
+
def remove(key)
key.remove_from(@store)
end
+
def contains?(key)
key.contained_in?(@store)
end
+
def item_for(key)
key.item_from(@store)
end
diff --git a/lib/orm/commands.rb b/lib/orm/commands.rb
deleted file mode 100644
index 236a792..0000000
--- a/lib/orm/commands.rb
+++ /dev/null
@@ -1,20 +0,0 @@
-require "database_command"
-
-class Commands
- def self.update_command_for(table, item)
- create_command { |connection| connection[table].update(item.attributes) }
- end
-
- def self.insert_command_for(table, item)
- create_command do |connection|
- id = connection[table].insert(item.attributes.delete_if {|key, value| key == :id })
- item.instance_variable_set(:@id, id)
- end
- end
-
- private
-
- def self.create_command
- DatabaseCommand.new { |connection| yield(connection) }
- end
-end
diff --git a/lib/orm/data_mapper.rb b/lib/orm/data_mapper.rb
deleted file mode 100644
index 69f7e13..0000000
--- a/lib/orm/data_mapper.rb
+++ /dev/null
@@ -1,8 +0,0 @@
-class DataMapper
- def initialize(clazz)
- @clazz = clazz
- end
- def map_from(row)
- @clazz.new(row)
- end
-end
diff --git a/lib/orm/database_command.rb b/lib/orm/database_command.rb
deleted file mode 100644
index 40943e6..0000000
--- a/lib/orm/database_command.rb
+++ /dev/null
@@ -1,8 +0,0 @@
-class DatabaseCommand
- def initialize(&lambda)
- @lambda = lambda
- end
- def run(connection)
- @lambda.call(connection)
- end
-end
diff --git a/lib/orm/database_connection_factory.rb b/lib/orm/database_connection_factory.rb
deleted file mode 100644
index 07d23d1..0000000
--- a/lib/orm/database_connection_factory.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-class DatabaseConnectionFactory
- def initialize(database_configuration, database_connection_provider)
- @database_configuration = database_configuration
- @database_connection_provider = database_connection_provider
- end
- def create_connection
- @database_configuration.configure(@database_connection_provider.provide)
- end
-end
diff --git a/lib/orm/database_gateway.rb b/lib/orm/database_gateway.rb
deleted file mode 100644
index a613896..0000000
--- a/lib/orm/database_gateway.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-class DatabaseGateway
- def initialize(context, key)
- @context = context
- @key = key
- end
- def run(command_or_query)
- command_or_query.run(connection)
- end
-
- private
-
- def connection
- session = @context.item_for(@key)
- session.connection
- end
-end
diff --git a/lib/orm/database_query.rb b/lib/orm/database_query.rb
deleted file mode 100644
index 58e6464..0000000
--- a/lib/orm/database_query.rb
+++ /dev/null
@@ -1,8 +0,0 @@
-class DatabaseQuery
- def initialize(&lambda)
- @lambda = lambda
- end
- def run(connection)
- @lambda.call(connection)
- end
-end
diff --git a/lib/orm/queries.rb b/lib/orm/queries.rb
deleted file mode 100644
index 5a4106d..0000000
--- a/lib/orm/queries.rb
+++ /dev/null
@@ -1,17 +0,0 @@
-require 'database_query'
-
-class Queries
- def self.find_all(table)
- create_query { |c| c.from(table).all }
- end
-
- def self.find_single(table, options)
- create_query { |c| c.from(table).where(options).first }
- end
-
- private
-
- def self.create_query
- DatabaseQuery.new { |connection| yield(connection) }
- end
-end
diff --git a/lib/orm/repository.rb b/lib/orm/repository.rb
deleted file mode 100644
index 3b44921..0000000
--- a/lib/orm/repository.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-require "queries"
-require "commands"
-
-class Repository
- def initialize(table, database_gateway, data_mapper)
- @table = table
- @database_gateway = database_gateway
- @data_mapper = data_mapper
- end
-
- def find_by(id)
- run(Queries.find_single(@table,{ :id => id })).map_using(@data_mapper)
- end
-
- def find_all
- run(Queries.find_all(@table)).map_all_using(@data_mapper)
- end
-
- def save(item)
- run(create_save_command_for(item))
- end
-
- private
-
- def create_save_command_for(item)
- return Commands.update_command_for(@table, item) if item.id > 0
- Commands.insert_command_for(@table, item)
- end
-
- def run(command)
- @database_gateway.run(command)
- end
-end
diff --git a/lib/orm/sequel_connection_provider.rb b/lib/orm/sequel_connection_provider.rb
deleted file mode 100644
index ffb304c..0000000
--- a/lib/orm/sequel_connection_provider.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-require 'sequel'
-
-class SequelConnectionProvider
- def provide
- Sequel
- end
-end
diff --git a/lib/orm/session_factory.rb b/lib/orm/session_factory.rb
deleted file mode 100644
index 46eceda..0000000
--- a/lib/orm/session_factory.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-class SessionFactory
- def initialize(database_connection_factory)
- @connection_factory = database_connection_factory
- end
- def create
- Session.new(@connection_factory.create_connection)
- end
-end
-class Session
- def initialize(connection)
- @connection = connection
- end
- def run(&block)
- @connection.transaction(&block)
- end
- def connection
- @connection
- end
- def dispose
- @connection.disconnect
- @connection = nil
- end
-end
diff --git a/lib/web/current_user_interceptor.rb b/lib/web/current_user_interceptor.rb
deleted file mode 100644
index c096a61..0000000
--- a/lib/web/current_user_interceptor.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-class CurrentUserInterceptor
- def initialize(container)
- @container = container
- end
-
- def intercept(invocation)
- invocation.proceed
- end
-end
diff --git a/lib/web/url_builder.rb b/lib/web/url_builder.rb
index b6af3a1..efec727 100644
--- a/lib/web/url_builder.rb
+++ b/lib/web/url_builder.rb
@@ -4,11 +4,13 @@ module Booty
@url = url
@params = Hash.new()
end
+
def append(key, value)
@params[key] = [] unless @params.has_key?(key)
@params[key].push(CGI.escape(value))
self
end
+
def build
result = @url.clone
result << "?" if @params.keys.any?
@@ -22,6 +24,7 @@ module Booty
end
result
end
+
def to_s
build
end
diff --git a/spec/integration/orm/database_gateway_spec.rb b/spec/integration/orm/database_gateway_spec.rb
deleted file mode 100644
index 6fcbd21..0000000
--- a/spec/integration/orm/database_gateway_spec.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-require "spec_helper"
-
-describe DatabaseGateway do
- let(:connection_factory) { DatabaseConnectionFactory.new(DatabaseConfiguration.new, SequelConnectionProvider.new) }
- let(:sut) { DatabaseGateway.new(context, key) }
- let(:context) { SimpleContext.new }
- let(:key) { Key.new('blah') }
- let(:session) { Session.new(connection) }
- let(:connection) { connection_factory.create_connection }
-
- before :each do
- TestDatabaseGateway.connection.create_table :blah do
- primary_key :id
- String :name
- end
- context.add(key, session)
- end
- after(:each) do
- TestDatabaseGateway.connection.drop_table :blah
- end
-
- context "when executing a query against the database" do
- before(:each) do
- TestDatabaseGateway.connection.from(:blah).insert(:name => 'mo')
- end
- it "should be able to return a result set" do
- query = DatabaseQuery.new { |c| c.from(:blah).all }
- results = sut.run(query)
- results.should == [:id => 1, :name => 'mo']
- end
- end
- context "when executing a command against the database" do
- it "should run the command against the open connection" do
- command = DatabaseCommand.new { |c| c.from(:blah).insert(:name => 'mo') }
- sut.run(command)
- TestDatabaseGateway.connection.from(:blah).count.should == 1
- end
- end
-end
diff --git a/spec/integration/orm/repository_spec.rb b/spec/integration/orm/repository_spec.rb
deleted file mode 100644
index 6897f48..0000000
--- a/spec/integration/orm/repository_spec.rb
+++ /dev/null
@@ -1,87 +0,0 @@
-require "spec_helper"
-
-describe Repository do
- let(:sut) { Repository.new(:products, gateway, DataMapper.new(Product)) }
- #let(:gateway) { DatabaseGateway.new( DatabaseConnectionFactory.new(DatabaseConfiguration.new, SequelConnectionProvider.new)) }
- let(:gateway) { DatabaseGateway.new(context, key) }
- let(:context) { SimpleContext.new }
- let(:key) { Key.new('blah') }
- let(:session) { Session.new(connection) }
- let(:connection) { DatabaseConnectionFactory.new(DatabaseConfiguration.new, SequelConnectionProvider.new).create_connection }
-
- before :each do
- context.add(key, session)
- end
-
- context "when fetching all products from the database" do
- let(:product) { Product.new(:id => 1, :name => "putty") }
-
- before :each do
- TestDatabaseGateway.connection.from(:products).insert(:name => product.name)
- @results = sut.find_all
- end
- after :each do
- TestDatabaseGateway.connection.from(:products).delete
- end
- it "should return each product" do
- @results.count.should == 1
- end
- it "should return a product" do
- @results.first.should be_an_instance_of(Product)
- end
- it "should map the name properly" do
- @results.first.name.should == product.name
- end
- it "should map the id properly" do
- @results.first.id.should_not be_nil
- end
- end
- context "when saving a new item" do
- let(:product) { Product.new(:name => 'blah') }
- before :each do
- sut.save(product)
- end
- after :each do
- TestDatabaseGateway.delete_all
- end
- it "should insert a new record into the database" do
- found = TestDatabaseGateway.connection.from(:products)
- found.count.should == 1
- end
- it "should update the product with its new id" do
- product.id.should be > 0
- end
- end
- context "when saving an existing item" do
- before :each do
- TestDatabaseGateway.connection.from(:products).insert(:name => 'foo')
- result = sut.find_all.first
- result.change_name('bar')
- sut.save(result)
- end
- after :each do
- TestDatabaseGateway.connection.from(:products).delete
- end
- it "should update the product with the latest changes" do
- found = TestDatabaseGateway.connection.from(:products).first
- found[:name].should == 'bar'
- end
- end
- context "when fetching an item by its id" do
- context "when the id exists" do
- before :each do
- @id = TestDatabaseGateway.connection.from(:products).insert(:name => 'blah')
- end
-
- let(:result) { sut.find_by(@id) }
-
- it "should return the item" do
- result.should == Product.new(:id => @id, :name => 'blah')
- end
-
- after :each do
- TestDatabaseGateway.connection.from(:products).delete
- end
- end
- end
-end
diff --git a/spec/integration/orm/test_database_gateway.rb b/spec/integration/orm/test_database_gateway.rb
index 86b7932..0cff77e 100644
--- a/spec/integration/orm/test_database_gateway.rb
+++ b/spec/integration/orm/test_database_gateway.rb
@@ -1,5 +1,6 @@
require 'yaml'
require 'pg'
+require 'sequel'
class TestDatabaseGateway
def self.connection
diff --git a/spec/integration_helper.rb b/spec/integration_helper.rb
index e29d3f0..ffc3db5 100644
--- a/spec/integration_helper.rb
+++ b/spec/integration_helper.rb
@@ -1,4 +1,5 @@
require "spec_helper"
+require 'sequel'
RSpec.configure do |config|
config.before :all do
diff --git a/spec/specs/orm/connection_factory_spec.rb b/spec/specs/orm/connection_factory_spec.rb
deleted file mode 100644
index e3aa677..0000000
--- a/spec/specs/orm/connection_factory_spec.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-require "spec_helper"
-
-describe DatabaseConnectionFactory do
- let(:sut) { DatabaseConnectionFactory.new(database_configuration, connection_provider) }
- let(:database_configuration) { fake }
- let(:connection_provider) { fake }
-
- context "when creating a new connection" do
- let(:connection) { fake }
- before :each do
- connection_provider.stub(:provide).and_return(connection)
- database_configuration.stub(:configure).with(connection).and_return(connection)
- @result = sut.create_connection
- end
- it "should return a new connection using the proper connection configuration" do
- @result.should == connection
- end
- it "should configure the connection" do
- database_configuration.should have_received(:configure, connection)
- end
- end
-end
diff --git a/spec/specs/orm/sequel_connection_provider_spec.rb b/spec/specs/orm/sequel_connection_provider_spec.rb
deleted file mode 100644
index f05a448..0000000
--- a/spec/specs/orm/sequel_connection_provider_spec.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-require "spec_helper"
-require "sequel"
-
-describe SequelConnectionProvider do
- let(:sut) { SequelConnectionProvider.new }
-
- context "when providing a connection" do
- before :each do
- @result = sut.provide
- end
- it "should provider the Sequel api gateway" do
- @result.should == Sequel
- end
- end
-end
diff --git a/spec/specs/orm/unit_of_work_spec.rb b/spec/specs/orm/unit_of_work_spec.rb
index 685f8a9..756d131 100644
--- a/spec/specs/orm/unit_of_work_spec.rb
+++ b/spec/specs/orm/unit_of_work_spec.rb
@@ -16,6 +16,7 @@ describe UnitOfWork do
session.should have_received(:begin_transaction)
end
end
+
context "when ending a unit of work" do
before { sut.dispose }
diff --git a/spec/specs/web/url_builder_spec.rb b/spec/specs/web/url_builder_spec.rb
index de2a35b..ece5838 100644
--- a/spec/specs/web/url_builder_spec.rb
+++ b/spec/specs/web/url_builder_spec.rb
@@ -11,6 +11,7 @@ module Booty
result.should == url
end
end
+
context "when building a url" do
let(:result) { sut.build }
@@ -21,6 +22,7 @@ module Booty
result.should == "#{url}?scope=https%3A%2F%2Fwww.googleapis.com"
end
end
+
context "with different query string params" do
before :each do
sut.append(:scope, "https://www.googleapis.com")
@@ -31,6 +33,7 @@ module Booty
result.should == "#{url}?scope=https%3A%2F%2Fwww.googleapis.com&redirect_uri=https%3A%2F%2Fmokhan.ca"
end
end
+
context "when multiple values are added for the same key" do
before :each do
sut.append(:scope, 'blah')