class Secret::Container
Attributes
chmod_mode[R]
directory[R]
files[R]
Public Class Methods
new(directory, auto_create = true, chmod = Secret::CHMOD_MODE)
click to toggle source
Initializes a container. Does significant checking on the directory to ensure it is writeable and it exists. @param [String] directory the directory to the container @param [Boolean] auto_create if true, will attempt to create the directory if it does not exist.
# File lib/secret/container.rb, line 9 def initialize(directory, auto_create = true, chmod = Secret::CHMOD_MODE) @directory = directory @chmod_mode = chmod @files = {} # Do some checking about our directory if ::File.exist?(directory) raise ArgumentError, "Specified directory '#{directory}' is actually a file!" unless ::File.directory?(directory) # Now make our directory if auto_create else raise ArgumentError, "Specified directory '#{directory}' does not exist!" unless auto_create FileUtils.mkdir_p(directory, :mode => chmod_mode) # Only give read/write access to this user end raise ArgumentError, "Directory '#{directory}' is not writeable!" unless ::File.writable?(directory) end
Public Instance Methods
contents(path)
click to toggle source
# File lib/secret/container.rb, line 32 def contents(path) file(path).contents end
destroy_all_locks!()
click to toggle source
Viciously destroys all locks that the file and its containers may have. Use carefully! @return [Integer] the number of files destroyed.
# File lib/secret/container.rb, line 75 def destroy_all_locks! files = Dir[::File.join(directory, '*.lock')] files.each{|f| ::File.delete(f) } return files.count end
dir(name)
click to toggle source
Another container within the directory
# File lib/secret/container.rb, line 53 def dir(name) Container.new ::File.join(directory, name), true, chmod_mode end
file(filename)
click to toggle source
Gets a file stored in the container. @param [Symbol] filename the name of the file. @return [Secret::File] a secret file
# File lib/secret/container.rb, line 39 def file(filename) fn = filename.to_s f = files[fn] return f unless f.nil? d = ::File.dirname(fn) container = d == "." ? self : dir(d) f = Secret::File.new(container, ::File.basename(filename) + Secret::FILE_EXT) files[fn] = f return f end
initialize_once!()
click to toggle source
This should be called once in some sort of initializer.
# File lib/secret/container.rb, line 69 def initialize_once! destroy_all_locks! end
method_missing(meth, *args, &block)
click to toggle source
Calls superclass method
# File lib/secret/container.rb, line 57 def method_missing(meth, *args, &block) super(meth, *args, &block) if args.any? or block_given? return file(meth) end
stash(path, contents)
click to toggle source
Stashes the contents of a file
# File lib/secret/container.rb, line 28 def stash(path, contents) file(path).stash(contents) end
uncache!()
click to toggle source
Deletes the cache of objects
# File lib/secret/container.rb, line 64 def uncache! @files = {} end