Commit 8c38a003235d67bb7d50055b28b338517339fa67
- Diff rendering mode:
- inline
- side by side
README.txt
(29 / 9)
|   | |||
| 137 | 137 | >> fast_car.save | |
| 138 | 138 | => {:revision=>"3335068490", :id=>"B1D0576DA2E7846550DCD61DCC8CDAE4"} | |
| 139 | 139 | ||
| 140 | We could also use the alias get instead of get_by_id | ||
| 141 | >> loading_the_fast_car = RacingCar.get_by_id(db_address, "B1D0576DA2E7846550DCD61DCC8CDAE4") | ||
| 140 | >> loading_the_fast_car = RacingCar.get(db_address, "B1D0576DA2E7846550DCD61DCC8CDAE4") | ||
| 142 | 141 | => #<RacingCar:0x129fddc @races_won=1, @revision="3335068490", @location="http://localhost:5984/mydb", @id="B1D0576DA2E7846550DCD61DCC8CDAE4"> | |
| 143 | 142 | ||
| 144 | 143 | >> loading_the_fast_car.revision == fast_car.revision && loading_the_fast_car.id == fast_car.id | |
| … | … | ||
| 160 | 160 | >> my_balloon.save | |
| 161 | 161 | => {:revision=>"1118628227", :id=>"ASD21"} | |
| 162 | 162 | ||
| 163 | >> other_balloon = Balloon.get_by_id("123") | ||
| 163 | >> other_balloon = Balloon.get("123") | ||
| 164 | 164 | => #<Balloon:0x12a6024 @id="123", @location="http://localhost:5984/mydb", @revision="1234"> | |
| 165 | 165 | ||
| 166 | 166 | ||
| … | … | ||
| 222 | 222 | >> the_pizza.save("http://localhost:5984/mydb") | |
| 223 | 223 | => {:revision=>"3412312521", :id=>"88781A212BB4676B48B352B209A4D979"} | |
| 224 | 224 | ||
| 225 | >> hans_pizza = Pizza.get_by_id("88781A212BB4676B48B352B209A4D979","http://localhost:5984/mydb") | ||
| 225 | >> hans_pizza = Pizza.get("88781A212BB4676B48B352B209A4D979","http://localhost:5984/mydb") | ||
| 226 | 226 | => #<Pizza:0x12989c4 @id="88781A212BB4676B48B352B209A4D979", @slices_left=1, @location="http://localhost:5984/mydb", @owner_name="Hans", @revision="3412312521", @my_pizza=false> | |
| 227 | 227 | ||
| 228 | 228 | >> hans_pizza.my_pizza? | |
| … | … | ||
| 237 | 237 | => {:revision=>"2414021920", :id=>"88781A212BB4676B48B352B209A4D979"} | |
| 238 | 238 | ||
| 239 | 239 | ||
| 240 | It is possible to load all objects of a certain type with the +all+ parameter | ||
| 241 | |||
| 242 | require 'rubygems' | ||
| 243 | require 'couch_object' | ||
| 244 | |||
| 245 | class Balloon | ||
| 246 | include CouchObject::Persistable | ||
| 247 | database 'http://localhost:5984/mydb' | ||
| 248 | end | ||
| 249 | |||
| 250 | >> Balloon.new.save | ||
| 251 | >> Balloon.new.save | ||
| 252 | >> Balloon.new.save | ||
| 253 | >> Balloon.new.save | ||
| 254 | >> Balloon.new.save | ||
| 255 | >> Balloon.new.save | ||
| 256 | >> balloons = Balloon.all | ||
| 257 | >> balloons.size | ||
| 258 | => 6 | ||
| 259 | |||
| 260 | |||
| 240 | 261 | You can also have timestamps added automatically to your class using the +add_timestamp_for+ method which takes the values +:on_create+ and +:on_update+. | |
| 241 | 262 | ||
| 242 | 263 | require 'rubygems' | |
| … | … | ||
| 330 | 330 | >> my_home.save | |
| 331 | 331 | => {:revision=>"519758193", :id=>"3E8E2F7A0AE7DACC7D537ECE5220C6FF"} | |
| 332 | 332 | ||
| 333 | >> your_home = House.get_by_id("3E8E2F7A0AE7DACC7D537ECE5220C6FF") | ||
| 333 | >> your_home = House.get("3E8E2F7A0AE7DACC7D537ECE5220C6FF") | ||
| 334 | 334 | >> your_home.owner.name | |
| 335 | 335 | => "Sebastian" | |
| 336 | 336 | ||
| … | … | ||
| 404 | 404 | => {:revision=>"1793606849", :id=>"C65B9837CD572FC66931F12392A3181E"} | |
| 405 | 405 | ||
| 406 | 406 | ||
| 407 | >> home_from_db = House.get_by_id("C65B9837CD572FC66931F12392A3181E") | ||
| 407 | >> home_from_db = House.get("C65B9837CD572FC66931F12392A3181E") | ||
| 408 | 408 | => #<House:0x1229790 @revision="1793606849", @id="C65B9837CD572FC66931F12392A3181E", @location="http://localhost:5984/mydb"> | |
| 409 | 409 | ||
| 410 | 410 | All relations are lazily loaded the first time they are needed. | |
| … | … | ||
| 504 | 504 | ||
| 505 | 505 | Example: | |
| 506 | 506 | ||
| 507 | >> home_from_db = House.get_by_id("C65B9837CD572FC66931F12392A3181E") | ||
| 507 | >> home_from_db = House.get("C65B9837CD572FC66931F12392A3181E") | ||
| 508 | 508 | => #<House:0x1229790 @revision="1793606849", @id="C65B9837CD572FC66931F12392A3181E", @location="http://localhost:5984/mydb"> | |
| 509 | 509 | ||
| 510 | 510 | Make sure the relations are NOT loaded | |
| … | … | ||
| 538 | 538 | >> my_home.save | |
| 539 | 539 | => {:id=>"4CDC2355A21DDC4D8887882540A84A14", :revision=>"3659215175"} | |
| 540 | 540 | ||
| 541 | >> loaded_home = House.get_by_id("4CDC2355A21DDC4D8887882540A84A14") | ||
| 541 | >> loaded_home = House.get("4CDC2355A21DDC4D8887882540A84A14") | ||
| 542 | 542 | ||
| 543 | 543 | We want to add a book to the "has" relation without loading the already saved book and bed | |
| 544 | 544 | >> loaded_home.do_not_load_has_many_relations | |
| … | … | ||
| 684 | 684 | instance_WITHOUT_smart_save_activated_2 = NotSoSmartClass.get("dong") | |
| 685 | 685 | ||
| 686 | 686 | The same as in the example above can be achieved with the convenience | |
| 687 | method +get_with_smart_save+ which takes the same parameters as +get_by_id+. | ||
| 687 | method +get_with_smart_save+ which takes the same parameters as +get+. | ||
| 688 | 688 | The same example could therefor also be written as: | |
| 689 | 689 | ||
| 690 | 690 | class NotSoSmartClass |
|   | |||
| 101 | 101 | ||
| 102 | 102 | # | |
| 103 | 103 | # Is raised when one the CouchDB returns an error which isn't covered | |
| 104 | # by one of the functions above | ||
| 104 | # by one of the error classes above | ||
| 105 | 105 | # | |
| 106 | 106 | class CouchDBError < StandardError | |
| 107 | 107 | end |
lib/couch_object/persistable.rb
(62 / 1)
|   | |||
| 208 | 208 | ||
| 209 | 209 | end | |
| 210 | 210 | ||
| 211 | # | ||
| 212 | # Loads all objects of its own type from the database | ||
| 213 | # It uses the get_from_view method | ||
| 214 | # | ||
| 215 | # Takes: | ||
| 216 | # * db_uri (string) as the URI to the database. | ||
| 217 | # Defaults to the db_uri set in the class if defined | ||
| 218 | # | ||
| 219 | # Returns: | ||
| 220 | # * An array of all objects of the type of the object | ||
| 221 | # the +get_all+ method is called on | ||
| 222 | # | ||
| 223 | # Raises: | ||
| 224 | # * CouchObject::Errors::StandardError if the view for loading | ||
| 225 | # the objects doesn't exist and can't be created. | ||
| 226 | # | ||
| 227 | def get_all(db_uri = self.location) | ||
| 228 | |||
| 229 | raise CouchObject::Errors::NoDatabaseLocationSet if db_uri.nil? | ||
| 230 | |||
| 231 | view_name = "couch_object_load_objects" | ||
| 232 | |||
| 233 | results = [] | ||
| 234 | |||
| 235 | begin | ||
| 236 | |||
| 237 | results = get_from_view("_view/#{view_name}/of_type_self", \ | ||
| 238 | {:key => self.to_s, | ||
| 239 | :db_uri => db_uri} ) | ||
| 240 | |||
| 241 | rescue CouchObject::Errors::MissingView, CouchObject::Errors::CouchDBError | ||
| 242 | |||
| 243 | # The view doesn't exist. It means this is the first | ||
| 244 | # time this script is used for a given database, or the user | ||
| 245 | # has deleted the view | ||
| 246 | view_code_query = JSON.unparse( | ||
| 247 | { | ||
| 248 | "_id" => "_design/#{view_name}", | ||
| 249 | "language" => "text/javascript", | ||
| 250 | "views" => | ||
| 251 | { | ||
| 252 | "of_type_self" => "function(doc){map(doc.class, doc);}" | ||
| 253 | } | ||
| 254 | } | ||
| 255 | ) | ||
| 256 | |||
| 257 | db = CouchObject::Database.open(db_uri) | ||
| 258 | |||
| 259 | if (response = db.put("_design%2F#{view_name}", \ | ||
| 260 | view_code_query)) | ||
| 261 | results = get_all(db_uri) | ||
| 262 | else | ||
| 263 | raise CouchObject::Errors::DatabaseSaveFailed, "Couldn't create the view..." | ||
| 264 | end | ||
| 265 | end | ||
| 266 | |||
| 267 | return results | ||
| 268 | |||
| 269 | end | ||
| 270 | alias all get_all | ||
| 271 | |||
| 211 | 272 | protected | |
| 212 | 273 | # | |
| 213 | 274 | # This recursive method initializes new instances of self and | |
| … | … | ||
| 964 | 964 | view_code_query)) | |
| 965 | 965 | results = couch_load_has_many_relations(which_relation) | |
| 966 | 966 | else | |
| 967 | raise CouchObject::Errors::StandardError, "Couldn't create the view..." | ||
| 967 | raise CouchObject::Errors::DatabaseSaveFailed, "Couldn't create the view..." | ||
| 968 | 968 | end | |
| 969 | 969 | end | |
| 970 | 970 |
spec/persistable/loading.rb
(4 / 1)
|   | |||
| 290 | 290 | ||
| 291 | 291 | end | |
| 292 | 292 | ||
| 293 | |||
| 294 | 293 | it "should be able to return instances from a view with different objects" do | |
| 295 | 294 | response = HTTPResponse.new(@view_content_mixed) | |
| 296 | 295 | @db.should_receive(:get).with("foo").and_return(response) | |
| … | … | ||
| 332 | 332 | {:db_uri => "foo"})}. | |
| 333 | 333 | should raise_error(CouchObject::Errors::MissingView) | |
| 334 | 334 | ||
| 335 | end | ||
| 336 | |||
| 337 | it "should be able to load all objects of a given type" do | ||
| 338 | |||
| 335 | 339 | end | |
| 336 | 340 | ||
| 337 | 341 | end |
spec/persistable/loading_spec.rb
(48 / 2)
|   | |||
| 1 | 1 | require File.dirname(__FILE__) + '/persistable_helper.rb' | |
| 2 | 2 | ||
| 3 | describe CouchObject::Persistable, "for loading objects:" do | ||
| 3 | describe CouchObject::Persistable, "for loading objects:" do |variable| | ||
| 4 | |||
| 5 | end | ||
| 4 | 6 | before(:each) do | |
| 5 | 7 | @bike = Bike.new | |
| 6 | 8 | @with_location = WithStorageLocation.new | |
| … | … | ||
| 104 | 104 | ||
| 105 | 105 | end | |
| 106 | 106 | ||
| 107 | it "should have the alias 'get' for get_by_id" do | ||
| 108 | Bike.respond_to?(:get).should == true | ||
| 109 | end | ||
| 107 | 110 | end | |
| 108 | 111 | ||
| 109 | 112 | describe CouchObject::Persistable, "for loading object with subobject:" do | |
| … | … | ||
| 295 | 295 | ||
| 296 | 296 | end | |
| 297 | 297 | ||
| 298 | |||
| 299 | 298 | it "should be able to return instances from a view with different objects" do | |
| 300 | 299 | response = HTTPResponse.new(@view_content_mixed) | |
| 301 | 300 | @db.should_receive(:get).with("foo").and_return(response) | |
| … | … | ||
| 338 | 338 | should raise_error(CouchObject::Errors::MissingView) | |
| 339 | 339 | ||
| 340 | 340 | end | |
| 341 | end | ||
| 341 | 342 | ||
| 343 | describe CouchObject::Persistable, "#get_all" do | ||
| 344 | before(:each) do | ||
| 345 | @db = mock("mock db") | ||
| 346 | CouchObject::Database.stub!(:open).and_return(@db) | ||
| 347 | end | ||
| 348 | |||
| 349 | it "should have a method to load all objects of its own type" do | ||
| 350 | Bike.respond_to?(:get_all).should == true | ||
| 351 | end | ||
| 352 | it "should raise an error for objects that don't have a database location" do | ||
| 353 | lambda { Bike.get_all }.should raise_error(CouchObject::Errors::NoDatabaseLocationSet) | ||
| 354 | end | ||
| 355 | it "should get objects from the get_from_view method" do | ||
| 356 | Bike.should_receive(:get_from_view).and_return([]) | ||
| 357 | Bike.get_all("foo_db").should == [] | ||
| 358 | end | ||
| 359 | it "should return an error if it can't create a new view" do | ||
| 360 | Bike.should_receive(:get_from_view).once.with("_view/couch_object_load_objects/of_type_self", {:key => "Bike", :db_uri => "foo_location"}).and_raise(CouchObject::Errors::MissingView) | ||
| 361 | # DB is stubbed | ||
| 362 | @db.should_receive(:put).and_return(false) | ||
| 363 | lambda{Bike.get_all("foo_location")}.should raise_error(CouchObject::Errors::DatabaseSaveFailed) | ||
| 364 | end | ||
| 365 | # it "should create a new view if it doesn't exist" do | ||
| 366 | # Bike.should_receive(:get_from_view).once.with("_view/couch_object_load_objects/of_type_self", {:key => "Bike", :db_uri => "foo_location"}).and_raise(CouchObject::Errors::MissingView) | ||
| 367 | # | ||
| 368 | # # DB is stubbed | ||
| 369 | # @db.should_receive(:put).and_return(true) | ||
| 370 | # # TODO | ||
| 371 | # # then it calls itself again, which I can't stub out... and since it | ||
| 372 | # # doesn't actually create a view in the spec it fails again... | ||
| 373 | # # and there we have an infinite loop... somebody fix please? | ||
| 374 | # Bike.get_all("foo_location").should == [] | ||
| 375 | # end | ||
| 376 | it "should return an empty array if no objects can be found" do | ||
| 377 | Bike.should_receive(:get_from_view).once.with("_view/couch_object_load_objects/of_type_self", {:key => "Bike", :db_uri => "foo_location"}).and_return([]) | ||
| 378 | Bike.get_all("foo_location").should == [] | ||
| 379 | end | ||
| 380 | it "should have an alias 'all' for get_all" do | ||
| 381 | Bike.respond_to?(:all).should == true | ||
| 382 | end | ||
| 383 | |||
| 342 | 384 | end |

