Class: CouchRest::Property
- 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
-
#alias ⇒ Object
readonly
Returns the value of attribute alias.
-
#autovalidation_check ⇒ Object
flag letting us know if we already checked the autovalidation settings.
-
#casted ⇒ Object
readonly
Returns the value of attribute casted.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#init_method ⇒ Object
readonly
Returns the value of attribute init_method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#read_only ⇒ Object
readonly
Returns the value of attribute read_only.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#cast(parent, value) ⇒ Object
Cast the provided value using the properties details.
-
#cast_value(parent, value) ⇒ Object
Cast an individual value, not an array.
- #default_value ⇒ Object
-
#initialize(name, type = nil, options = {}) ⇒ Property
constructor
Attribute to define.
- #to_s ⇒ Object
-
#type_class ⇒ Object
Always provide the basic type as a class.
Methods included from Mixins::Typecast
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, = {}) @name = name.to_s @casted = true parse_type(type) () self end |
Instance Attribute Details
#alias ⇒ Object (readonly)
Returns the value of attribute alias.
11 12 13 |
# File 'lib/couchrest/property.rb', line 11 def alias @alias end |
#autovalidation_check ⇒ Object
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 |
#casted ⇒ Object (readonly)
Returns the value of attribute casted.
11 12 13 |
# File 'lib/couchrest/property.rb', line 11 def casted @casted end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
11 12 13 |
# File 'lib/couchrest/property.rb', line 11 def default @default end |
#init_method ⇒ Object (readonly)
Returns the value of attribute init_method.
11 12 13 |
# File 'lib/couchrest/property.rb', line 11 def init_method @init_method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/couchrest/property.rb', line 11 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/couchrest/property.rb', line 11 def @options end |
#read_only ⇒ Object (readonly)
Returns the value of attribute read_only.
11 12 13 |
# File 'lib/couchrest/property.rb', line 11 def read_only @read_only end |
#type ⇒ Object (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_value ⇒ Object
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_s ⇒ Object
23 24 25 |
# File 'lib/couchrest/property.rb', line 23 def to_s name end |
#type_class ⇒ Object
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 |