use strings instead of constants

This commit is contained in:
Chris Anderson 2008-10-02 23:56:22 -07:00
parent 2f35d3c10e
commit 696d89c45c
2 changed files with 6 additions and 7 deletions

View file

@ -450,14 +450,13 @@ module CouchRest
self.class.casts.each do |k,v| self.class.casts.each do |k,v|
next unless self[k] next unless self[k]
target = v[:as] target = v[:as]
if target.is_a?(Array) && target[0].is_a?(Class) if target.is_a?(Array)
klass = ::Extlib::Inflection.constantize(target[0])
self[k] = self[k].collect do |value| self[k] = self[k].collect do |value|
target[0].new(value) klass.new(value)
end end
elsif target.is_a?(Class)
self[k] = target.new(self[k])
else else
raise ArgumentError, "Call like - cast :field, :as => MyClass - or - :as => [MyClass] if the field is an array." self[k] = ::Extlib::Inflection.constantize(target).new(self[k])
end end
end end
end end

View file

@ -27,8 +27,8 @@ end
class Course < CouchRest::Model class Course < CouchRest::Model
key_accessor :title key_accessor :title
cast :questions, :as => [Question] cast :questions, :as => ['Question']
cast :professor, :as => Person cast :professor, :as => 'Person'
view_by :title view_by :title
end end