Class: Boxafe::Box

Inherits:
Object
  • Object
show all
Defined in:
lib/boxafe/box.rb

Defined Under Namespace

Classes: DSL

Constant Summary collapse

OPTION_KEYS =
[ :name, :root, :mount, :volume, :encfs_config, :keychain ]

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Box

Returns a new instance of Box.

Raises:



8
9
10
11
12
13
# File 'lib/boxafe/box.rb', line 8

def initialize options = {}

  raise Boxafe::Error, "The :name option is required" unless options[:name]

  @options = options
end

Instance Method Details

#configure(options = {}, &block) ⇒ Object



80
81
82
83
84
# File 'lib/boxafe/box.rb', line 80

def configure options = {}, &block
  @options.merge! options
  DSL.new(@options).instance_eval &block if block
  self
end

#description(verbose = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/boxafe/box.rb', line 38

def description verbose = false
  opts = options
  String.new.tap do |s|

    s << Paint["## #{name}", :cyan, :bold]
    s << "\nEncrypted Root: "
    s << if File.directory?(opts[:root])
      Paint["#{opts[:root]}", :green]
    elsif File.exists?(opts[:root])
      Paint["#{opts[:root]} (not a directory)", :red]
    else
      Paint["#{opts[:root]} (doesn't exist)", :red]
    end

    s << "\nMount Point: "
    s << case mount_status
    when :mounted
      Paint["#{opts[:mount]}", :green]
    when :invalid
      Paint["#{opts[:mount]} (not a directory)", :red]
    else
      Paint["#{opts[:mount]} (not mounted)", :yellow]
    end

    s << "\nVolume Name: #{opts[:volume]}"
    s << "\nKeychain Password: #{opts[:keychain]}" if opts[:keychain]
    s << "\nEncFS Config: #{opts[:encfs_config]}" if opts[:encfs_config]

    s << "\nCommand: #{Paint[encfs.command, :yellow]}" if verbose
  end
end

#encfsObject



30
31
32
# File 'lib/boxafe/box.rb', line 30

def encfs
  Boxafe::Encfs.new options
end

#ensure_mount_pointObject



34
35
36
# File 'lib/boxafe/box.rb', line 34

def ensure_mount_point
  FileUtils.mkdir_p options[:mount]
end

#mountObject



19
20
21
# File 'lib/boxafe/box.rb', line 19

def mount
  system encfs.command
end

#mount_statusObject



70
71
72
73
74
75
76
77
78
# File 'lib/boxafe/box.rb', line 70

def mount_status
  if File.directory? options[:mount]
    :mounted
  elsif File.exists? options[:mount]
    :invalid
  else
    :unmounted
  end
end

#nameObject



15
16
17
# File 'lib/boxafe/box.rb', line 15

def name
  @options[:name]
end

#optionsObject



86
87
88
89
90
91
92
93
94
# File 'lib/boxafe/box.rb', line 86

def options
  @options.tap do |opts|
    opts[:root] = File.expand_path opts[:root] || "~/Dropbox/#{opts[:name]}"
    opts[:mount] = File.expand_path opts[:mount] || "/Volumes/#{opts[:name]}"
    opts[:encfs_config] = File.expand_path opts[:encfs_config] if opts[:encfs_config]
    opts[:volume] ||= opts[:name]
    opts[:keychain] = opts[:name] if opts[:keychain] == true
  end
end

#unmountObject



23
24
25
26
27
28
# File 'lib/boxafe/box.rb', line 23

def unmount
  opts = options
  result = system "#{opts[:umount]} #{opts[:mount]}"
  sleep opts[:umount_delay] if opts[:umount_delay] and opts[:umount_delay] > 0
  result
end