Casted Model now no longer depends on a Hash
This commit is contained in:
parent
98772ae98a
commit
406d2bc791
8 changed files with 57 additions and 26 deletions
|
@ -81,10 +81,6 @@ module CouchRest
|
|||
super
|
||||
end
|
||||
|
||||
def persisted?
|
||||
!new?
|
||||
end
|
||||
|
||||
def to_key
|
||||
new? ? nil : [id]
|
||||
end
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
module CouchRest::Model
|
||||
module CastedModel
|
||||
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
include CouchRest::Attributes
|
||||
include CouchRest::Model::Configuration
|
||||
include CouchRest::Model::Properties
|
||||
include CouchRest::Model::PropertyProtection
|
||||
|
@ -19,23 +19,17 @@ module CouchRest::Model
|
|||
def base_doc?
|
||||
false # Can never be base doc!
|
||||
end
|
||||
|
||||
# Initialize a new Casted Model. Accepts the same
|
||||
# options as CouchRest::Model::Base for preparing and initializing
|
||||
# attributes.
|
||||
def initialize(keys = {}, options = {})
|
||||
super()
|
||||
prepare_all_attributes(keys, options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(keys = {})
|
||||
raise StandardError unless self.is_a? Hash
|
||||
prepare_all_attributes(keys)
|
||||
super()
|
||||
end
|
||||
|
||||
def []= key, value
|
||||
super(key.to_s, value)
|
||||
end
|
||||
|
||||
def [] key
|
||||
super(key.to_s)
|
||||
end
|
||||
|
||||
# False if the casted model has already
|
||||
# been saved in the containing document
|
||||
def new?
|
||||
|
|
|
@ -28,7 +28,8 @@ module CouchRest
|
|||
# Trigger the callbacks (before, after, around)
|
||||
# only if the document isn't new
|
||||
def update(options = {})
|
||||
raise "Calling #{self.class.name}#update on document that has not been created!" if self.new?
|
||||
raise "Cannot save a destroyed document!" if destroyed?
|
||||
raise "Calling #{self.class.name}#update on document that has not been created!" if new?
|
||||
return false unless perform_validations(options)
|
||||
return true if !self.disable_dirty && !self.changed?
|
||||
_run_update_callbacks do
|
||||
|
@ -69,6 +70,10 @@ module CouchRest
|
|||
!!@_destroyed
|
||||
end
|
||||
|
||||
def persisted?
|
||||
!new? && !destroyed?
|
||||
end
|
||||
|
||||
# Update the document's attributes and save. For example:
|
||||
#
|
||||
# doc.update_attributes :name => "Fred"
|
||||
|
|
|
@ -168,7 +168,7 @@ module CouchRest
|
|||
# check if this property is going to casted
|
||||
type = options.delete(:type) || options.delete(:cast_as)
|
||||
if block_given?
|
||||
type = Class.new(Hash) do
|
||||
type = Class.new do
|
||||
include CastedModel
|
||||
end
|
||||
if block.arity == 1 # Traditional, with options
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue