Module: CouchRest::Mixins::Properties

Includes:
CouchRest::More::Typecast
Defined in:
lib/couchrest/mixins/properties.rb

Defined Under Namespace

Modules: ClassMethods Classes: IncludeError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CouchRest::More::Typecast

#typecast_value

Class Method Details

.included(base) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/couchrest/mixins/properties.rb', line 14

def self.included(base)
  base.class_eval <<-EOS, __FILE__, __LINE__ + 1
      extlib_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_defaultsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/couchrest/mixins/properties.rb', line 23

def apply_defaults
  return if self.respond_to?(:new?) && (new? == false)
  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
    unless property.default.nil?
        if property.default.class == Proc
          self[key] = property.default.call
        else
          self[key] = Marshal.load(Marshal.dump(property.default))
        end
      end
  end
end

#associate_casted_to_parent(casted, assigned) ⇒ Object



70
71
72
73
# File 'lib/couchrest/mixins/properties.rb', line 70

def associate_casted_to_parent(casted, assigned)
  casted.casted_by = self if casted.respond_to?(:casted_by)
  casted.document_saved = true if !assigned && casted.respond_to?(:document_saved)
end

#cast_keysObject



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

def cast_keys
  return unless self.class.properties
  self.class.properties.each do |property|
    cast_property(property)
  end
end

#cast_property(property, assigned = false) ⇒ Object



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 48

def cast_property(property, assigned=false)
  return unless property.casted
  key = self.has_key?(property.name) ? property.name : property.name.to_sym
  # Don't cast the property unless it has a value
  return unless self[key]
  if property.type.is_a?(Array)
    klass = property.type[0]
    self[key] = [self[key]] unless self[key].is_a?(Array)
    arr = self[key].collect do |value|
      value = typecast_value(value, klass, property.init_method)
      associate_casted_to_parent(value, assigned)
      value
    end
    # allow casted_by calls to be passed up chain by wrapping in CastedArray
    self[key] = klass != String ? ::CouchRest::CastedArray.new(arr) : arr
    self[key].casted_by = self if self[key].respond_to?(:casted_by)
  else
    self[key] = typecast_value(self[key], property.type, property.init_method)
    associate_casted_to_parent(self[key], assigned)
  end
end

#cast_property_by_name(property_name) ⇒ Object



75
76
77
78
79
80
# File 'lib/couchrest/mixins/properties.rb', line 75

def cast_property_by_name(property_name)
  return unless self.class.properties
  property = self.class.properties.detect{|property| property.name == property_name}
  return unless property
  cast_property(property, true)
end