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.



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

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



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

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

  stdout << document.formatted
  exit
end

#generate(options) ⇒ Object



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

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



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

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

  stderr << opts.to_s
  exit 1
end

#inputObject



61
62
63
# File 'lib/cpf_cnpj/cli.rb', line 61

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



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

def opts
  @opts ||= OptionParser.new
end

#process_commandObject



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

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



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

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

#strip(document) ⇒ Object



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

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

  stdout << document.stripped
  exit
end

#validate(document) ⇒ Object



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

def validate(document)
  return if document.valid?

  stderr << "Error: Invalid number\n"
  exit 1
end