Class: CouchRest::Property

Inherits:
Object show all
Includes:
Mixins::Typecast
Defined in:
lib/couchrest/property.rb,
lib/couchrest/validation/auto_validate.rb

Overview

Basic attribute support for adding getter/setter + validation

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Typecast

#typecast_value

Constructor Details

#initialize(name, type = nil, options = {}) ⇒ Property

Attribute to define. All Properties are assumed casted unless the type is nil.



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

def initialize(name, type = nil, options = {})
  @name = name.to_s
  @casted = true
  parse_type(type)
  parse_options(options)
  self
end

Instance Attribute Details

#aliasObject (readonly)

Returns the value of attribute alias.



11
12
13
# File 'lib/couchrest/property.rb', line 11

def alias
  @alias
end

#autovalidation_checkObject

flag letting us know if we already checked the autovalidation settings



7
8
9
# File 'lib/couchrest/validation/auto_validate.rb', line 7

def autovalidation_check
  @autovalidation_check
end

#castedObject (readonly)

Returns the value of attribute casted.



11
12
13
# File 'lib/couchrest/property.rb', line 11

def casted
  @casted
end

#defaultObject (readonly)

Returns the value of attribute default.



11
12
13
# File 'lib/couchrest/property.rb', line 11

def default
  @default
end

#init_methodObject (readonly)

Returns the value of attribute init_method.



11
12
13
# File 'lib/couchrest/property.rb', line 11

def init_method
  @init_method
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/couchrest/property.rb', line 11

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/couchrest/property.rb', line 11

def options
  @options
end

#read_onlyObject (readonly)

Returns the value of attribute read_only.



11
12
13
# File 'lib/couchrest/property.rb', line 11

def read_only
  @read_only
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/couchrest/property.rb', line 11

def type
  @type
end

Instance Method Details

#cast(parent, value) ⇒ Object

Cast the provided value using the properties details.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/couchrest/property.rb', line 28

def cast(parent, value)
  return value unless casted
  if type.is_a?(Array)
    # Convert to array if it is not already
    value = [value].compact unless value.is_a?(Array)
    arr = value.collect { |data| cast_value(parent, data) }
    # allow casted_by calls to be passed up chain by wrapping in CastedArray
    value = type_class != String ? ::CouchRest::CastedArray.new(arr, self) : arr
    value.casted_by = parent if value.respond_to?(:casted_by)
  elsif !value.nil?
    value = cast_value(parent, value)
  end
  value
end

#cast_value(parent, value) ⇒ Object

Cast an individual value, not an array



44
45
46
47
48
# File 'lib/couchrest/property.rb', line 44

def cast_value(parent, value)
  raise "An array inside an array cannot be casted, use CastedModel" if value.is_a?(Array)
  value = typecast_value(value, self)
  associate_casted_value_to_parent(parent, value)
end

#default_valueObject



50
51
52
53
54
55
56
57
# File 'lib/couchrest/property.rb', line 50

def default_value
  return if default.nil?
  if default.class == Proc
    default.call
  else
    Marshal.load(Marshal.dump(default))
  end
end

#to_sObject



23
24
25
# File 'lib/couchrest/property.rb', line 23

def to_s
  name
end

#type_classObject

Always provide the basic type as a class. If the type is an array, the class will be extracted.



61
62
63
64
65
66
67
68
# File 'lib/couchrest/property.rb', line 61

def type_class
  return String unless casted # This is rubbish, to handle validations
  return @type_class unless @type_class.nil?
  base = @type.is_a?(Array) ? @type.first : @type
  base = String if base.nil?
  base = TrueClass if base.is_a?(String) && base.downcase == 'boolean'
  @type_class = base.is_a?(Class) ? base : base.constantize
end