Added:
trunk/spec/base/count_spec.rb
Modified:
trunk/Rakefile
trunk/lib/active_couch/base.rb
trunk/spec/base/after_delete_spec.rb
trunk/spec/base/after_save_spec.rb
trunk/spec/base/before_delete_spec.rb
trunk/spec/base/before_save_spec.rb
trunk/spec/base/create_spec.rb
trunk/spec/base/database_spec.rb
trunk/spec/base/delete_spec.rb
trunk/spec/base/find_spec.rb
trunk/spec/base/from_json_spec.rb
trunk/spec/base/has_many_spec.rb
trunk/spec/base/has_spec.rb
trunk/spec/base/id_spec.rb
trunk/spec/base/initialize_spec.rb
trunk/spec/base/rev_spec.rb
trunk/spec/base/save_spec.rb
trunk/spec/base/site_spec.rb
trunk/spec/base/to_json_spec.rb
Log:
- ActiveCouch::Base#find supports find by id so Person.find('123')
returns one object (or nil if ID does not exist)
- ActiveCouch::Base#count added to count the number of objects which
satisfy a given condition (E.g. Person.count(:params => {:name => "McLovin"})
Modified: trunk/Rakefile
==============================================================================
--- trunk/Rakefile (original)
+++ trunk/Rakefile Sun Jan 27 20:40:26 2008
@@ -1,6 +1,7 @@
require 'rubygems'
require 'rake/gempackagetask'
+PKG_NAME = 'activecouch'
PKG_VERSION = File.read('VERSION').chomp
PKG_FILES = FileList[
'[A-Z]*',
@@ -26,6 +27,30 @@
Rake::GemPackageTask.new(spec) do |pkg|
pkg.need_zip = true
pkg.need_tar = true
+end
+
+task :lines do
+ lines, codelines, total_lines, total_codelines = 0, 0, 0, 0
+
+ for file_name in FileList["lib/active_couch/**/*.rb"]
+ next if file_name =~ /vendor/
+ f = File.open(file_name)
+
+ while line = f.gets
+ lines += 1
+ next if line =~ /^\s*$/
+ next if line =~ /^\s*#/
+ codelines += 1
+ end
+ puts "L: #{sprintf("%4d", lines)}, LOC #{sprintf("%4d",
codelines)} | #{file_name}"
+
+ total_lines += lines
+ total_codelines += codelines
+
+ lines, codelines = 0, 0
+ end
+
+ puts "Total: Lines #{total_lines}, LOC #{total_codelines}"
end
task :default => [:package]
Modified: trunk/lib/active_couch/base.rb
==============================================================================
--- trunk/lib/active_couch/base.rb (original)
+++ trunk/lib/active_couch/base.rb Sun Jan 27 20:40:26 2008
@@ -289,10 +289,25 @@
case scope
when :all then find_every(options)
when :first then find_every(options).first
- else raise ArgumentError("find must have the
first parameter as either :all or :first")
+ else find_one(scope) #raise ArgumentError("find
must have the first parameter as either :all or :first")
end
end
+ # Retrieves the count of the number of objects in the CouchDB
database, based on the
+ # search parameters given.
+ #
+ # Example:
+ # class Person < ActiveCouch::Base
+ # has :name
+ # end
+ #
+ # # This returns the count of the number of objects
+ # people_count = Person.count(:params => {:name => "McLovin"})
+ def count(params = {})
+ result_set = find(:all, params)
+ result_set.size
+ end
+
# Initializes a new subclass of ActiveCouch::Base and saves in
the CouchDB database
# as a new document
#
@@ -407,6 +422,15 @@
instantiate_collection(connection.get(path))
end
+ def find_one(id)
+ path = "/#{database_name}/#{id}"
+ begin
+ instantiate_object(connection.get(path))
+ rescue ResourceNotFound
+ nil
+ end
+ end
+
# Generates a query string by using the ActiveCouch
convention, which is to
# have the view defined by pre-pending the attribute to be
queried with 'by_'
# So for example, if the params hash is :name => 'McLovin',
@@ -429,6 +453,14 @@
def instantiate_collection(result)
hash = JSON.parse(result)
hash['rows'].collect { |row| self.new(row['value']) }
+ end
+
+ # Instantiates an ActiveCouch::Base object, based on the
result obtained from
+ # the GET URL
+ def instantiate_object(result)
+ puts result
+ hash = JSON.parse(result)
+ self.new(hash)
end
end # End class methods
Modified: trunk/spec/base/after_delete_spec.rb
==============================================================================
--- trunk/spec/base/after_delete_spec.rb (original)
+++ trunk/spec/base/after_delete_spec.rb Sun Jan 27 20:40:26 2008
@@ -21,6 +21,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should have a class method called after_delete" do
@@ -55,6 +56,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should execute the block as a param to after_delete" do
@@ -92,6 +94,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should call before_save in the object passed as a param to
after_delete" do
Modified: trunk/spec/base/after_save_spec.rb
==============================================================================
--- trunk/spec/base/after_save_spec.rb (original)
+++ trunk/spec/base/after_save_spec.rb Sun Jan 27 20:40:26 2008
@@ -23,6 +23,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should have a class method called after_save" do
@@ -54,6 +55,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should execute the block as a param to after_save" do
@@ -87,6 +89,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should call before_save in the object passed as a param to
after_save" do
Modified: trunk/spec/base/before_delete_spec.rb
==============================================================================
--- trunk/spec/base/before_delete_spec.rb (original)
+++ trunk/spec/base/before_delete_spec.rb Sun Jan 27 20:40:26 2008
@@ -21,6 +21,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should have a class method called before_save" do
@@ -55,6 +56,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should execute the block as a param to before_save" do
@@ -91,6 +93,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should call before_save in the object passed as a param to
before_delete" do
Modified: trunk/spec/base/before_save_spec.rb
==============================================================================
--- trunk/spec/base/before_save_spec.rb (original)
+++ trunk/spec/base/before_save_spec.rb Sun Jan 27 20:40:26 2008
@@ -21,6 +21,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should have a class method called before_save" do
@@ -52,6 +53,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should execute the block as a param to before_save" do
@@ -85,6 +87,7 @@
after(:each) do
# Migration needed for this spec
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should call before_save in the object passed as a param to
before_save" do
Added: trunk/spec/base/count_spec.rb
==============================================================================
--- (empty file)
+++ trunk/spec/base/count_spec.rb Sun Jan 27 20:40:26 2008
@@ -0,0 +1,75 @@
+require File.dirname(__FILE__) + '/../spec_helper.rb'
+
+describe "ActiveCouch::Base #count method with just simple attributes" do
+ before(:each) do
+ # Define the model
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984'
+ has :name
+ end
+ # Define the migration
+ class ByName < ActiveCouch::Migration
+ define :for_db => 'people' do
+ with_key 'name'
+ end
+ end
+ # Create the database first
+ ActiveCouch::Migrator.create_database('http://localhost:5984', 'people')
+ # Create a view
+ ActiveCouch::Migrator.migrate('http://localhost:5984', ByName)
+ # Save an object
+ Person.new(:name => 'McLovin').save
+ end
+
+ after(:each) do
+ # Delete the database last
+ ActiveCouch::Migrator.delete_database('http://localhost:5984', 'people')
+ Object.send(:remove_const, :Person)
+ end
+
+ it "should respond to the find method" do
+ Person.should respond_to(:count)
+ end
+
+ it "should return an array with one Person object in it, when sent
method find with parameter :all" do
+ count = Person.count(:params => {:name => 'McLovin'})
+ count.should == 1
+ end
+end
+
+
+describe "ActiveCouch::Base #find method with multiple documents in
the CouchDB database" do
+ before(:each) do
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984'
+
+ has :first_name
+ has :last_name
+ end
+
+ # Define the migration
+ class ByLastName < ActiveCouch::Migration
+ define :for_db => 'people' do
+ with_key 'last_name'
+ end
+ end
+ # Create the database first
+ ActiveCouch::Migrator.create_database('http://localhost:5984', 'people')
+ # Create a view
+ ActiveCouch::Migrator.migrate('http://localhost:5984', ByLastName)
+ # Save two objects
+ Person.create(:last_name => 'McLovin', :first_name => 'Seth')
+ Person.create(:last_name => 'McLovin', :first_name => 'Bob')
+ end
+
+ after(:each) do
+ # Delete the database last
+ ActiveCouch::Migrator.delete_database('http://localhost:5984', 'people')
+ Object.send(:remove_const, :Person)
+ end
+
+ it "should find all objects in the database when find method is sent
the param :all" do
+ count = Person.count(:params => {:last_name => 'McLovin'})
+ count.should == 2
+ end
+end
Modified: trunk/spec/base/create_spec.rb
==============================================================================
--- trunk/spec/base/create_spec.rb (original)
+++ trunk/spec/base/create_spec.rb Sun Jan 27 20:40:26 2008
@@ -11,6 +11,7 @@
after(:each) do
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should have a class method called create" do
Modified: trunk/spec/base/database_spec.rb
==============================================================================
--- trunk/spec/base/database_spec.rb (original)
+++ trunk/spec/base/database_spec.rb Sun Jan 27 20:40:26 2008
@@ -55,6 +55,11 @@
end
end
+ after(:all) do
+ Object.send(:remove_const, :Parent)
+ Object.send(:remove_const, :Child)
+ end
+
it "should have a base_class of the parent" do
Child.base_class.should == Parent
end
Modified: trunk/spec/base/delete_spec.rb
==============================================================================
--- trunk/spec/base/delete_spec.rb (original)
+++ trunk/spec/base/delete_spec.rb Sun Jan 27 20:40:26 2008
@@ -12,6 +12,7 @@
after(:each) do
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should have an instance method called delete" do
@@ -51,6 +52,7 @@
after(:each) do
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should have a class method called delete" do
Modified: trunk/spec/base/find_spec.rb
==============================================================================
--- trunk/spec/base/find_spec.rb (original)
+++ trunk/spec/base/find_spec.rb Sun Jan 27 20:40:26 2008
@@ -24,6 +24,7 @@
after(:each) do
# Delete the database last
ActiveCouch::Migrator.delete_database('http://localhost:5984', 'people')
+ Object.send(:remove_const, :Person)
end
it "should respond to the find method" do
@@ -64,6 +65,7 @@
end
end
+
describe "ActiveCouch::Base #find method with multiple documents in
the CouchDB database" do
before(:each) do
class Person < ActiveCouch::Base
@@ -90,7 +92,8 @@
after(:each) do
# Delete the database last
- ActiveCouch::Migrator.delete_database('http://localhost:5984', 'people')
+ ActiveCouch::Migrator.delete_database('http://localhost:5984', 'people')
+ Object.send(:remove_const, :Person)
end
it "should find all objects in the database when find method is sent
the param :all" do
@@ -147,6 +150,8 @@
after(:each) do
# Create the database first
ActiveCouch::Migrator.delete_database('http://localhost:5984', 'blogs')
+ Object.send(:remove_const, :Blog)
+ Object.send(:remove_const, :Comment)
end
it "should be able to retrieve the simple attributes" do
@@ -162,4 +167,72 @@
(blog.comments.inspect =~ /ya rly!/).should_not == nil
end
+end
+
+describe "ActiveCouch::Base #find method with no params passed" do
+ before(:each) do
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984/'
+ has :name
+ end
+ # Define the migration
+ class ByName < ActiveCouch::Migration
+ define :for_db => 'people' do
+ with_key 'name'
+ end
+ end
+ # Create the database first
+ ActiveCouch::Migrator.create_database('http://localhost:5984', 'people')
+ # Create a view
+ ActiveCouch::Migrator.migrate('http://localhost:5984', ByName)
+ # Save two objects
+ Person.create(:name => 'McLovin')
+ Person.create(:name => 'Seth')
+ end
+
+ after(:each) do
+ # Delete the database last
+ ActiveCouch::Migrator.delete_database('http://localhost:5984', 'people')
+ Object.send(:remove_const, :Person)
+ end
+
+ it "should return all documents if passed :all, with no params specified"
+end
+
+
+describe "ActiveCouch::Base #find method with an ID passed" do
+ before(:each) do
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984/'
+ has :name
+ end
+ # Define the migration
+ class ByName < ActiveCouch::Migration
+ define :for_db => 'people' do
+ with_key 'name'
+ end
+ end
+ # Create the database first
+ ActiveCouch::Migrator.create_database('http://localhost:5984', 'people')
+ # Create a view
+ ActiveCouch::Migrator.migrate('http://localhost:5984', ByName)
+ # Save two objects
+ Person.create(:name => 'McLovin', :id => '123')
+ end
+
+ after(:each) do
+ # Delete the database last
+ ActiveCouch::Migrator.delete_database('http://localhost:5984', 'people')
+ Object.send(:remove_const, :Person)
+ end
+
+ it "should return an ActiveCouch::Base object if the ID exists" do
+ person = Person.find('123')
+ person.name.should == 'McLovin'
+ end
+
+ it "should return nil if the ID does not exist" do
+ person = Person.find('321')
+ person.should == nil
+ end
end
Modified: trunk/spec/base/from_json_spec.rb
==============================================================================
--- trunk/spec/base/from_json_spec.rb (original)
+++ trunk/spec/base/from_json_spec.rb Sun Jan 27 20:40:26 2008
@@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/../spec_helper.rb'
describe "ActiveCouch::Base #from_json method, with many attributes" do
- before(:each) do
+ before(:all) do
class Hotel < ActiveCouch::Base
has :name, :which_is => :text, :with_default_value => "Swissotel
The Stamford"
has :star_rating, :which_is => :decimal, :with_default_value => 5.0
@@ -17,6 +17,12 @@
has_many :hospitals
end
end
+
+ after(:all) do
+ Object.send(:remove_const, :Hotel)
+ Object.send(:remove_const, :Hospital)
+ Object.send(:remove_const, :CrazyPerson)
+ end
it "should have the from_json method" do
Hotel.should respond_to(:from_json)
Modified: trunk/spec/base/has_many_spec.rb
==============================================================================
--- trunk/spec/base/has_many_spec.rb (original)
+++ trunk/spec/base/has_many_spec.rb Sun Jan 27 20:40:26 2008
@@ -20,6 +20,12 @@
@a1 = AgedPerson.new
end
+ after(:each) do
+ Object.send(:remove_const, :Person)
+ Object.send(:remove_const, :AgedPerson)
+ Object.send(:remove_const, :Contact)
+ end
+
it "should have an instance variable called associations which is a
Hash with the key being :people" do
Contact.associations.class.should == Hash
Contact.associations.keys.should == [:people]
@@ -60,6 +66,11 @@
@blog = Blog.new(:title => 'Lolcats Primer', :comments =>
[@comment1, @comment2])
@blog1 = Blog.new(:title => 'Lolcats Primer The Sequel', :comments
=> [{:body => 'can'}, {:body => 'haz'}])
end
+
+ after(:each) do
+ Object.send(:remove_const, :Comment)
+ Object.send(:remove_const, :Blog)
+ end
it "should be able to initialize with a hash which contains
descendents of ActiveCouch::Base" do
@comment1.body.should == "I can haz redbull?"
Modified: trunk/spec/base/has_spec.rb
==============================================================================
--- trunk/spec/base/has_spec.rb (original)
+++ trunk/spec/base/has_spec.rb Sun Jan 27 20:40:26 2008
@@ -5,10 +5,14 @@
class Person < ActiveCouch::Base
has :name, :which_is => :text
end
-
+ # Initialize a new Person object
@p = Person.new
end
+ after(:each) do
+ Object.send(:remove_const, :Person)
+ end
+
it "should have a method called name which returns the value of the
variable name" do
@p.should respond_to(:name)
@p.name.should == ""
@@ -30,6 +34,10 @@
@n = NamedPerson.new
end
+ after(:each) do
+ Object.send(:remove_const, :NamedPerson)
+ end
+
it "should have a method called name which returns the value of the
variable name" do
@n.should respond_to(:name)
@n.name.should == "McLovin"
@@ -51,6 +59,10 @@
@a = AgedPerson.new
end
+
+ after(:each) do
+ Object.send(:remove_const, :AgedPerson)
+ end
it "should have an instance variable called attributes which is a
Hash with the keys being :name, :age" do
AgedPerson.attributes.class.should == Hash
Modified: trunk/spec/base/id_spec.rb
==============================================================================
--- trunk/spec/base/id_spec.rb (original)
+++ trunk/spec/base/id_spec.rb Sun Jan 27 20:40:26 2008
@@ -1,13 +1,16 @@
require File.dirname(__FILE__) + '/../spec_helper.rb'
describe "An object instantiated from the subclass of
ActiveCouch::Base" do
- before(:each) do
+ before(:all) do
class Person < ActiveCouch::Base
has :name, :which_is => :text
end
-
@person = Person.new
end
+
+ after(:all) do
+ Object.send(:remove_const, :Person)
+ end
it "should have accessors for the id attribute" do
@person.should respond_to(:id)
Modified: trunk/spec/base/initialize_spec.rb
==============================================================================
--- trunk/spec/base/initialize_spec.rb (original)
+++ trunk/spec/base/initialize_spec.rb Sun Jan 27 20:40:26 2008
@@ -1,12 +1,16 @@
require File.dirname(__FILE__) + '/../spec_helper.rb'
describe "ActiveCouch::Base #new method with a hash containing one
key-value pair" do
- before(:each) do
+ before(:all) do
class Person < ActiveCouch::Base
has :name
end
end
+ after(:all) do
+ Object.send(:remove_const, :Person)
+ end
+
it "should be able to initialize attributes correctly from a hash" do
p = Person.new(:name => 'McLovin')
p.name.should == 'McLovin'
@@ -14,13 +18,17 @@
end
describe "ActiveCouch::Base #new method with a hash containing more
than one key-value pair" do
- before(:each) do
+ before(:all) do
class Person < ActiveCouch::Base
has :name
has :age, :which_is => :number, :with_default_value => 25
end
end
+ after(:all) do
+ Object.send(:remove_const, :Person)
+ end
+
it "should be able to initialize attributes correctly from the hash" do
p = Person.new(:name => 'McLovin', :age => 12)
p.name.should == 'McLovin'
@@ -29,11 +37,15 @@
end
describe "ActiveCouch::Base #new method with a hash containing a
CouchDB reserved attribute" do
- before(:each) do
+ before(:all) do
class Person < ActiveCouch::Base
has :name
end
end
+
+ after(:all) do
+ Object.send(:remove_const, :Person)
+ end
it "should be able to initialize attributes correclty from the has,
including CouchDB reserved attributes" do
p = Person.new(:name => 'McLovin', :id => '123')
Modified: trunk/spec/base/rev_spec.rb
==============================================================================
--- trunk/spec/base/rev_spec.rb (original)
+++ trunk/spec/base/rev_spec.rb Sun Jan 27 20:40:26 2008
@@ -1,13 +1,17 @@
require File.dirname(__FILE__) + '/../spec_helper.rb'
describe "An object instantiated from the subclass of
ActiveCouch::Base" do
- before(:each) do
+ before(:all) do
class Person < ActiveCouch::Base
has :name, :which_is => :text
end
@person = Person.new
end
+
+ after(:all) do
+ Object.send(:remove_const, :Person)
+ end
it "should have reader/writer for the rev attribute" do
@person.should respond_to(:rev)
Modified: trunk/spec/base/save_spec.rb
==============================================================================
--- trunk/spec/base/save_spec.rb (original)
+++ trunk/spec/base/save_spec.rb Sun Jan 27 20:40:26 2008
@@ -13,6 +13,7 @@
after(:each) do
# Delete after we're done
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should have an instance method called save" do
@@ -40,6 +41,7 @@
after(:each) do
# Delete after we're done
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ Object.send(:remove_const, :Person)
end
it "should be new" do
@@ -56,9 +58,8 @@
it "should allow you to set the id attribute, and the id must be
reflected in the object after saving" do
@person.id = 'abc_def'
- puts @person.to_json
-
@person.save
+
@person.id.should == 'abc_def'
end
Modified: trunk/spec/base/site_spec.rb
==============================================================================
--- trunk/spec/base/site_spec.rb (original)
+++ trunk/spec/base/site_spec.rb Sun Jan 27 20:40:26 2008
@@ -1,13 +1,13 @@
require File.dirname(__FILE__) + '/../spec_helper.rb'
describe "A subclass of ActiveCouch::Base object which has called
establish_connection" do
- before(:each) do
+ before(:all) do
class Cat < ActiveCouch::Base
site 'http://192.168.0.150:7777'
end
end
- after(:each) do
+ after(:all) do
# Remove class definition so we can start fresh in each spec.
Object.send(:remove_const, :Cat)
end
@@ -20,14 +20,14 @@
end
describe "An object instantiated from a subclass of ActiveCouch::Base
which has called establish_connection" do
- before(:each) do
+ before(:all) do
class Cat < ActiveCouch::Base
site 'http://192.168.0.150'
end
@cat = Cat.new
end
- after(:each) do
+ after(:all) do
# Remove class definition so we can start fresh in each spec.
Object.send(:remove_const, :Cat)
end
Modified: trunk/spec/base/to_json_spec.rb
==============================================================================
--- trunk/spec/base/to_json_spec.rb (original)
+++ trunk/spec/base/to_json_spec.rb Sun Jan 27 20:40:26 2008
@@ -11,6 +11,10 @@
@h = Hotel.new
end
+ after(:each) do
+ Object.send(:remove_const, :Hotel)
+ end
+
it "should have to the to_json method" do
@h.should respond_to(:to_json)
end
@@ -52,6 +56,11 @@
@c.add_hospital(@h1)
@c.add_hospital(@h2)
end
+
+ after(:each) do
+ Object.send(:remove_const, :Hospital)
+ Object.send(:remove_const, :CrazyPerson)
+ end
it "should produce valid JSON when sent the to_json method" do
json_output = @c.to_json