Class: Secret::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/secret/container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, auto_create = true, chmod = Secret::CHMOD_MODE) ⇒ Container

Initializes a container. Does significant checking on the directory to ensure it is writeable and it exists.

Parameters:

  • directory (String)

    the directory to the container

  • auto_create (Boolean) (defaults to: true)

    if true, will attempt to create the directory if it does not exist.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



57
58
59
60
# 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

Instance Attribute Details

#chmod_modeObject (readonly)

Returns the value of attribute chmod_mode.



4
5
6
# File 'lib/secret/container.rb', line 4

def chmod_mode
  @chmod_mode
end

#directoryObject (readonly)

Returns the value of attribute directory.



4
5
6
# File 'lib/secret/container.rb', line 4

def directory
  @directory
end

#filesObject (readonly)

Returns the value of attribute files.



4
5
6
# File 'lib/secret/container.rb', line 4

def files
  @files
end

Instance Method Details

#contents(path) ⇒ Object



32
33
34
# File 'lib/secret/container.rb', line 32

def contents(path)
  file(path).contents
end

#destroy_all_locks!Integer

Viciously destroys all locks that the file and its containers may have. Use carefully!

Returns:

  • (Integer)

    the number of files destroyed.



75
76
77
78
79
# 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) ⇒ Object

Another container within the directory



53
54
55
# File 'lib/secret/container.rb', line 53

def dir(name)
  Container.new ::File.join(directory, name), true, chmod_mode
end

#file(filename) ⇒ Secret::File

Gets a file stored in the container.

Parameters:

  • filename (Symbol)

    the name of the file.

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
# 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!Object

This should be called once in some sort of initializer.



69
70
71
# File 'lib/secret/container.rb', line 69

def initialize_once!
  destroy_all_locks!
end

#stash(path, contents) ⇒ Object

Stashes the contents of a file



28
29
30
# File 'lib/secret/container.rb', line 28

def stash(path, contents)
  file(path).stash(contents)
end

#uncache!Object

Deletes the cache of objects



64
65
66
# File 'lib/secret/container.rb', line 64

def uncache!
  @files = {}
end