Sha256: ef1428ad6424462aa1a7ff0fe6fbacab93afb5e7604a6e88ff68d32c1354868e

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

module Snapshotar
  module Storage

    class FileStorage

      ##
      # Use this property to specify the local path where snapshots are stored.
      # Default:: +tmp/+
      #
      attr_accessor :base_path

      def initialize(path = nil) #:nodoc:
        @base_path = path || "tmp"

        # create tmp dir if it's not existent
        unless File.directory?(@base_path)
          FileUtils.mkdir_p(@base_path)
        end
      end

      ##
      # lists available snapshots in this storage.
      #
      # returns:: array of filenames
      #
      def index
        Dir["#{@base_path}/*.json"].map{|p| File.basename(p)}
      end

      ##
      # loads a snapshot specified by the given +filename+.
      #
      # Params::
      #  +filename+:: name of the snapshot to load
      #
      # returns:: still seralized json
      #
      def show(filename)
        File.read File.join(@base_path, filename)
      end

      ##
      # creates a snapshot specified by the given +filename+ with data provided
      #
      # Params::
      #  +filename+:: name of the snapshot to create
      #  +serialized_tree+:: json serialized data
      #
      def create(filename,serialized_tree)
        File.open(File.join(@base_path, filename),"w") do |f|
          f.write(serialized_tree)
        end
      end

      ##
      # deletes a snapshot specified by the given +filename+.
      #
      # Params::
      #  +filename+:: name of the snapshot to delete
      #
      def delete(filename)
        File.delete File.join(@base_path, filename)
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
snapshotar-0.0.7 lib/snapshotar/storage/file_storage.rb
snapshotar-0.0.6 lib/snapshotar/storage/file_storage.rb
snapshotar-0.0.4 lib/snapshotar/storage/file_storage.rb
snapshotar-0.0.3 lib/snapshotar/storage/file_storage.rb
snapshotar-0.0.2 lib/snapshotar/storage/file_storage.rb
snapshotar-0.0.1 lib/snapshotar/storage/file_storage.rb