| 1 |
module CouchObject |
| 2 |
module Persistable |
| 3 |
def self.included(klazz) |
| 4 |
klazz.extend(ClassMethods) |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
klazz.class_eval do |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
def self.couch_object_timestamp_on_update?; false; end |
| 21 |
def self.couch_object_timestamp_on_create?; false; end |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
def self.add_timestamp_for(*timestamp_actions) |
| 39 |
timestamp_actions.each do |action| |
| 40 |
case action |
| 41 |
when :on_create |
| 42 |
self.class_eval do |
| 43 |
def self.couch_object_timestamp_on_create?; true; end |
| 44 |
end |
| 45 |
when :on_update |
| 46 |
self.class_eval do |
| 47 |
def self.couch_object_timestamp_on_update?; true; end |
| 48 |
end |
| 49 |
end |
| 50 |
end |
| 51 |
end |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
def self.location; @couch_object_class_storage_location ||= nil; end |
| 89 |
def location; @location; end |
| 90 |
alias storage_location location |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
def self.database(db_uri) |
| 107 |
@couch_object_class_storage_location = db_uri |
| 108 |
self.instance_eval do |
| 109 |
define_method("location") do |
| 110 |
@location ||= db_uri |
| 111 |
end |
| 112 |
end |
| 113 |
end |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
def use_smart_save; false; end |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
def self.smart_save |
| 138 |
self.instance_eval do |
| 139 |
define_method("use_smart_save") do |
| 140 |
true |
| 141 |
end |
| 142 |
end |
| 143 |
end |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
def self.deactivate_smart_save |
| 157 |
self.instance_eval do |
| 158 |
define_method("use_smart_save") do |
| 159 |
false |
| 160 |
end |
| 161 |
end |
| 162 |
end |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
def has_many; []; end |
| 172 |
def has_one; []; end |
| 173 |
def has; []; end |
| 174 |
def belongs_to; []; end |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
def self.has_many(what_it_has = nil) |
| 199 |
raise CouchObject::Errors::HasManyAssociationError if what_it_has == nil |
| 200 |
|
| 201 |
@couch_object_has_many ||= [] |
| 202 |
@couch_object_has_many << what_it_has unless \ |
| 203 |
@couch_object_has_many.include?(what_it_has) |
| 204 |
|
| 205 |
self.instance_eval do |
| 206 |
|
| 207 |
has_many_object_variable = \ |
| 208 |
"@couch_object_#{what_it_has.to_s}" |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
define_method(what_it_has.to_s) do |
| 217 |
eval("#{has_many_object_variable}.nil? ? " + \ |
| 218 |
"#{has_many_object_variable} = " + \ |
| 219 |
"couch_load_has_many_relations(\"#{what_it_has}\") : " + \ |
| 220 |
"#{has_many_object_variable}") |
| 221 |
end |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
all_the_things_it_has = @couch_object_has_many |
| 233 |
define_method("has_many") do |
| 234 |
|
| 235 |
what_is_has_output = [] |
| 236 |
self.send(:has).each do |has| |
| 237 |
what_is_has_output << has \ |
| 238 |
unless has.to_s[0..7] == "has_one_" |
| 239 |
end |
| 240 |
what_is_has_output |
| 241 |
end |
| 242 |
define_method("has") do |
| 243 |
all_the_things_it_has |
| 244 |
end |
| 245 |
end |
| 246 |
|
| 247 |
end |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
def self.belongs_to(what_it_belongs_to = nil, as = nil) |
| 275 |
raise CouchObject::Errors::BelongsToAssociationError \ |
| 276 |
if what_it_belongs_to.nil? || as.nil? |
| 277 |
|
| 278 |
@couch_object_what_it_belongs_to ||= [] |
| 279 |
@couch_object_what_it_belongs_to << what_it_belongs_to unless \ |
| 280 |
@couch_object_what_it_belongs_to.include?(what_it_belongs_to) |
| 281 |
|
| 282 |
self.instance_eval do |
| 283 |
|
| 284 |
belongs_to_object_variable = \ |
| 285 |
"@couch_object_#{what_it_belongs_to.to_s}" |
| 286 |
|
| 287 |
|
| 288 |
define_method(what_it_belongs_to.to_s) do |
| 289 |
eval("#{belongs_to_object_variable} ||= " + \ |
| 290 |
" couch_load_belongs_to_relation(\"#{as[:as]}\")") |
| 291 |
end |
| 292 |
|
| 293 |
|
| 294 |
define_method("#{what_it_belongs_to.to_s}=") do |object_to_add| |
| 295 |
|
| 296 |
|
| 297 |
if object_to_add.respond_to?("has_one_#{as[:as]}") || \ |
| 298 |
eval("#{belongs_to_object_variable}" + \ |
| 299 |
".respond_to?(:has_one_#{as[:as]})") |
| 300 |
is_a_has_many_relationship = false |
| 301 |
else |
| 302 |
is_a_has_many_relationship = true |
| 303 |
end |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
original_state_of_load_belongs_to_relations = \ |
| 311 |
@do_not_load_belongs_to_relations |
| 312 |
@do_not_load_belongs_to_relations = true |
| 313 |
|
| 314 |
if is_a_has_many_relationship |
| 315 |
|
| 316 |
|
| 317 |
eval("#{belongs_to_object_variable}." \ |
| 318 |
+ "send(:end_relationsship_with, self, \"#{as[:as]}\" ) " \ |
| 319 |
+ "unless #{belongs_to_object_variable} == nil") |
| 320 |
|
| 321 |
|
| 322 |
instance_variable_set("#{belongs_to_object_variable}", \ |
| 323 |
object_to_add) |
| 324 |
|
| 325 |
|
| 326 |
self.add_relation_to_master(as[:as]) if object_to_add |
| 327 |
|
| 328 |
else |
| 329 |
|
| 330 |
|
| 331 |
self.set_has_one_relation_to_master(as[:as], nil) |
| 332 |
|
| 333 |
|
| 334 |
instance_variable_set("#{belongs_to_object_variable}", \ |
| 335 |
object_to_add) |
| 336 |
|
| 337 |
|
| 338 |
self.set_has_one_relation_to_master(as[:as], self) \ |
| 339 |
if object_to_add |
| 340 |
|
| 341 |
end |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
@do_not_load_belongs_to_relations = \ |
| 346 |
original_state_of_load_belongs_to_relations |
| 347 |
|
| 348 |
end |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
define_method("#{what_it_belongs_to.to_s}" + \ |
| 353 |
"_without_call_back=") do |object_to_add| |
| 354 |
|
| 355 |
|
| 356 |
instance_variable_set("#{belongs_to_object_variable}", \ |
| 357 |
object_to_add) |
| 358 |
end |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
return_value_for_function = @couch_object_what_it_belongs_to |
| 370 |
define_method("belongs_to") do |
| 371 |
return_value_for_function |
| 372 |
end |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
define_method("belongs_to_#{what_it_belongs_to}_as") do |
| 386 |
as[:as] |
| 387 |
end |
| 388 |
|
| 389 |
end |
| 390 |
|
| 391 |
end |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
def self.has_one(what_it_has = nil) |
| 399 |
raise CouchObject::Errors::HasOneAssociationError if what_it_has == nil |
| 400 |
|
| 401 |
related_has_many_relationship = "has_one_#{what_it_has.to_s}".to_sym |
| 402 |
|
| 403 |
|
| 404 |
self.send(:has_many, related_has_many_relationship) |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
define_method(what_it_has) do |
| 410 |
self.send(related_has_many_relationship).first |
| 411 |
end |
| 412 |
|
| 413 |
define_method("#{what_it_has}=") do |new_relation| |
| 414 |
|
| 415 |
self.send(related_has_many_relationship). |
| 416 |
remove(self.send(related_has_many_relationship).first) \ |
| 417 |
unless self.send(related_has_many_relationship) == [] |
| 418 |
|
| 419 |
|
| 420 |
self.send(related_has_many_relationship).disable_call_back_on_add |
| 421 |
|
| 422 |
if new_relation |
| 423 |
|
| 424 |
|
| 425 |
self.send(related_has_many_relationship) << new_relation \ |
| 426 |
unless new_relation == nil |
| 427 |
|
| 428 |
|
| 429 |
what_it_belongs_to = define_relationship_name(new_relation) |
| 430 |
|
| 431 |
new_relation. |
| 432 |
send("#{what_it_belongs_to}_without_call_back=", self) |
| 433 |
|
| 434 |
end |
| 435 |
|
| 436 |
|
| 437 |
self.send(related_has_many_relationship).enable_call_back_on_add |
| 438 |
|
| 439 |
end |
| 440 |
|
| 441 |
define_method("has_one") do |
| 442 |
what_is_has_output = [] |
| 443 |
self.send(:has).each do |has| |
| 444 |
what_is_has_output << has.to_s[8..-1].to_sym \ |
| 445 |
if has.to_s[0..7] == "has_one_" |
| 446 |
end |
| 447 |
what_is_has_output |
| 448 |
end |
| 449 |
|
| 450 |
end |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
def define_relationship_name(other_object) |
| 467 |
|
| 468 |
belongs_to_relationship_name = nil |
| 469 |
|
| 470 |
other_object.send(:belongs_to).each do |what_it_belongs_to| |
| 471 |
|
| 472 |
name_of_relation_in_master = other_object. |
| 473 |
send("belongs_to_#{what_it_belongs_to}_as".to_sym) |
| 474 |
|
| 475 |
return what_it_belongs_to.to_s \ |
| 476 |
if self.respond_to?(name_of_relation_in_master) |
| 477 |
|
| 478 |
end |
| 479 |
|
| 480 |
|
| 481 |
raise "The master class #{self} doesn't have a relation" + \ |
| 482 |
" matching the relation defined in the child class " \ |
| 483 |
if belongs_to_relationship_name == nil |
| 484 |
|
| 485 |
end |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
def add_relation_to_master(relation_name) |
| 493 |
|
| 494 |
if master_class = get_master_for_relation(relation_name) |
| 495 |
|
| 496 |
masters_objects_relations = \ |
| 497 |
master_class.send(relation_name) |
| 498 |
|
| 499 |
if masters_objects_relations == [] |
| 500 |
masters_objects_relations << self |
| 501 |
else |
| 502 |
unless masters_objects_relations.include?(self) |
| 503 |
masters_objects_relations << self |
| 504 |
end |
| 505 |
end |
| 506 |
|
| 507 |
end |
| 508 |
|
| 509 |
end |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
def set_has_one_relation_to_master(relation_name, to_what) |
| 517 |
|
| 518 |
if master_class = get_master_for_relation(relation_name) |
| 519 |
|
| 520 |
|
| 521 |
master_class.send("#{relation_name}=", to_what) |
| 522 |
|
| 523 |
end |
| 524 |
|
| 525 |
end |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
def end_has_one_relation_to_master(relation_name) |
| 534 |
|
| 535 |
set_has_one_relation_to_master(relation_name, nil) |
| 536 |
|
| 537 |
end |
| 538 |
|
| 539 |
|
| 540 |
def get_master_for_relation(relation_name) |
| 541 |
|
| 542 |
accessor_for_what_it_belongs_to = nil |
| 543 |
self.send(:belongs_to).each do |what_it_belongs_to| |
| 544 |
|
| 545 |
|
| 546 |
find_string = "belongs_to_#{what_it_belongs_to}_as" |
| 547 |
accessor_for_what_it_belongs_to = what_it_belongs_to \ |
| 548 |
if self.send(find_string) == relation_name |
| 549 |
end |
| 550 |
|
| 551 |
return nil if accessor_for_what_it_belongs_to == nil |
| 552 |
|
| 553 |
master_class = self.send(accessor_for_what_it_belongs_to) |
| 554 |
return nil if master_class == nil |
| 555 |
|
| 556 |
raise "The master class doesn't have a matching relation " + \ |
| 557 |
"defined" unless master_class.respond_to?(relation_name) |
| 558 |
|
| 559 |
return master_class |
| 560 |
|
| 561 |
end |
| 562 |
|
| 563 |
|
| 564 |
end |
| 565 |
end |
| 566 |
|
| 567 |
end |
| 568 |
end |