Module: CouchRest::Model::Properties

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#as_couch_jsonObject

Provide an attribute hash ready to be sent to CouchDB but with all the nil attributes removed.



17
18
19
# File 'lib/couchrest/model/properties.rb', line 17

def as_couch_json
  super.delete_if{|k,v| v.nil?}
end

#properties_with_valuesObject Also known as: attributes

Returns the Class properties with their values

Returns

Array

the list of properties with their values



25
26
27
28
29
# File 'lib/couchrest/model/properties.rb', line 25

def properties_with_values
  props = {}
  properties.each { |property| props[property.name] = read_attribute(property.name) }
  props
end

#read_attribute(property) ⇒ Object

Read the casted value of an attribute defined with a property.

Returns

Object

the casted attibutes value.



35
36
37
# File 'lib/couchrest/model/properties.rb', line 35

def read_attribute(property)
  self[find_property!(property).to_s]
end

#set_attributes(hash) ⇒ Object



62
63
64
65
# File 'lib/couchrest/model/properties.rb', line 62

def set_attributes(hash)
  attrs = remove_protected_attributes(hash)
  directly_set_attributes(attrs)
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.



51
52
53
54
55
56
# File 'lib/couchrest/model/properties.rb', line 51

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

#write_attribute(property, value) ⇒ Object

Store a casted value in the current instance of an attribute defined with a property and update dirty status



41
42
43
44
45
46
# File 'lib/couchrest/model/properties.rb', line 41

def write_attribute(property, value)
  prop = find_property!(property)
  value = prop.cast(self, value)
  couchrest_attribute_will_change!(prop.name) if use_dirty? && self[prop.name] != value
  self[prop.name] = value
end