Module: CouchRest::Mixins::Properties

Defined in:
lib/couchrest/mixins/properties.rb

Defined Under Namespace

Modules: ClassMethods Classes: IncludeError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.class_eval <<-EOS, __FILE__, __LINE__ + 1
      extend CouchRest::InheritableAttributes
      couchrest_inheritable_accessor(:properties) unless self.respond_to?(:properties)
      self.properties ||= []
  EOS
  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

Instance Method Details

#apply_all_property_defaultsObject



39
40
41
42
43
44
45
# 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

#propertiesObject

Returns the Class properties

Returns

Array

the list of properties for model’s class



25
26
27
# File 'lib/couchrest/mixins/properties.rb', line 25

def properties
  self.class.properties
end

#read_attribute(property) ⇒ Object



29
30
31
# File 'lib/couchrest/mixins/properties.rb', line 29

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

#write_attribute(property, value) ⇒ Object



33
34
35
36
37
# 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