Class: Photein::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/photein/config.rb

Constant Summary collapse

OPTIONS =
[
  ['-v',             '--verbose',                 'print verbose output'],
  ['-s SOURCE',      '--source=SOURCE',           'path to the source directory'],
  ['-m MASTER',      '--library-master=MASTER',   'path to a destination directory (master)'],
  ['-d DESKTOP',     '--library-desktop=DESKTOP', 'path to a destination directory (desktop-optimized)'],
  ['-w WEB',         '--library-web=WEB',         'path to a destination directory (web-optimized)'],
  ['-r',             '--recursive',               'ingest source files recursively'],
  ['-k',             '--keep',                    'do not delete source files'],
  ['-i',             '--interactive',             'ask whether to import each file found'],
  ['-n',             '--dry-run',                 'perform a "no-op" trial run'],
  [                  '--safe',                    'skip files in use by other processes'],
].freeze
OPTION_NAMES =
OPTIONS
.flatten
.grep(/^--/)
.map { |option| option[/\w[a-z\-]+/] }
.map(&:to_sym)

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



58
59
60
# File 'lib/photein/config.rb', line 58

def [](key)
  @params[key]
end

.destinationsObject



75
76
77
78
79
80
81
# File 'lib/photein/config.rb', line 75

def destinations
  @destinations ||= {
    master:  @params[:'library-master'],
    desktop: @params[:'library-desktop'],
    web:     @params[:'library-web']
  }.compact.transform_values(&Pathname.method(:new))
end

.method_missing(m, *args, &blk) ⇒ Object



62
63
64
65
# File 'lib/photein/config.rb', line 62

def method_missing(m, *args, &blk)
  m.to_s.tr('_', '-').to_sym
    .then { |key| OPTION_NAMES.include?(key) ? self[key] : super }
end

.parse_opts!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/photein/config.rb', line 36

def parse_opts!
  parser = OptionParser.new do |opts|
    opts.version = Photein::VERSION
    opts.banner  = <<~BANNER
      Usage: photein [--version] [-h | --help] [<args>]
    BANNER

    OPTIONS.each { |opt| opts.on(*opt) }
  end.tap { |p| p.parse!(into: @params) }

  @params[:verbose] ||= @params[:'dry-run']
  @params.freeze

  raise "no source directory given" if !@params.key?(:source)
  (%i[library-master library-desktop library-web] & @params.keys)
    .then { |dest_dirs| raise "no destination directory given" if dest_dirs.empty? }
rescue => e
  warn("#{parser.program_name}: #{e.message}")
  warn(parser.help) if e.is_a?(OptionParser::ParseError)
  exit 1
end

.respond_to_missing?(m, *args) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/photein/config.rb', line 67

def respond_to_missing?(m, *args)
  @params.key?(m.to_s.tr('_', '-').to_sym) || super
end

.set(**params) ⇒ Object



32
33
34
# File 'lib/photein/config.rb', line 32

def set(**params)
  @params.replace(params)
end

.sourceObject



71
72
73
# File 'lib/photein/config.rb', line 71

def source
  @source ||= Pathname(@params[:source])
end