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

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

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

- (Object) alias (readonly)

Returns the value of attribute alias



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

def alias
  @alias
end

- (Object) casted (readonly)

Returns the value of attribute casted



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

def casted
  @casted
end

- (Object) default (readonly)

Returns the value of attribute default



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

def default
  @default
end

- (Object) init_method (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

- (Object) name (readonly)

Returns the value of attribute name



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

def name
  @name
end

- (Object) options (readonly)

Returns the value of attribute options



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

def options
  @options
end

- (Object) read_only (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

- (Object) type (readonly)

Returns the value of attribute type



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

def type
  @type
end

- (Object) type_class (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

- (Object) build(*args)

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

- (Object) cast(parent, value)

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

- (Object) cast_value(parent, value)

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

- (Object) default_value



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

- (Object) to_s



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

def to_s
  name
end