Module: CouchRest::Model::Embeddable

Extended by:
ActiveSupport::Concern
Includes:
Attributes
Defined in:
lib/couchrest/model/embeddable.rb

Instance Method Summary collapse

Instance Method Details

#idObject Also known as: to_key, to_param

The to_param method is needed for rails to generate resourceful routes. In your controller, remember that it’s actually the id of the document.



50
51
52
53
# File 'lib/couchrest/model/embeddable.rb', line 50

def id
  return nil if base_doc.nil?
  base_doc.id
end

#initialize(keys = {}, options = {}) ⇒ Object

Initialize a new Casted Model. Accepts the same options as CouchRest::Model::Base for preparing and initializing attributes.



31
32
33
34
35
# File 'lib/couchrest/model/embeddable.rb', line 31

def initialize(keys = {}, options = {})
  super()
  prepare_all_attributes(keys, options)
  run_callbacks(:initialize) { self }
end

#new?Boolean Also known as: new_record?

False if the casted model has already been saved in the containing document

Returns:

  • (Boolean)


39
40
41
# File 'lib/couchrest/model/embeddable.rb', line 39

def new?
  casted_by.nil? ? true : casted_by.new?
end

#persisted?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/couchrest/model/embeddable.rb', line 44

def persisted?
  !new?
end

#update_attributes_without_saving(hash) ⇒ Object Also known as: attributes=

Sets the attributes from a hash



58
59
60
61
62
63
64
65
# File 'lib/couchrest/model/embeddable.rb', line 58

def update_attributes_without_saving(hash)
  hash.each do |k, v|
    raise NoMethodError, "#{k}= method not available, use property :#{k}" unless self.respond_to?("#{k}=")
  end      
  hash.each do |k, v|
    self.send("#{k}=",v)
  end
end