Module: CouchRest::Model::CastedModel

Extended by:
ActiveSupport::Concern
Defined in:
lib/couchrest/model/casted_model.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



26
27
28
# File 'lib/couchrest/model/casted_model.rb', line 26

def [] key
  super(key.to_s)
end

#[]=(key, value) ⇒ Object



22
23
24
# File 'lib/couchrest/model/casted_model.rb', line 22

def []= key, value
  super(key.to_s, value)
end

#base_docObject

Gets a reference to the top level extended document that a model is saved inside of



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

def base_doc
  return nil unless @casted_by
  @casted_by.base_doc
end

#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/casted_model.rb', line 50

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

#initialize(keys = {}) ⇒ Object

Raises:

  • (StandardError)


16
17
18
19
20
# File 'lib/couchrest/model/casted_model.rb', line 16

def initialize(keys = {})
  raise StandardError unless self.is_a? Hash
  prepare_all_attributes(keys)
  super()
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/casted_model.rb', line 39

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

#persisted?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/couchrest/model/casted_model.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/casted_model.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