2009-01-29 18:45:01 -08:00
|
|
|
module CouchRest
|
2009-07-21 03:17:27 +06:00
|
|
|
|
2009-02-05 17:06:12 -08:00
|
|
|
# Basic attribute support for adding getter/setter + validation
|
2009-01-29 18:45:01 -08:00
|
|
|
class Property
|
2009-02-09 11:20:23 -08:00
|
|
|
attr_reader :name, :type, :read_only, :alias, :default, :casted, :init_method, :options
|
2009-07-21 03:17:27 +06:00
|
|
|
|
2009-01-29 18:45:01 -08:00
|
|
|
# attribute to define
|
2009-02-05 17:06:12 -08:00
|
|
|
def initialize(name, type = nil, options = {})
|
2009-02-09 11:20:23 -08:00
|
|
|
@name = name.to_s
|
2009-02-12 20:28:07 -08:00
|
|
|
parse_type(type)
|
2009-01-29 18:45:01 -08:00
|
|
|
parse_options(options)
|
|
|
|
self
|
|
|
|
end
|
2009-07-21 03:17:27 +06:00
|
|
|
|
2009-01-29 18:45:01 -08:00
|
|
|
private
|
2009-07-21 03:17:27 +06:00
|
|
|
|
2009-02-12 20:28:07 -08:00
|
|
|
def parse_type(type)
|
|
|
|
if type.nil?
|
2010-03-30 20:50:47 +00:00
|
|
|
@type = String
|
2009-02-24 22:51:13 -08:00
|
|
|
elsif type.is_a?(Array) && type.empty?
|
2010-03-30 20:50:47 +00:00
|
|
|
@type = [Object]
|
2009-02-12 20:28:07 -08:00
|
|
|
else
|
2010-03-30 20:50:47 +00:00
|
|
|
base_type = type.is_a?(Array) ? type.first : type
|
|
|
|
if base_type.is_a?(String)
|
2010-03-31 08:25:33 +00:00
|
|
|
if base_type.downcase == 'boolean'
|
|
|
|
base_type = TrueClass
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
base_type = ::CouchRest.constantize(base_type)
|
|
|
|
rescue # leave base type as a string and convert in more/typecast
|
|
|
|
end
|
2010-03-30 20:50:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
@type = type.is_a?(Array) ? [base_type] : base_type
|
2009-02-12 20:28:07 -08:00
|
|
|
end
|
|
|
|
end
|
2009-07-21 03:17:27 +06:00
|
|
|
|
2009-01-29 18:45:01 -08:00
|
|
|
def parse_options(options)
|
|
|
|
return if options.empty?
|
2009-02-05 18:57:11 -08: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 10:31:19 +08:00
|
|
|
@default = options.delete(:default) unless options[:default].nil?
|
2009-02-09 11:20:23 -08:00
|
|
|
@casted = options[:casted] ? true : false
|
2009-10-23 23:42:48 -02:00
|
|
|
@init_method = options[:init_method] ? options.delete(:init_method) : 'new'
|
2009-02-05 17:06:12 -08:00
|
|
|
@options = options
|
2009-01-29 18:45:01 -08:00
|
|
|
end
|
2009-07-21 03:17:27 +06:00
|
|
|
|
2009-01-29 18:45:01 -08:00
|
|
|
end
|
2009-04-26 10:31:19 +08:00
|
|
|
end
|