Module: CouchRest::Mixins::Attributes

Included in:
ExtendedDocument
Defined in:
lib/couchrest/mixins/attributes.rb

Instance Method Summary collapse

Instance Method Details

#prepare_all_attributes(doc = {}, options = {}) ⇒ Object

Support for handling attributes

This would be better in the properties file, but due to scoping issues this is not yet possible.



11
12
13
14
15
16
17
18
19
# File 'lib/couchrest/mixins/attributes.rb', line 11

def prepare_all_attributes(doc = {}, options = {})
  apply_all_property_defaults
  if options[:directly_set_attributes]
    directly_set_read_only_attributes(doc)          
  else
    remove_protected_attributes(doc)
  end
  directly_set_attributes(doc) unless doc.nil?
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.



35
36
37
38
# File 'lib/couchrest/mixins/attributes.rb', line 35

def update_attributes(hash)
  update_attributes_without_saving hash
  save
end

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

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.



24
25
26
27
28
29
# File 'lib/couchrest/mixins/attributes.rb', line 24

def update_attributes_without_saving(hash)
  # Remove any protected and update all the rest. Any attributes
  # which do not have a property will simply be ignored.
  attrs = remove_protected_attributes(hash)
  directly_set_attributes(attrs)
end