module NoBrainer::Document::Persistance

Public Instance Methods

_create(options={}) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 57
def _create(options={})
  attrs = self.class.persistable_attributes(@_attributes)
  result = NoBrainer.run(self.class.rql_table.insert(attrs))
  self.pk_value ||= result['generated_keys'].to_a.first
  @new_record = false
  unlock_unique_fields # just an optimization for the uniquness validation
  true
end
_initialize(attrs={}, options={}) click to toggle source
Calls superclass method
# File lib/no_brainer/document/persistance.rb, line 4
def _initialize(attrs={}, options={})
  @new_record = !options[:from_db]
  super
end
_reload(options={}) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 21
def _reload(options={})
  criteria = root_class.raw
  if opt = options[:missing_attributes]
    criteria = criteria.pluck(opt[:pluck]) if opt[:pluck]
    criteria = criteria.without(opt[:without]) if opt[:without]
  end
  attrs = criteria.find(pk_value)

  options = options.merge(:pristine => true, :from_db => true)

  if options[:keep_ivars]
    assign_attributes(attrs, options)
  else
    instance_variables.each { |ivar| remove_instance_variable(ivar) }
    initialize(attrs, options)
  end

  self
end
_save?(options={}) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 86
def _save?(options={})
  new_record? ? _create(options) : _update_only_changed_attrs(options)
end
_update(attrs) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 66
def _update(attrs)
  rql = ->(doc){ self.class.persistable_attributes(attrs, :rql_doc => doc) }
  NoBrainer.run { selector.update(&rql) }
end
_update_only_changed_attrs(options={}) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 71
def _update_only_changed_attrs(options={})
  # We won't be using the `changes` values, because they went through
  # read_attribute(), and we want the raw values.
  attrs = Hash[self.changed.map do |k|
    attr = @_attributes[k]
    # If we have a hash to save, we need to specify r.literal(),
    # otherwise, the hash would just get merged with the existing one.
    attr = RethinkDB::RQL.new.literal(attr) if attr.is_a?(Hash)
    [k, attr]
  end]
  _update(attrs) if attrs.present?
  unlock_unique_fields # just an optimization for the uniquness validation
  true
end
delete() click to toggle source
# File lib/no_brainer/document/persistance.rb, line 117
def delete
  unless @destroyed
    NoBrainer.run { selector.delete }
    @destroyed = true
  end
  @_attributes.freeze
  true
end
destroy() click to toggle source
# File lib/no_brainer/document/persistance.rb, line 126
def destroy
  delete
end
destroyed?() click to toggle source
# File lib/no_brainer/document/persistance.rb, line 13
def destroyed?
  !!@destroyed
end
new_record?() click to toggle source
# File lib/no_brainer/document/persistance.rb, line 9
def new_record?
  !!@new_record
end
persisted?() click to toggle source
# File lib/no_brainer/document/persistance.rb, line 17
def persisted?
  !new_record? && !destroyed?
end
reload(options={}) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 41
def reload(options={})
  [:without, :pluck].each do |type|
    next unless v = options.delete(type)

    v = Hash[v.flatten.map { |k| [k, true] }] if v.is_a?(Array)
    v = {v => true} unless v.is_a?(Hash)
    v = v.select { |k,_v| _v }
    v = v.with_indifferent_access
    next unless v.present?

    options[:missing_attributes] ||= {}
    options[:missing_attributes][type] = v
  end
  _reload(options)
end
save(*args) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 98
def save(*args)
  save?(*args)
end
save!(*args) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 94
def save!(*args)
  save?(*args) or raise NoBrainer::Error::DocumentInvalid, self
end
save?(options={}) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 90
def save?(options={})
  _save?(options)
end
update(*args) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 112
def update(*args)
  update?(*args)
end
Also aliased as: update_attributes
update!(*args) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 107
def update!(*args)
  update?(*args) or raise NoBrainer::Error::DocumentInvalid, self
end
Also aliased as: update_attributes!
update?(attrs, options={}) click to toggle source
# File lib/no_brainer/document/persistance.rb, line 102
def update?(attrs, options={})
  assign_attributes(attrs, options)
  save?(options)
end
update_attributes(*args)
Alias for: update
update_attributes!(*args)
Alias for: update!