module PopulateMe::DocumentMixins::Persistence

Public Class Methods

included(base) click to toggle source
# File lib/populate_me/document_mixins/persistence.rb, line 58
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

attachment(f) click to toggle source
# File lib/populate_me/document_mixins/persistence.rb, line 15
def attachment f
  attacher = WebUtils.resolve_class_name self.class.fields[f][:class_name]
  attacher.new self, f
end
delete(o={}) click to toggle source

Deletion

# File lib/populate_me/document_mixins/persistence.rb, line 47
def delete o={}
  exec_callback :before_delete
  perform_delete
  exec_callback :after_delete
end
perform_create() click to toggle source
# File lib/populate_me/document_mixins/persistence.rb, line 36
def perform_create
  self.class.documents << self.to_h
  self.id
end
perform_delete() click to toggle source
# File lib/populate_me/document_mixins/persistence.rb, line 52
def perform_delete
  index = self.class.documents.index{|d| d['id']==self.id }
  raise MissingDocumentError, "No document can be found with this ID: #{self.id}" if self.id.nil?||index.nil?
  self.class.documents.delete_at(index)
end
perform_update() click to toggle source
# File lib/populate_me/document_mixins/persistence.rb, line 40
def perform_update
  index = self.class.documents.index{|d| d['id']==self.id }
  raise MissingDocumentError, "No document can be found with this ID: #{self.id}" if self.id.nil?||index.nil?
  self.class.documents[index] = self.to_h
end
persistent_instance_variables() click to toggle source
# File lib/populate_me/document_mixins/persistence.rb, line 5
def persistent_instance_variables
  instance_variables.select do |k|
    if self.class.fields.empty?
      k !~ /^@_/
    else
      self.class.fields.key? k[1..-1].to_sym
    end
  end
end
save() click to toggle source

Saving

# File lib/populate_me/document_mixins/persistence.rb, line 21
def save
  return unless valid?
  exec_callback :before_save
  if new?
    exec_callback :before_create
    id = perform_create
    exec_callback :after_create
  else
    exec_callback :before_update
    id = perform_update
    exec_callback :after_update
  end
  exec_callback :after_save
  id
end