Class: Snapshotar::Core
- Inherits:
-
Object
- Object
- Snapshotar::Core
- Defined in:
- lib/snapshotar/core.rb
Instance Method Summary collapse
-
#delete(filename) ⇒ Object
delete a snapshot.
-
#export(filename = nil) ⇒ Object
Performs a snapshot.
-
#import(filename) ⇒ Object
Load a snapshot.
-
#initialize ⇒ Core
constructor
:nodoc:.
-
#list ⇒ Object
List all available snapshots.
Constructor Details
#initialize ⇒ Core
:nodoc:
10 11 12 13 14 15 |
# File 'lib/snapshotar/core.rb', line 10 def initialize #:nodoc: storage_class = Snapshotar::Storage::S3Storage if Snapshotar.configuration.storage_type == :s3 storage_class = Snapshotar::Storage::FileStorage if Snapshotar.configuration.storage_type == :file @storage = storage_class.new end |
Instance Method Details
#delete(filename) ⇒ Object
delete a snapshot.
- Params
filename
-
name of the snapshot to delete
120 121 122 123 |
# File 'lib/snapshotar/core.rb', line 120 def delete(filename) @storage.delete(filename) end |
#export(filename = nil) ⇒ Object
Performs a snapshot
- Params
filename
-
filename to create or nil to let snapshotar create something
like snapshotar_dump_<timestamp>.json
- returns
-
filename
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# 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) ⇒ Object
Load a snapshot.
- Params
filename
-
name of the snapshot to load
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# 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 ⇒ Object
List all available snapshots.
- returns
-
array of filenames
22 23 24 |
# File 'lib/snapshotar/core.rb', line 22 def list @storage.index end |