Class: CouchRest::Model::Property

Inherits:
Object
  • Object
show all
Includes:
Typecast
Defined in:
lib/couchrest/model/property.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Typecast

#typecast_value

Constructor Details

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

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



11
12
13
14
15
16
17
# File 'lib/couchrest/model/property.rb', line 11

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.



7
8
9
# File 'lib/couchrest/model/property.rb', line 7

def alias
  @alias
end

#castedObject (readonly)

Returns the value of attribute casted.



7
8
9
# File 'lib/couchrest/model/property.rb', line 7

def casted
  @casted
end

#defaultObject (readonly)

Returns the value of attribute default.



7
8
9
# File 'lib/couchrest/model/property.rb', line 7

def default
  @default
end

#init_methodObject (readonly)

Returns the value of attribute init_method.



7
8
9
# File 'lib/couchrest/model/property.rb', line 7

def init_method
  @init_method
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/couchrest/model/property.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/couchrest/model/property.rb', line 7

def options
  @options
end

#read_onlyObject (readonly)

Returns the value of attribute read_only.



7
8
9
# File 'lib/couchrest/model/property.rb', line 7

def read_only
  @read_only
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/couchrest/model/property.rb', line 7

def type
  @type
end

#type_classObject (readonly)

Returns the value of attribute type_class.



7
8
9
# File 'lib/couchrest/model/property.rb', line 7

def type_class
  @type_class
end

Instance Method Details

#build(*args) ⇒ Object

Initialize a new instance of a property’s type ready to be used. If a proc is defined for the init method, it will be used instead of a normal call to the class.

Raises:

  • (StandardError)


65
66
67
68
69
70
71
72
# File 'lib/couchrest/model/property.rb', line 65

def build(*args)
  raise StandardError, "Cannot build property without a class" if @type_class.nil?
  if @init_method.is_a?(Proc)
    @init_method.call(*args)
  else
    @type_class.send(@init_method, *args)
  end
end

#cast(parent, value) ⇒ Object

Cast the provided value using the properties details.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/couchrest/model/property.rb', line 24

def cast(parent, value)
  return value unless casted
  if type.is_a?(Array)
    if value.nil?
      value = []
    elsif [Hash, HashWithIndifferentAccess].include?(value.class)
      # Assume provided as a params hash where key is index
      value = parameter_hash_to_array(value)
    elsif !value.is_a?(Array)
      raise "Expecting an array or keyed hash for property #{parent.class.name}##{self.name}"
    end
    arr = value.collect { |data| cast_value(parent, data) }
    # allow casted_by calls to be passed up chain by wrapping in CastedArray
    CastedArray.new(arr, self, parent)
  elsif (type == Object || type == Hash) && (value.is_a?(Hash))
    # allow casted_by calls to be passed up chain by wrapping in CastedHash
    CastedHash[value, self, parent]
  elsif !value.nil?
    cast_value(parent, value)
  end
end

#cast_value(parent, value) ⇒ Object

Cast an individual value



47
48
49
50
# File 'lib/couchrest/model/property.rb', line 47

def cast_value(parent, value)
  value = typecast_value(value, self)
  associate_casted_value_to_parent(parent, value)
end

#default_valueObject



52
53
54
55
56
57
58
59
60
# File 'lib/couchrest/model/property.rb', line 52

def default_value
  return if default.nil?
  if default.class == Proc
    default.call
  else
    # TODO identify cause of mutex errors
    Marshal.load(Marshal.dump(default))
  end
end

#to_sObject



19
20
21
# File 'lib/couchrest/model/property.rb', line 19

def to_s
  name
end