Class: CpfCnpj::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/cpf_cnpj/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document_class, arguments, stdin, stdout, stderr) ⇒ CLI

Returns a new instance of CLI.



7
8
9
10
11
12
13
# File 'lib/cpf_cnpj/cli.rb', line 7

def initialize(document_class, arguments, stdin, stdout, stderr)
  @document_class = document_class
  @arguments = arguments
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



5
6
7
# File 'lib/cpf_cnpj/cli.rb', line 5

def arguments
  @arguments
end

#document_classObject (readonly)

Returns the value of attribute document_class.



5
6
7
# File 'lib/cpf_cnpj/cli.rb', line 5

def document_class
  @document_class
end

#stderrObject (readonly)

Returns the value of attribute stderr.



5
6
7
# File 'lib/cpf_cnpj/cli.rb', line 5

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



5
6
7
# File 'lib/cpf_cnpj/cli.rb', line 5

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



5
6
7
# File 'lib/cpf_cnpj/cli.rb', line 5

def stdout
  @stdout
end

Instance Method Details

#bin_nameObject



15
16
17
# File 'lib/cpf_cnpj/cli.rb', line 15

def bin_name
  document_name.downcase
end

#check(_document) ⇒ Object

No-op method. CPF is always validated on CpfCnpj::CLI#start.



85
86
87
# File 'lib/cpf_cnpj/cli.rb', line 85

def check(_document)
  exit
end

#document_nameObject



19
20
21
# File 'lib/cpf_cnpj/cli.rb', line 19

def document_name
  document_class.name
end

#format(document) ⇒ Object



105
106
107
108
109
110
# File 'lib/cpf_cnpj/cli.rb', line 105

def format(document)
  return unless options[:format]

  stdout << document.formatted
  exit
end

#generate(options) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cpf_cnpj/cli.rb', line 89

def generate(options)
  return unless options[:generate]

  document = document_class.new(document_class.generate)

  output =  if options[:strip]
              document.stripped
            else
              document.formatted
            end

  stdout << output

  exit
end

#help(run = options.empty?) ⇒ Object



119
120
121
122
123
124
# File 'lib/cpf_cnpj/cli.rb', line 119

def help(run = options.empty?)
  return unless run

  stderr << opts.to_s
  exit 1
end

#inputObject

rubocop:enable Metrics/MethodLength, Metrics/AbcSize



63
64
65
# File 'lib/cpf_cnpj/cli.rb', line 63

def input
  stdin.tty? ? arguments.first : stdin.read
end

#optionsObject



23
24
25
# File 'lib/cpf_cnpj/cli.rb', line 23

def options
  @options ||= {}
end

#optsObject



126
127
128
# File 'lib/cpf_cnpj/cli.rb', line 126

def opts
  @opts ||= OptionParser.new
end

#process_commandObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



28
29
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
60
# File 'lib/cpf_cnpj/cli.rb', line 28

def process_command
  opts.banner = "Usage: #{bin_name} [options] [#{document_name} number]"
  opts.separator ""
  opts.separator "Specific options:"

  opts.on("-c", "--check", "Check if #{document_name} is valid") do
    options[:check] = true
  end

  opts.on("-g", "--generate", "Generate a new #{document_name}") do
    options[:generate] = true
  end

  opts.on("-f", "--format", "Format #{document_name} with separators") do
    options[:format] = true
  end

  opts.on("-s", "--strip", "Remove #{document_name} separators") do
    options[:strip] = true
  end

  opts.on("-v", "--version", "Show version") do
    stdout << VERSION
    exit
  end

  opts.on_tail("-h", "--help", "Show help") do
    help(true)
  end

  opts.parse!(arguments)
  opts.permute!(arguments)
end

#startObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/cpf_cnpj/cli.rb', line 67

def start
  process_command
  help
  generate(options)
  document = document_class.new(input)
  validate(document)
  format(document)
  strip(document)
  check(document)
end

#strip(document) ⇒ Object



112
113
114
115
116
117
# File 'lib/cpf_cnpj/cli.rb', line 112

def strip(document)
  return unless options[:strip]

  stdout << document.stripped
  exit
end

#validate(document) ⇒ Object



78
79
80
81
82
# File 'lib/cpf_cnpj/cli.rb', line 78

def validate(document)
  return if document.valid?
  stderr << "Error: Invalid number\n"
  exit 1
end