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



9
10
11
12
13
14
15
16
# File 'lib/couchrest/mixins/properties.rb', line 9

def self.included(base)
  base.cattr_accessor(:properties)
    base.class_eval <<-EOS, __FILE__, __LINE__
        @@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_defaultsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/couchrest/mixins/properties.rb', line 18

def apply_defaults
  return unless self.respond_to?(:new_document?) && new_document?
  return unless self.class.respond_to?(:properties) 
  return if self.class.properties.empty?
  # TODO: cache the default object
  self.class.properties.each do |property|
    key = property.name.to_s
    # let's make sure we have a default and we can assign the value
    if property.default && (self.respond_to?("#{key}=") || self.key?(key))
        if property.default.class == Proc
          self[key] = property.default.call
        else
          self[key] = Marshal.load(Marshal.dump(property.default))
        end
      end
  end
end

#cast_keysObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/couchrest/mixins/properties.rb', line 36

def cast_keys
  return unless self.class.properties
  self.class.properties.each do |property|
    next unless property.casted
    key = self.has_key?(property.name) ? property.name : property.name.to_sym
    target = property.type
    if target.is_a?(Array)
      next unless self[key]
      klass = ::CouchRest.constantize(target[0])
      self[property.name] = self[key].collect do |value|
        # Auto parse Time objects
        obj = ( (property.init_method == 'new') && klass == Time) ? Time.parse(value) : klass.send(property.init_method, value)
        obj.casted_by = self if obj.respond_to?(:casted_by)
        obj
      end
    else
      # Auto parse Time objects
      self[property.name] = if ((property.init_method == 'new') && target == 'Time') 
        self[key].is_a?(String) ? Time.parse(self[key].dup) : self[key]
      else
        # Let people use :send as a Time parse arg
        klass = ::CouchRest.constantize(target)
        # I'm not convince we should or should not create a new instance if we are casting a doc/extended doc without default value and nothing was passed
        # unless (property.casted && 
        #   (klass.superclass == CouchRest::ExtendedDocument || klass.superclass == CouchRest::Document) && 
        #     (self[key].nil? || property.default.nil?))
        klass.send(property.init_method, self[key])
        #end
      end
      self[property.name].casted_by = self if self[property.name].respond_to?(:casted_by)
    end
  end
end