Module: CouchRest::Model::Properties
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/couchrest/model/properties.rb
Instance Method Summary (collapse)
-
- (Object) as_couch_json
Provide an attribute hash ready to be sent to CouchDB but with all the nil attributes removed.
-
- (Object) properties_with_values
(also: #attributes)
Returns the Class properties with their values.
-
- (Object) read_attribute(property)
Read the casted value of an attribute defined with a property.
- - (Object) set_attributes(hash)
-
- (Object) update_attributes_without_saving(hash)
(also: #attributes=)
Takes a hash as argument, and applies the values by using writer methods for each key.
-
- (Object) write_attribute(property, value)
Store a casted value in the current instance of an attribute defined with a property and update dirty status.
Instance Method Details
- (Object) as_couch_json
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 |
- (Object) properties_with_values 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 |
- (Object) read_attribute(property)
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 |
- (Object) set_attributes(hash)
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 |
- (Object) update_attributes_without_saving(hash) 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 |
- (Object) write_attribute(property, value)
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.is_a?(String) ? value : prop.cast(self, value) couchrest_attribute_will_change!(prop.name) if use_dirty? && self[prop.name] != value self[prop.name] = value end |