Skip to content

Commit

Permalink
- ActiveCouch::Base#find supports find by id so Person.find('123') re…
Browse files Browse the repository at this point in the history
…turns 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"})
  • Loading branch information
arunthampi committed Jan 28, 2008
1 parent 9e6e7a3 commit 7d1ff41
Show file tree
Hide file tree
Showing 20 changed files with 299 additions and 16 deletions.
25 changes: 25 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'rubygems'
require 'rake/gempackagetask'

PKG_NAME = 'activecouch'
PKG_VERSION = File.read('VERSION').chomp
PKG_FILES = FileList[
'[A-Z]*',
Expand Down Expand Up @@ -28,4 +29,28 @@ Rake::GemPackageTask.new(spec) do |pkg|
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]
34 changes: 33 additions & 1 deletion lib/active_couch/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,25 @@ def find(*arguments)
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
#
Expand Down Expand Up @@ -407,6 +422,15 @@ def find_every(options)
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',
Expand All @@ -430,6 +454,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

private
Expand Down
3 changes: 3 additions & 0 deletions spec/base/after_delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def print_message
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
Expand Down Expand Up @@ -55,6 +56,7 @@ class Person < ActiveCouch::Base
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
Expand Down Expand Up @@ -92,6 +94,7 @@ class Person < ActiveCouch::Base
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
Expand Down
3 changes: 3 additions & 0 deletions spec/base/after_save_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def print_message
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
Expand Down Expand Up @@ -54,6 +55,7 @@ class Person < ActiveCouch::Base
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
Expand Down Expand Up @@ -87,6 +89,7 @@ class Person < ActiveCouch::Base
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
Expand Down
3 changes: 3 additions & 0 deletions spec/base/before_delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def zero_age
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
Expand Down Expand Up @@ -55,6 +56,7 @@ class Person < ActiveCouch::Base
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
Expand Down Expand Up @@ -91,6 +93,7 @@ class Person < ActiveCouch::Base
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
Expand Down
3 changes: 3 additions & 0 deletions spec/base/before_save_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def set_first_name
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
Expand Down Expand Up @@ -52,6 +53,7 @@ class Person < ActiveCouch::Base
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
Expand Down Expand Up @@ -85,6 +87,7 @@ class Person < ActiveCouch::Base
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
Expand Down
75 changes: 75 additions & 0 deletions spec/base/count_spec.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/base/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Person < ActiveCouch::Base

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
Expand Down
5 changes: 5 additions & 0 deletions spec/base/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class Child < Parent
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
Expand Down
2 changes: 2 additions & 0 deletions spec/base/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Person < ActiveCouch::Base

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
Expand Down Expand Up @@ -51,6 +52,7 @@ class Person < ActiveCouch::Base

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
Expand Down
Loading

0 comments on commit 7d1ff41

Please sign in to comment.