Permalink
Browse files

Start turning into a gem

  • Loading branch information...
1 parent 04d49a7 commit a92a5f28b2a8f40b245eaafedbb401072391724d @nathansobo committed Mar 4, 2011
View
@@ -0,0 +1 @@
+Gemfile.lock
View
@@ -0,0 +1,3 @@
+source "http://www.rubygems.org"
+
+gemspec
View
@@ -0,0 +1,19 @@
+Copyright (C) 2011 by Nathan Sobo
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
No changes.
View
@@ -0,0 +1,17 @@
+$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
+require "prequel/version"
+
+desc "Build prequel #{Prequel::VERSION}"
+task :build do
+ system "gem build prequel.gemspec"
+end
+
+desc "Push prequel #{Prequel::VERSION} to the gem repository"
+task :release => :build do
+ system "gem push prequel-#{Prequel::VERSION}"
+end
+
+desc "Run prequel specs"
+task :spec do
+ system "rspec spec/prequel"
+end
View
@@ -1,3 +1,7 @@
+require 'active_support/all'
+require 'sequel'
+require 'prequel/version'
+
module Prequel
extend ActiveSupport::Autoload
extend self
@@ -0,0 +1,3 @@
+module Prequel
+ VERSION = "0.0.1" unless defined?(::Prequel::VERSION)
+end
View
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+lib = File.expand_path('../lib/', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+
+require 'prequel/version'
+
+Gem::Specification.new do |s|
+ s.name = "prequel"
+ s.version = Prequel::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Nathan Sobo"]
+ s.email = ["nathansobo@gmail.com"]
+ s.homepage = "http://github.com/nathansobo/prequel"
+ s.summary = "A ground-up relational algebraic ORM."
+ s.description = "Prequel is the database library I've always wanted."
+
+ s.add_dependency('activesupport', '>= 3.0.4')
+ s.add_dependency('sequel', '>= 3.20.0')
+ s.add_development_dependency "rspec"
+
+ s.files = Dir.glob("lib/**/*") + %w(LICENSE README.md)
+ s.require_path = 'lib'
+end
@@ -0,0 +1,66 @@
+require 'spec_helper'
+
+module Prequel
+ describe CompositeTuple do
+ before do
+ class Blog < Record
+ column :id, :integer
+ column :title, :string
+ end
+
+ class Post < Record
+ column :id, :integer
+ column :blog_id, :integer
+ column :title, :string
+ end
+
+ Blog.create_table
+ Post.create_table
+
+ DB[:blogs] << { :id => 1, :title => "Blog 1" }
+ DB[:posts] << { :id => 1, :blog_id => 1, :title => "Blog 1, Post 1" }
+ end
+
+ describe "#[](table_or_field_name)" do
+ subject { Blog.join(Post, Blog[:id] => :blog_id).all.first }
+
+ it "returns the record or field value for the given name" do
+ subject[:blogs].should == Blog.find(1)
+ subject[:title].should == Blog.find(1).title
+ subject[:posts__title].should == Post.find(1).title
+ end
+ end
+
+ describe "#get_record(table_name)" do
+ subject { Blog.join(Post, Blog[:id] => :blog_id).all.first }
+
+ it "returns the record associated with the given table name or nil if none exists" do
+ subject.get_record(:blogs).should == Blog.find(1)
+ subject.get_record(:posts).should == Post.find(1)
+ subject.get_record(:elves).should be_nil
+ end
+ end
+
+ describe "#get_field_value(field_name)" do
+ subject { Blog.join(Post, Blog[:id] => :blog_id).all.first }
+
+ it "if given an unqualified name, returns the value of the leftmost field with that name" do
+ subject.get_field_value(:title).should == Blog.find(1).title
+ end
+
+ it "if given a qualified name, returns the value of the field with that name from the specified record" do
+ subject.get_field_value(:posts__title).should == Post.find(1).title
+ subject.get_field_value(:posts__junk).should be_nil
+ subject.get_field_value(:junk__title).should be_nil
+ end
+
+ context "if the composite tuple contains records and non-record tuples (produced from a projection)" do
+ subject { Blog.join(Post.project(:blog_id.as(:my_blog_id)), Blog[:id] => :my_blog_id).all.first }
+
+ it "allows the fields of the non-record tuples to be accessed" do
+ subject.get_field_value(:my_blog_id).should == 1
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe Prequel do
+ describe "when the DB constant is referenced for the first time" do
+ it "assigns it to Sequel::DATABASES.first" do
+ Prequel::DB.should == Sequel::DATABASES.first
+ end
+ end
+
+ describe "when any other constant is referenced" do
+ it "raises an error as usual" do
+ expect { Prequel::JUNK }.to raise_error(NameError)
+ end
+ end
+end
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+module Prequel
+ describe Record do
+ before do
+ class Blog < Record
+ column :id, :integer
+ column :title, :string
+ end
+ end
+
+ describe "when it is subclassed" do
+ specify "the subclass gets associated with a table" do
+ Blog.table.name.should == :blogs
+ Blog.table.tuple_class.should == Blog
+ end
+
+ specify "accessor methods are assigned on the subclass for columns on the table" do
+ b = Blog.new
+ b.title = "Title"
+ b.title.should == "Title"
+ end
+ end
+
+ describe ".new(field_values)" do
+ it "returns a record with the same id from the identity map if it exists" do
+ Blog.create_table
+ DB[:blogs] << { :id => 1, :title => "Blog 1" }
+
+ blog = Blog.find(1)
+ blog.id.should == 1
+ Blog.find(1).should equal(blog)
+
+ stub(Prequel).session { Session.new }
+
+ Blog.find(1).should_not equal(blog)
+ end
+ end
+ end
+end
Oops, something went wrong. Retry.

0 comments on commit a92a5f2

Please sign in to comment.