Skip to content

Commit

Permalink
Make Storage a subclass of BaseStorage which supports per-instance
Browse files Browse the repository at this point in the history
storage_dir.
  • Loading branch information
Christopher Wade committed Mar 20, 2011
1 parent 658a150 commit 841876c
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions plugins/core/lib/core/plugin/storage.rb
@@ -1,23 +1,18 @@

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

class BaseStorage

# Open a storage file or create it if it doesn't exist.
#
# @param [String] storage_dir the path to the directory the storage file should exist in
# @param [String] name a (short) name, should be suitable for use as a filename
def initialize(name)
def initialize(storage_dir, name)
@storage_dir = storage_dir
@name = name
@mutex = Mutex.new
unless File.exists?(Storage.storage_dir)
FileUtils.mkdir_p(Storage.storage_dir)
unless File.exists?(@storage_dir)
FileUtils.mkdir_p(@storage_dir)
end
rollback
end
Expand Down Expand Up @@ -82,14 +77,28 @@ def keys
private

def path
File.join(Storage.storage_dir, @name + ".yaml")
File.join(@storage_dir, @name + ".yaml")
end

def update_timestamp
@last_modified_time = File.stat(path).mtime
end
end

class Storage < Plugin::BaseStorage
class << self
attr_writer :storage_dir
end

def self.storage_dir
@user_dir ||= File.join(Redcar.user_dir, "storage")
end

def initialize(name)
super(self.class.storage_dir, name)
end
end

# A Storage which is used by multiple plugins. This kind of storage can only
# contain arrays, because otherwise plugins could not set their defaults as
# addition to the existing ones of other plugins.
Expand Down

0 comments on commit 841876c

Please sign in to comment.