Class: Cane::CLI::Translator

Inherits:
Struct
  • Object
show all
Defined in:
lib/cane/cli/translator.rb

Overview

Translates CLI options with given defaults to a hash suitable to be passed to ‘Cane.run`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultsObject

Returns the value of attribute defaults

Returns:

  • (Object)

    the current value of defaults



6
7
8
# File 'lib/cane/cli/translator.rb', line 6

def defaults
  @defaults
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



6
7
8
# File 'lib/cane/cli/translator.rb', line 6

def options
  @options
end

Instance Method Details

#check_disabled(check, params) ⇒ Object



39
40
41
42
43
# File 'lib/cane/cli/translator.rb', line 39

def check_disabled(check, params)
  relevant_options = options.keys & params + [check]

  check == relevant_options[-1]
end

#option_with_default(key) ⇒ Object



45
46
47
# File 'lib/cane/cli/translator.rb', line 45

def option_with_default(key)
  options.fetch(key, defaults.fetch(key))
end

#to_hashObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cane/cli/translator.rb', line 7

def to_hash
  result = {}
  translate_abc_options(result)
  translate_doc_options(result)
  translate_style_options(result)

  result[:threshold] = options.fetch(:threshold, [])
  result[:max_violations] = option_with_default(:max_violations).to_i

  result
end

#translate_abc_options(result) ⇒ Object



19
20
21
22
23
24
# File 'lib/cane/cli/translator.rb', line 19

def translate_abc_options(result)
  result[:abc] = {
    files: option_with_default(:abc_glob),
    max:   option_with_default(:abc_max).to_i
  } unless check_disabled(:no_abc, [:abc_glob, :abc_max])
end

#translate_doc_options(result) ⇒ Object



33
34
35
36
37
# File 'lib/cane/cli/translator.rb', line 33

def translate_doc_options(result)
  result[:doc] = {
    files: option_with_default(:doc_glob),
  } unless check_disabled(:no_doc, [:doc_glob])
end

#translate_style_options(result) ⇒ Object



26
27
28
29
30
31
# File 'lib/cane/cli/translator.rb', line 26

def translate_style_options(result)
  result[:style] = {
    files:   option_with_default(:style_glob),
    measure: option_with_default(:style_measure).to_i,
  } unless check_disabled(:no_style, [:style_glob])
end