class Snapshotar::Core

Public Instance Methods

delete(filename) click to toggle source

delete a snapshot.

Params
filename

name of the snapshot to delete

# File lib/snapshotar/core.rb, line 120
def delete(filename)

  @storage.delete(filename)
end
export(filename = nil) click to toggle source

Performs a snapshot

Params
filename

filename to create or nil to let snapshotar create something

like +snapshotar_dump_<timestamp>.json+

returns

filename

# File lib/snapshotar/core.rb, line 35
def export(filename = nil)
  filename ||= "snapshotar_dump_#{Time.now.to_i}.json"

  serialized = Jbuilder.encode do |json|
    Snapshotar.configuration.models.each do |m|
      model = m.first
      json.set! model.name do

        # iterate objects
        json.array! model.unscoped.all do |itm|

          # support inherited classes
          json.set! :clazz, itm.class.to_s unless (itm.class.to_s == model.name)

          # iterate attributes
          m[1..-1].each do |attr|

            next unless itm.respond_to?(attr.to_sym)
            next if itm[attr].nil?

            # replace uploads by their url
            if itm.send(attr.to_sym).respond_to?(:url)
              json.set! "#{attr}_url".to_sym, itm.send(attr.to_sym).url
            elsif itm.send(attr.to_sym).is_a? ::BSON::ObjectId
              json.set! attr.to_sym, itm[attr].to_s
            else
              json.set! attr.to_sym, itm[attr]
            end
          end
        end
      end
    end
  end

  @storage.create(filename,serialized)

  return filename
end
import(filename) click to toggle source

Load a snapshot.

Params
filename

name of the snapshot to load

# File lib/snapshotar/core.rb, line 79
def import(filename)
  tree = JSON.load @storage.show(filename)

  tree.each do |key,value|
    clazz = key.constantize

    value.each do |itm|
      item_params = {}
      itm.each do |itm_key,itm_value|

        next if itm_value.nil?

        # handle inheritance
        if (itm_key.to_s == "clazz")
          clazz = itm_value.constantize
          next
        end

        # handle url paths separatley
        if itm_key.to_s.end_with?("_url")
          if File.exist?(itm_value)
            orig_key = itm_key.to_s[0..-5].to_sym
            item_params[orig_key] = File.open(itm_value)
          else  # remote
            item_params["remote_#{itm_key}"] = itm_value
          end
        else
          item_params[itm_key] = itm_value
        end
      end
      clazz.new(item_params).save(validate: false)
    end
  end
end
list() click to toggle source

List all available snapshots.

returns

array of filenames

# File lib/snapshotar/core.rb, line 22
def list
  @storage.index
end