Basic attribute support for adding getter/setter + validation
flag letting us know if we already checked the autovalidation settings
Cast the provided value using the properties details.
# 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 an individual value, not an array
# 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
# 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
# File lib/couchrest/property.rb, line 23 def to_s name end
Always provide the basic type as a class. If the type is an array, the class will be extracted.
# 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
Attribute to define. All Properties are assumed casted unless the type is nil.
# 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