Class: Imapcli::OptionValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/imapcli/option_validator.rb

Overview

Utility class to validate user options

Invalid options will trigger a runtime exception (which is handled by GLI).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptionValidator

Returns a new instance of OptionValidator.



8
9
10
11
# File 'lib/imapcli/option_validator.rb', line 8

def initialize
  @errors = []
  @warnings = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/imapcli/option_validator.rb', line 6

def errors
  @errors
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/imapcli/option_validator.rb', line 6

def options
  @options
end

#warningsObject (readonly)

Returns the value of attribute warnings.



6
7
8
# File 'lib/imapcli/option_validator.rb', line 6

def warnings
  @warnings
end

Instance Method Details

#global_options_valid?(global_options) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/imapcli/option_validator.rb', line 13

def global_options_valid?(global_options)
  if global_options[:s].nil? || global_options[:s].empty?
    @errors << 'missing server name (use -s option or set IMAP_SERVER environment variable)'
  end
  if global_options[:u].nil? || global_options[:u].empty?
    @errors << 'missing user name (use -u option or set IMAP_USER environment variable)'
  end
  if global_options[:P] && global_options[:p]
    @errors << '-p and -P options do not agree'
  end

  pass?
end

#pass?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/imapcli/option_validator.rb', line 61

def pass?
  @errors.empty?
end

#stats_options_valid?(options, args) ⇒ true false

Validates options for the stats command.

Returns:

  • (true false)

    indicating success or failure; warnings can be accessed as attribute



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/imapcli/option_validator.rb', line 30

def stats_options_valid?(options, args)
  @options = {}
  raise 'incompatible options -r/--recurse and -R/--no_recurse' if options[:r] && options[:R]

  if options[:recurse]
    if args.empty?
      @warnings << 'warning: superfluous -r/--recurse option; will recurse from root by default'
    else
      @options[:depth] = -1
    end
  elsif options[:no_recurse]
    if args.empty?
      @options[:depth] = 0
    else
      @warnings << 'warning: superfluous -R/--no_recurse option; will not recurse from non-root mailbox by default'
    end
  end

  if options[:sort]
    available_sort_options = %w(count total_size min_size q1 median_size q3 max_size)
    if available_sort_options.include? options[:sort].downcase
      @options[:sort] = options[:sort].to_sym
    else
      # GLI did not print the available options even with the :must_match option
      @errors << "sort option must be one of: #{available_sort_options.join(', ')}"
    end
  end

  pass?
end

#warnings?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/imapcli/option_validator.rb', line 65

def warnings?
  @warnings.count > 0
end