Sha256: c357d0754146af6c189038a4ebf68be0585327ec0260cc7ae80457186bb5446e

Contents?: true

Size: 1.96 KB

Versions: 38

Compression:

Stored size: 1.96 KB

Contents

module Redcar
  class Plugin
    class Storage
      class << self
        attr_writer :storage_dir
      end
    
      def self.storage_dir
        @user_dir ||= File.join(Redcar.user_dir, "storage")
      end

      # Open a storage file or create it if it doesn't exist.
      #
      # @param [String] a (short) name, should be suitable for use as a filename
      def initialize(name)
        @name    = name
        unless File.exists?(Storage.storage_dir)
          FileUtils.mkdir_p(Storage.storage_dir)
        end
        rollback
      end

      # Save the storage to disk.
      def save
        File.open(path, "w") { |f| YAML.dump(@storage, f) }
        update_timestamp
        self
      end

      # Rollback the storage to the latest revision saved to disk or empty it if
      # it hasn't been saved.
      def rollback
        if File.exists?(path)
          @storage = YAML.load_file(path)
          raise 'storage file is corrupted--please delete ' + path unless @storage.is_a? Hash
          update_timestamp
        else
          @storage = {}
        end
        self
      end
      
      # retrieve key value
      # note: it does not re-read from disk before returning you this value
      def [](key)
        if @last_modified_time
          if File.stat(path()).mtime != @last_modified_time
            rollback
          end
        end
        @storage[key]
      end
      
      # set key to value
      # note: it automatically saves this to disk
      def []=(key, value)
        @storage[key] = value
        save
        value
      end
      
      def set_default(key, value)
        unless @storage.has_key?(key)
          self[key] = value
        end
        value
      end
      
      def keys
        @storage.keys
      end
      
      private
      
      def path
        File.join(Storage.storage_dir, @name + ".yaml")
      end
      
      def update_timestamp
        @last_modified_time = File.stat(path()).mtime
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
redcar-0.11.0dev plugins/core/lib/core/plugin/storage.rb
redcar-0.10 plugins/core/lib/core/plugin/storage.rb
redcar-0.9.2 plugins/core/lib/core/plugin/storage.rb
redcar-0.9.1 plugins/core/lib/core/plugin/storage.rb
redcar-0.9.0 plugins/core/lib/core/plugin/storage.rb
redcar-0.8.1 plugins/core/lib/core/plugin/storage.rb
redcar-0.8 plugins/core/lib/core/plugin/storage.rb
redcar-0.7 plugins/core/lib/core/plugin/storage.rb
redcar-0.6.1 plugins/core/lib/core/plugin/storage.rb
redcar-0.6 plugins/core/lib/core/plugin/storage.rb
redcar-0.6.1dev plugins/core/lib/core/plugin/storage.rb
redcar-0.5.1 plugins/core/lib/core/plugin/storage.rb
redcar-0.5 plugins/core/lib/core/plugin/storage.rb
redcar-0.5.6dev plugins/core/lib/core/plugin/storage.rb
redcar-0.5.5dev plugins/core/lib/core/plugin/storage.rb
redcar-0.5.4dev plugins/core/lib/core/plugin/storage.rb
redcar-0.5.3dev plugins/core/lib/core/plugin/storage.rb
redcar-0.5.2dev plugins/core/lib/core/plugin/storage.rb
redcar-0.5.1dev plugins/core/lib/core/plugin/storage.rb
redcar-0.4.1 plugins/core/lib/core/plugin/storage.rb