2009-02-09 20:20:23 +01:00
|
|
|
module CouchRest
|
|
|
|
module CastedModel
|
|
|
|
|
|
|
|
def self.included(base)
|
2010-05-10 21:19:24 +02:00
|
|
|
base.send(:include, ::CouchRest::Mixins::Callbacks)
|
2009-07-30 03:32:34 +02:00
|
|
|
base.send(:include, ::CouchRest::Mixins::Properties)
|
2009-02-09 20:20:23 +01:00
|
|
|
base.send(:attr_accessor, :casted_by)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(keys={})
|
2009-04-02 16:00:28 +02:00
|
|
|
raise StandardError unless self.is_a? Hash
|
2010-06-16 21:04:53 +02:00
|
|
|
apply_all_property_defaults # defined in CouchRest::Mixins::Properties
|
2009-04-02 16:00:28 +02:00
|
|
|
super()
|
2009-02-09 20:20:23 +01:00
|
|
|
keys.each do |k,v|
|
2010-06-16 21:04:53 +02:00
|
|
|
write_attribute(k.to_s, v)
|
2009-02-09 20:20:23 +01:00
|
|
|
end if keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def []= key, value
|
|
|
|
super(key.to_s, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def [] key
|
|
|
|
super(key.to_s)
|
|
|
|
end
|
2009-05-27 22:24:25 +02:00
|
|
|
|
2009-05-29 07:42:30 +02:00
|
|
|
# Gets a reference to the top level extended
|
|
|
|
# document that a model is saved inside of
|
|
|
|
def base_doc
|
2009-05-31 08:20:39 +02:00
|
|
|
return nil unless @casted_by
|
2009-05-29 07:42:30 +02:00
|
|
|
@casted_by.base_doc
|
|
|
|
end
|
|
|
|
|
2009-05-29 02:00:06 +02:00
|
|
|
# False if the casted model has already
|
2009-05-29 01:09:53 +02:00
|
|
|
# been saved in the containing document
|
2009-06-05 05:44:44 +02:00
|
|
|
def new?
|
2010-06-16 21:04:53 +02:00
|
|
|
@casted_by.nil? ? true : @casted_by.new?
|
2009-05-29 01:09:53 +02:00
|
|
|
end
|
2009-06-05 05:44:44 +02:00
|
|
|
alias :new_record? :new?
|
2009-05-29 01:09:53 +02:00
|
|
|
|
2009-05-27 22:24:25 +02:00
|
|
|
# Sets the attributes from a hash
|
|
|
|
def update_attributes_without_saving(hash)
|
|
|
|
hash.each do |k, v|
|
|
|
|
raise NoMethodError, "#{k}= method not available, use property :#{k}" unless self.respond_to?("#{k}=")
|
|
|
|
end
|
|
|
|
hash.each do |k, v|
|
|
|
|
self.send("#{k}=",v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias :attributes= :update_attributes_without_saving
|
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
end
|
2009-05-29 02:00:06 +02:00
|
|
|
end
|