Class: Resumer::Command::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/resumer/commands/export.rb

Overview

Export a YAML CV to an HTML or PDF file

Instance Method Summary collapse

Constructor Details

#initialize(args = [], options = nil) ⇒ Export

Returns a new instance of Export.

Raises:



10
11
12
13
14
15
16
17
18
19
# File 'lib/resumer/commands/export.rb', line 10

def initialize(args = [], options = nil)
  raise Error, 'No source file given, please provide a YAML source file' \
    unless args.count.positive?

  @export = Resumer::Export.new
  @settings = parse_options(options)
  @src, @dest = parse_args(args)
  @settings[:format] = define_format(@dest) if options.format.nil?
  @settings[:override] = (ask_override if File.exist? @dest) || false
end

Instance Method Details

#ask_overrideObject



77
78
79
80
81
# File 'lib/resumer/commands/export.rb', line 77

def ask_override
  agree('Destination exists, do you want to override? [y/N]', true) do |q|
    q.responses[:not_valid] = 'Please enter "[y]es" or "[n]o".'
  end
end

#define_destination(arg, source) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/resumer/commands/export.rb', line 62

def define_destination(arg, source)
  return File.absolute_path arg if arg

  # Default to source path with export format extension
  File.join(
    File.absolute_path(File.dirname(source)),
    File.basename(source, '.*')
  ) + '.' + @settings[:format].to_s
end

#define_format(destination) ⇒ Object



72
73
74
75
# File 'lib/resumer/commands/export.rb', line 72

def define_format(destination)
  # File extension without leading dot
  File.extname(destination)[/^\.(.*)/, 1].to_sym
end

#define_source(arg) ⇒ Object

Raises:



54
55
56
57
58
59
60
# File 'lib/resumer/commands/export.rb', line 54

def define_source(arg)
  source = File.absolute_path arg
  raise Error, "Source file does not exist: #{source}" \
    unless File.exist? source

  source
end

#load(file) ⇒ Object



83
84
85
86
87
# File 'lib/resumer/commands/export.rb', line 83

def load(file)
  YAML.safe_load(File.read(file), permitted_classes: [Symbol], symbolize_names: true)
rescue StandardError => e
  raise Error, "Failed to read #{file}: #{e.message}"
end

#parse_args(args) ⇒ Object



21
22
23
24
25
# File 'lib/resumer/commands/export.rb', line 21

def parse_args(args)
  source = define_source(args.first)
  destination = define_destination(args[1], source)
  [source, destination]
end

#parse_custom_config(config_file) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/resumer/commands/export.rb', line 38

def parse_custom_config(config_file)
  config_file_path = File.expand_path(config_file)
  config = load(config_file_path)
  return config if config[:theme].nil?

  config[:theme] = parse_custom_theme(config[:theme], config_file_path)
  config
end

#parse_custom_theme(path, base_path) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
# File 'lib/resumer/commands/export.rb', line 47

def parse_custom_theme(path, base_path)
  theme_path = File.expand_path(path, File.dirname(base_path))
  return theme_path if File.exist?(theme_path)

  raise ArgumentError, "Cannot find custom theme '#{theme_path}'"
end

#parse_options(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/resumer/commands/export.rb', line 27

def parse_options(options)
  settings = config = {}
  unless options.format.nil?
    settings[:format] = options.format.downcase.to_sym
  end

  config = parse_custom_config(options.config) unless options.config.nil?

  @export.defaults.merge(config, settings)
end

#runObject

TODO: check the override+file-exists options and throw errors if false+true



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/resumer/commands/export.rb', line 91

def run
  # Ensure source file is valid YAML/CV
  data = load(@src)

  # Ensure destination format is supported
  unless @export.formats.include? @settings[:format]
    raise Error, "Unsupported format: '#{@settings[:format].to_s.upcase}'"
  end

  say(
    "Exporting #{@src} to #{@dest} in #{@settings[:format].to_s.upcase}" \
    " format (override: #{@settings[:override]})"
  )

  @export.run(data, @dest, @settings)
end