Permalink
Browse files

first working version

  • Loading branch information...
1 parent f4a105f commit 0c0b89468078623c954bbab9b301e78fba8c2e31 @steveklabnik committed Sep 1, 2010
Showing with 58 additions and 42 deletions.
  1. +2 −1 Gemfile
  2. +1 −6 Gemfile.lock
  3. +17 −17 lib/extra/extra.rb
  4. +19 −18 spec/extra_spec.rb
  5. +19 −0 test.rb
View
@@ -1,6 +1,7 @@
source "http://rubygems.org"
-gem "candy"
+# gem "candy" #not for now
+gem "mongo"
group :test do
gem "rspec"
View
@@ -2,11 +2,6 @@ GEM
remote: http://rubygems.org/
specs:
bson (1.0.7)
- bson_ext (1.0.7)
- candy (0.2.10)
- bson (>= 0.20.1)
- bson_ext (>= 0.20.1)
- mongo (>= 0.20.1)
mongo (1.0.8)
bson (>= 1.0.5)
rspec (1.3.0)
@@ -15,5 +10,5 @@ PLATFORMS
ruby
DEPENDENCIES
- candy
+ mongo
rspec
View
@@ -1,37 +1,37 @@
-require 'candy'
+require 'mongo'
+include Mongo
module Extra
- class Extras
- include Candy::Collection
- collects :extra
- end
class Extra
- include Candy::Piece
+
class << self
+ attr_accessor :db, :collection
def source opts={}
opts[:host] ||= "localhost"
- Candy.host = opts[:host]
opts[:port] ||= 27017
- Candy.port = opts[:port]
+ self.db = Mongo::Connection.new(opts[:host], opts[:port]).db("extraextra")
+ self.collection = db["extras"]
end
def !(category, user, text)
- extra = Extra.new
- extra.category = category
- extra.who_id = user.id
- extra.who_name = user.username
- extra.who_class = user.class
- extra.what = text
- extra.when = Time.now
+ collection.insert category: category,
+ who_id: user.id,
+ who_name: user.username,
+ who_class: user.class.to_s,
+ what: text,
+ when: Time.now.to_s
end
def read_all_about_it user
- Extras.new(:who_id => user.id, :who_class => user.class)
+
+ collection.find(who_id: user.id,
+ who_class: user.class.to_s
+ ).to_a
+
end
-
end
end
View
@@ -1,18 +1,20 @@
require_relative "../lib/extra.rb"
describe Extra::Extra do
- it "should exist" do
- extra = Extra::Extra.new
- end
-
describe "#source" do
it "should exist" do
Extra::Extra.respond_to?(:source).should == true
end
- it "should set Candy.host and Candy.port" do
- Candy.should_receive(:host=).with("localhost")
- Candy.should_receive(:port=).with(1337)
+ it "should set up a connection" do
+ obj = mock("Connection")
+ db = mock("db")
+ db.should_receive(:[])
+ obj.should_receive(:db).with("extraextra").and_return(db)
+ Mongo::Connection.should_receive(:new).with("localhost", 1337).and_return(obj)
+ Extra::Extra.should_receive(:db=).and_return(db)
+ Extra::Extra.should_receive(:db).and_return(db)
+
Extra::Extra.source :host => "localhost", :port => 1337
end
end
@@ -21,14 +23,9 @@
it "should take three args and save them" do
#holy setup batman
- extra = mock("Extra")
- Extra::Extra.should_receive(:new).and_return(extra)
- extra.should_receive(:category=).with(:breaking)
- extra.should_receive(:who_id=).with(1)
- extra.should_receive(:who_name=).with("steve")
- extra.should_receive(:who_class=).with('User')
- extra.should_receive(:what=).with("hit a home run")
- extra.should_receive(:when=)
+ collection = mock("collection")
+ Extra::Extra.should_receive(:collection).and_return(collection)
+ collection.should_receive(:insert).with(:category => :breaking, :who_id => 1, :who_name => "steve", :who_class => 'User', :what => "hit a home run", :when => Time.now.to_s)
user = mock("User", :id => 1, :class => 'User', :username => "steve")
Extra::Extra::! :breaking, user, "hit a home run"
@@ -43,9 +40,13 @@
it "should return Extras for the user" do
user = mock("User", :id => 1, :class => "User")
- Extra::Extras.should_receive(:new).with(:who_id => 1, :who_class => "User")
-
- Extra::Extra.read_all_about_it(user)
+ extra = mock("Extra")
+ collection = mock("collection")
+ collection.should_receive(:find).with(:who_id=>1, :who_class=>"User").and_return([extra])
+ Extra::Extra.should_receive(:collection).and_return(collection)
+
+ extras = Extra::Extra.read_all_about_it(user)
+ extras.should == [extra]
end
end
end
View
19 test.rb
@@ -0,0 +1,19 @@
+require './lib/extra/extra.rb'
+
+class User
+ attr_accessor :id, :username
+end
+
+steve = User.new
+steve.id = 1
+steve.username = "steve"
+
+Extra::Extra.source
+
+Extra::Extra.! :breaking, steve, "won at everything!"
+
+extras = Extra::Extra.read_all_about_it(steve)
+
+extras.each do |extra|
+ puts extra.to_s
+end

0 comments on commit 0c0b894

Please sign in to comment.