2009-01-30 03:45:01 +01:00
|
|
|
module CouchRest
|
|
|
|
|
2009-02-06 02:06:12 +01:00
|
|
|
# Basic attribute support for adding getter/setter + validation
|
2009-01-30 03:45:01 +01:00
|
|
|
class Property
|
2009-02-09 20:20:23 +01:00
|
|
|
attr_reader :name, :type, :read_only, :alias, :default, :casted, :init_method, :options
|
2009-01-30 03:45:01 +01:00
|
|
|
|
|
|
|
# attribute to define
|
2009-02-06 02:06:12 +01:00
|
|
|
def initialize(name, type = nil, options = {})
|
2009-02-09 20:20:23 +01:00
|
|
|
@name = name.to_s
|
2009-02-13 05:28:07 +01:00
|
|
|
parse_type(type)
|
2009-01-30 03:45:01 +01:00
|
|
|
parse_options(options)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
2009-02-13 05:28:07 +01:00
|
|
|
|
|
|
|
def parse_type(type)
|
|
|
|
if type.nil?
|
|
|
|
@type = 'String'
|
2009-02-25 07:51:13 +01:00
|
|
|
elsif type.is_a?(Array) && type.empty?
|
|
|
|
@type = 'Array'
|
2009-02-13 05:28:07 +01:00
|
|
|
else
|
|
|
|
@type = type.is_a?(Array) ? [type.first.to_s] : type.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-30 03:45:01 +01:00
|
|
|
def parse_options(options)
|
|
|
|
return if options.empty?
|
2009-02-06 03:57:11 +01:00
|
|
|
@validation_format = options.delete(:format) if options[:format]
|
|
|
|
@read_only = options.delete(:read_only) if options[:read_only]
|
|
|
|
@alias = options.delete(:alias) if options[:alias]
|
2009-04-26 04:31:19 +02:00
|
|
|
@default = options.delete(:default) unless options[:default].nil?
|
2009-02-09 20:20:23 +01:00
|
|
|
@casted = options[:casted] ? true : false
|
2009-02-10 00:12:22 +01:00
|
|
|
@init_method = options[:send] ? options.delete(:send) : 'new'
|
2009-02-06 02:06:12 +01:00
|
|
|
@options = options
|
2009-01-30 03:45:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-04-26 04:31:19 +02:00
|
|
|
end
|
2009-05-29 02:56:42 +02:00
|
|
|
|
|
|
|
class CastedArray < Array
|
|
|
|
attr_accessor :casted_by
|
|
|
|
|
|
|
|
def << obj
|
|
|
|
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
|
|
|
super(obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
def push(obj)
|
|
|
|
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
|
|
|
super(obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
def []= index, obj
|
|
|
|
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
|
|
|
super(index, obj)
|
|
|
|
end
|
|
|
|
end
|