module CouchRest::Mixins::Properties

Public Instance Methods

apply_all_property_defaults() click to toggle source
# File lib/couchrest/mixins/properties.rb, line 39
def apply_all_property_defaults
  return if self.respond_to?(:new?) && (new? == false)
  # TODO: cache the default object
  self.class.properties.each do |property|
    write_attribute(property, property.default_value)
  end
end
properties() click to toggle source

Returns the Class properties

Returns

Array

the list of properties for model’s class

# File lib/couchrest/mixins/properties.rb, line 25
def properties
  self.class.properties
end
read_attribute(property) click to toggle source
# File lib/couchrest/mixins/properties.rb, line 29
def read_attribute(property)
  self[property.to_s]
end
write_attribute(property, value) click to toggle source
# File lib/couchrest/mixins/properties.rb, line 33
def write_attribute(property, value)
  prop = property.is_a?(::CouchRest::Property) ? property : self.class.properties.detect {|p| p.to_s == property.to_s}
  raise "Missing property definition for #{property.to_s}" unless prop
  self[prop.to_s] = prop.cast(self, value)
end

Public Class Methods

included(base) click to toggle source
# File lib/couchrest/mixins/properties.rb, line 11
      def self.included(base)
        base.class_eval "            extend CouchRest::InheritableAttributes
            couchrest_inheritable_accessor(:properties) unless self.respond_to?(:properties)
            self.properties ||= []
", __FILE__, __LINE__ + 1
        base.extend(ClassMethods)
        raise CouchRest::Mixins::Properties::IncludeError, "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (base.new.respond_to?(:[]) && base.new.respond_to?(:[]=))
      end