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, options = {}, &block) ⇒ Property

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



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

def initialize(name, options = {}, &block)
  @name = name.to_s
  parse_options(options)
  parse_type(options, &block)
  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

#allow_blankObject (readonly)

Returns the value of attribute allow_blank.



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

def allow_blank
  @allow_blank
end

#arrayObject (readonly)

Returns the value of attribute array.



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

def array
  @array
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

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)


73
74
75
76
77
78
79
80
81
# File 'lib/couchrest/model/property.rb', line 73

def build(*args)
  raise StandardError, "Cannot build property without a class" if @type.nil?

  if @init_method.is_a?(Proc)
    @init_method.call(*args)
  else
    @type.send(@init_method, *args)
  end
end

#cast(parent, value) ⇒ Object

Cast the provided value using the properties details.



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

def cast(parent, value)
  return value unless casted
  if array
    if value.nil?
      value = []
    elsif value.is_a?(Hash)
      # 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) }
    arr.reject!{ |data| data.nil? } unless allow_blank
    # 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



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

def cast_value(parent, value)
  if !allow_blank && value.to_s.empty?
    nil
  else
    value = typecast_value(parent, self, value)
    associate_casted_value_to_parent(parent, value)
  end
end

#default_valueObject



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

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



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

def to_s
  name
end

#to_symObject



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

def to_sym
  @_sym_name ||= name.to_sym
end