module Rethinker::Document::Persistence
Public Class Methods
new(attrs={}, options={})
click to toggle source
TODO after_initialize, after_find callback
Calls superclass method
# File lib/rethinker/document/persistence.rb, line 10 def initialize(attrs={}, options={}) super @new_record = !options[:from_db] end
Public Instance Methods
_create()
click to toggle source
# File lib/rethinker/document/persistence.rb, line 27 def _create run_callbacks :create do result = Rethinker.run { self.class.table.insert(attributes) } self.id ||= result['generated_keys'].first @new_record = false true end end
delete()
click to toggle source
# File lib/rethinker/document/persistence.rb, line 59 def delete selector.delete @destroyed = true # TODO freeze attributes true end
destroy()
click to toggle source
# File lib/rethinker/document/persistence.rb, line 66 def destroy run_callbacks(:destroy) { delete } end
destroyed?()
click to toggle source
# File lib/rethinker/document/persistence.rb, line 19 def destroyed? !!@destroyed end
new_record?()
click to toggle source
# File lib/rethinker/document/persistence.rb, line 15 def new_record? @new_record end
persisted?()
click to toggle source
# File lib/rethinker/document/persistence.rb, line 23 def persisted? !new_record? && !destroyed? end
reload()
click to toggle source
# File lib/rethinker/document/persistence.rb, line 36 def reload assign_attributes(selector.run, :pristine => true) self end
save(options={})
click to toggle source
# File lib/rethinker/document/persistence.rb, line 48 def save(options={}) run_callbacks :save do new_record? ? _create : update { attributes } end end
update(&block)
click to toggle source
# File lib/rethinker/document/persistence.rb, line 41 def update(&block) run_callbacks :update do selector.update(&block) true end end
update_attributes(attrs, options={})
click to toggle source
# File lib/rethinker/document/persistence.rb, line 54 def update_attributes(attrs, options={}) assign_attributes(attrs, options) save end