Sha256: cc2245ffc01d0ca66c7ded52394774e71ff1476de162db824e99992f76fdda06

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

# encoding: UTF-8
require 'fileutils'

module Boxafe

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

    def initialize options = {}
      raise OptionError.new("The :name option is required", :name) unless options[:name]
      @options = options
      @validator = Validator.new raise_first: true
    end

    def name
      @options[:name]
    end

    def command
      encfs.command
    end

    def mount
      @validator.validate mount_options
      ensure_mount_point
      system command
    end

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

    def mounted?
      File.directory? mount_options[:mount]
    end

    def mount_options
      default_mount_options.merge(@options).tap do |options|
        options[:keychain] = @options[:name] if options[:keychain] == true
      end
    end

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

    private

    def ensure_mount_point
      FileUtils.mkdir_p mount_options[:mount]
    end

    def encfs
      Boxafe::Encfs.new mount_options
    end
    
    def default_mount_options
      name = @options[:name]
      {
        # TODO: change default root and mount dirs depending on host os
        root: "~/Dropbox/#{name}",
        mount: "/Volumes/#{name}",
        volume: name
      }
    end

    class DSL

      def initialize options
        @options = options
      end

      OPTION_KEYS.each{ |name| define_method(name){ |value| @options[name] = value } }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
boxafe-0.1.6 lib/boxafe/box.rb