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

+ (Object) included(base)



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

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

- (Object) apply_defaults



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

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

- (Object) associate_casted_to_parent(casted, assigned)



74
75
76
77
# File 'lib/couchrest/mixins/properties.rb', line 74

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

- (Object) cast_keys



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

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

- (Object) cast_property(property, assigned = false)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/couchrest/mixins/properties.rb', line 47

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 = ::CouchRest.constantize(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
    self[key] = klass != String ? CastedArray.new(arr) : arr
    self[key].casted_by = self if self[key].respond_to?(:casted_by)
  else
    if property.type.downcase == 'boolean'
      klass = TrueClass
    else
      klass = ::CouchRest.constantize(property.type)
    end
    
    self[key] = typecast_value(self[key], klass, property.init_method)
    associate_casted_to_parent(self[key], assigned)
  end
end

- (Object) cast_property_by_name(property_name)



79
80
81
82
83
84
# File 'lib/couchrest/mixins/properties.rb', line 79

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