Module: CouchSurfer::Model::InstanceMethods

Defined in:
lib/couch_surfer/model.rb

Instance Method Summary collapse

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 attachment_url(attachment_name)
  return unless has_attachment?(attachment_name)
  "#{database.root}/#{self.id}/#{attachment_name}"
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 create_attachment(args={})
  raise ArgumentError unless args[:file] && args[:name]
  return if has_attachment?(args[:name])
  self['_attachments'] ||= {}
  set_attachment_attr(args)
rescue ArgumentError => e
  raise ArgumentError, 'You must specify :file and :name'
end

#databaseObject

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 delete_attachment(attachment_name)
  return unless self['_attachments']
  self['_attachments'].delete attachment_name
end

#destroyObject

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

Returns:

  • (Boolean)


559
560
561
# File 'lib/couch_surfer/model.rb', line 559

def has_attachment?(attachment_name)
  !!(self['_attachments'] && self['_attachments'][attachment_name] && !self['_attachments'][attachment_name].empty?)
end

#idObject



473
474
475
# File 'lib/couch_surfer/model.rb', line 473

def id
  @attributes.id
end

#identity_fieldObject



477
478
479
# File 'lib/couch_surfer/model.rb', line 477

def identity_field
  [CouchSurfer::Model, :id]
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


465
466
467
# File 'lib/couch_surfer/model.rb', line 465

def key? key
  @attributes.key? key
end

#new_document?Boolean

Returns:

  • (Boolean)


469
470
471
# File 'lib/couch_surfer/model.rb', line 469

def new_document?
  !@attributes['_rev']
end

#new_record?Boolean

Rails-specific

Returns:

  • (Boolean)


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 read_attachment(attachment_name)
  Base64.decode64(database.fetch_attachment(self, attachment_name))
end

#reloadObject



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

#revObject



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_jsonObject



569
570
571
# File 'lib/couch_surfer/model.rb', line 569

def to_json
  attributes.to_json
end

#to_paramObject

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 update_attachment(args={})
  raise ArgumentError unless args[:file] && args[:name]
  return unless has_attachment?(args[:name])
  delete_attachment(args[:name])
  set_attachment_attr(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