Skip to content

Commit

Permalink
first working example, but only for creating
Browse files Browse the repository at this point in the history
  • Loading branch information
paulca committed Feb 7, 2009
0 parents commit e51f76b
Show file tree
Hide file tree
Showing 12 changed files with 552 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Stuffing
========

Introduction goes here.


Example
=======

Example goes here.


Copyright (c) 2009 [name of plugin creator], released under the MIT license
11 changes: 11 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rake'
require 'spec/rake/spectask'

desc 'Default: run specs.'
task :default => :spec

desc 'Run the specs'
Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
t.spec_files = FileList['spec/**/*_spec.rb']
end
5 changes: 5 additions & 0 deletions autotest/discover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))

Autotest.add_discovery do
"rspec"
end
2 changes: 2 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'stuffing'
ActiveRecord::Base.send(:include, Stuffing)
1 change: 1 addition & 0 deletions install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Install hook code here
46 changes: 46 additions & 0 deletions lib/stuffing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'ostruct'
require 'couchrest'
module Stuffing
def self.included(base)
base.extend StuffingMethod
end

module StuffingMethod
def stuffing(method_name = :stuffing, options = {})

after_create :create_stuffing
after_update :update_stuffing
after_destroy :destroy_stuffing

class_eval do

@@database = options[:database] || "#{File.basename(RAILS_ROOT)}_#{RAILS_ENV}"
@@host = options[:host] || 'localhost'
@@port = options[:port] || 5984
@@method_name = method_name

def couchdb
connection = CouchRest.new("http://#{@@host}:#{@@port}")
connection.database!(@@database)
end

def couchdb_id
"#{self.class}-#{id}"
end

define_method(method_name) do
@stuffing ||= new_record? ? {} : couchdb.get(couchdb_id)
end

define_method("#{method_name}=") do |args|
@stuffing = args
end

def create_stuffing
couchdb.save({'_id' => couchdb_id}.merge(send(@@method_name)))
end

end
end
end
end
1 change: 1 addition & 0 deletions spec.opts
410 changes: 410 additions & 0 deletions spec/debug.log

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
begin
require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
rescue LoadError
puts "You need to install rspec in your base app"
exit
end

plugin_spec_dir = File.dirname(__FILE__)
ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")

48 changes: 48 additions & 0 deletions spec/stuffing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require File.dirname(__FILE__) + '/spec_helper'

class Booja < ActiveRecord::Base
stuffing
end

describe do
before do
@booja = Booja.new
@booja.couchdb.delete!
end

it "should have a default stuffing of an empty Hash" do
@booja.stuffing.should == {}
end

it "should be able to set the stuffing" do
@booja.stuffing = 'hello'
@booja.stuffing.should == 'hello'
end

it "should give me the database" do
@booja.couchdb.should be_a_kind_of(CouchRest::Database)
end

it "should give me the database" do
@booja.couchdb.name.should == 'plugins_test'
end

it "should save the record to CouchDB" do
@booja.save
CouchRest.new('http://localhost:5984').database('plugins_test').get("Booja-#{@booja.id}").keys.should == ['_rev', '_id']
end

describe "updating" do
before do
@booja = Booja.new
@booja.couchdb.delete!
@booja.stuffing = {:this => 'that'}
@booja.save
end

it "should find the booja" do
@found_booja = Booja.find(@booja.id)
@found_booja.stuffing.keys.should == ['_id', '_rev', 'this']
end
end
end
4 changes: 4 additions & 0 deletions tasks/stuffing_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :stuffing do
# # Task goes here
# end
1 change: 1 addition & 0 deletions uninstall.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Uninstall hook code here

0 comments on commit e51f76b

Please sign in to comment.