Module: CouchSurfer::Model::InstanceMethods
- Defined in:
- lib/couch_surfer/model.rb
Instance Method Summary collapse
-
#==(other_object) ⇒ Object
Is this OK?.
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#attachment_url(attachment_name) ⇒ Object
returns URL to fetch the attachment from.
-
#create_attachment(args = {}) ⇒ Object
creates a file attachment to the current doc.
-
#database ⇒ Object
returns the database used by this model’s class.
-
#delete_attachment(attachment_name) ⇒ Object
deletes a file attachment from the current doc.
-
#destroy ⇒ Object
Deletes the document from the database.
-
#has_attachment?(attachment_name) ⇒ Boolean
returns true if attachment_name exists.
- #id ⇒ Object
- #identity_field ⇒ Object
- #key?(key) ⇒ Boolean
- #new_document? ⇒ Boolean
-
#new_record? ⇒ Boolean
Rails-specific.
-
#read_attachment(attachment_name) ⇒ Object
reads the data from an attachment.
- #reload ⇒ Object
- #rev ⇒ Object
-
#save(bulk = false) ⇒ Object
Overridden to set the unique ID.
-
#save! ⇒ Object
Saves the document to the db using create or update.
- #to_json ⇒ Object
-
#to_param ⇒ Object
Rails-specific.
-
#update_attachment(args = {}) ⇒ Object
modifies a file attachment on the current doc.
-
#update_attributes(hash) ⇒ Object
Takes a hash as argument, and applies the values by using writer methods for each key.
-
#update_attributes_without_saving(hash) ⇒ Object
Takes a hash as argument, and applies the values by using writer methods for each key.
Instance Method Details
#==(other_object) ⇒ Object
Is this OK?
486 487 488 |
# File 'lib/couch_surfer/model.rb', line 486 def == other_object attributes == other_object.attributes end |
#[](key) ⇒ Object
461 462 463 |
# File 'lib/couch_surfer/model.rb', line 461 def [] key @attributes[key.to_s] end |
#[]=(key, value) ⇒ Object
457 458 459 |
# File 'lib/couch_surfer/model.rb', line 457 def []= key, value @attributes[key.to_s] = value end |
#attachment_url(attachment_name) ⇒ Object
returns URL to fetch the attachment from
564 565 566 567 |
# File 'lib/couch_surfer/model.rb', line 564 def () return unless () "#{database.root}/#{self.id}/#{}" end |
#create_attachment(args = {}) ⇒ Object
creates a file attachment to the current doc
528 529 530 531 532 533 534 535 |
# File 'lib/couch_surfer/model.rb', line 528 def (args={}) raise ArgumentError unless args[:file] && args[:name] return if (args[:name]) self['_attachments'] ||= {} (args) rescue ArgumentError => e raise ArgumentError, 'You must specify :file and :name' end |
#database ⇒ Object
returns the database used by this model’s class
433 434 435 |
# File 'lib/couch_surfer/model.rb', line 433 def database self.class.database end |
#delete_attachment(attachment_name) ⇒ Object
deletes a file attachment from the current doc
553 554 555 556 |
# File 'lib/couch_surfer/model.rb', line 553 def () return unless self['_attachments'] self['_attachments'].delete end |
#destroy ⇒ Object
Deletes the document from the database. Runs the :destroy callbacks. Removes the _id
and _rev
fields, preparing the document to be saved to a new _id
.
518 519 520 521 522 523 524 525 |
# File 'lib/couch_surfer/model.rb', line 518 def destroy result = database.delete_doc self if result['ok'] self['_rev'] = nil self['_id'] = nil end result['ok'] end |
#has_attachment?(attachment_name) ⇒ Boolean
returns true if attachment_name exists
559 560 561 |
# File 'lib/couch_surfer/model.rb', line 559 def () !!(self['_attachments'] && self['_attachments'][] && !self['_attachments'][].empty?) end |
#id ⇒ Object
473 474 475 |
# File 'lib/couch_surfer/model.rb', line 473 def id @attributes.id end |
#identity_field ⇒ Object
477 478 479 |
# File 'lib/couch_surfer/model.rb', line 477 def identity_field [CouchSurfer::Model, :id] end |
#key?(key) ⇒ Boolean
465 466 467 |
# File 'lib/couch_surfer/model.rb', line 465 def key? key @attributes.key? key end |
#new_document? ⇒ Boolean
469 470 471 |
# File 'lib/couch_surfer/model.rb', line 469 def new_document? !@attributes['_rev'] end |
#new_record? ⇒ Boolean
Rails-specific
428 429 430 |
# File 'lib/couch_surfer/model.rb', line 428 def new_record? id.nil? end |
#read_attachment(attachment_name) ⇒ Object
reads the data from an attachment
538 539 540 |
# File 'lib/couch_surfer/model.rb', line 538 def () Base64.decode64(database.(self, )) end |
#reload ⇒ Object
490 491 492 493 494 |
# File 'lib/couch_surfer/model.rb', line 490 def reload reloaded_doc = self.class.get(id) @attributes = reloaded_doc.attributes self end |
#rev ⇒ Object
481 482 483 |
# File 'lib/couch_surfer/model.rb', line 481 def rev @attributes.rev end |
#save(bulk = false) ⇒ Object
Overridden to set the unique ID.
501 502 503 504 505 506 507 |
# File 'lib/couch_surfer/model.rb', line 501 def save bulk = false if new_document? create(bulk) else update(bulk) end end |
#save! ⇒ Object
Saves the document to the db using create or update. Raises an exception if the document is not saved properly.
511 512 513 |
# File 'lib/couch_surfer/model.rb', line 511 def save! raise "#{self.inspect} failed to save" unless self.save end |
#to_json ⇒ Object
569 570 571 |
# File 'lib/couch_surfer/model.rb', line 569 def to_json attributes.to_json end |
#to_param ⇒ Object
Rails-specific
423 424 425 |
# File 'lib/couch_surfer/model.rb', line 423 def to_param id end |
#update_attachment(args = {}) ⇒ Object
modifies a file attachment on the current doc
543 544 545 546 547 548 549 550 |
# File 'lib/couch_surfer/model.rb', line 543 def (args={}) raise ArgumentError unless args[:file] && args[:name] return unless (args[:name]) (args[:name]) (args) rescue ArgumentError => e raise ArgumentError, 'You must specify :file and :name' end |
#update_attributes(hash) ⇒ Object
Takes a hash as argument, and applies the values by using writer methods for each key. Raises a NoMethodError if the corresponding methods are missing. In case of error, no attributes are changed.
452 453 454 455 |
# File 'lib/couch_surfer/model.rb', line 452 def update_attributes hash update_attributes_without_saving hash save end |
#update_attributes_without_saving(hash) ⇒ Object
Takes a hash as argument, and applies the values by using writer methods for each key. It doesn’t save the document at the end. Raises a NoMethodError if the corresponding methods are missing. In case of error, no attributes are changed.
440 441 442 443 444 445 446 447 |
# File 'lib/couch_surfer/model.rb', line 440 def update_attributes_without_saving hash hash.each do |k, v| raise NoMethodError, "#{k}= method not available, use key_accessor or key_writer :#{k}" unless self.respond_to?("#{k}=") end hash.each do |k, v| @attributes[k] = v end end |