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.



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

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.



3
4
5
# File 'lib/cpf_cnpj/cli.rb', line 3

def arguments
  @arguments
end

#document_classObject (readonly)

Returns the value of attribute document_class.



3
4
5
# File 'lib/cpf_cnpj/cli.rb', line 3

def document_class
  @document_class
end

#stderrObject (readonly)

Returns the value of attribute stderr.



3
4
5
# File 'lib/cpf_cnpj/cli.rb', line 3

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



3
4
5
# File 'lib/cpf_cnpj/cli.rb', line 3

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



3
4
5
# File 'lib/cpf_cnpj/cli.rb', line 3

def stdout
  @stdout
end

Instance Method Details

#bin_nameObject



13
14
15
# File 'lib/cpf_cnpj/cli.rb', line 13

def bin_name
  document_name.downcase
end

#check(_document) ⇒ Object

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



74
75
76
# File 'lib/cpf_cnpj/cli.rb', line 74

def check(_document)
  exit
end

#document_nameObject



17
18
19
# File 'lib/cpf_cnpj/cli.rb', line 17

def document_name
  document_class.name
end

#format(document) ⇒ Object



90
91
92
93
# File 'lib/cpf_cnpj/cli.rb', line 90

def format(document)
  stdout << document.formatted
  exit
end

#generate(options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cpf_cnpj/cli.rb', line 78

def generate(options)
  document = document_class.new(document_class.generate)

  if options[:strip]
    stdout << document.stripped
  else
    stdout << document.formatted
  end

  exit
end

#helpObject



100
101
102
103
# File 'lib/cpf_cnpj/cli.rb', line 100

def help
  stderr << opts.to_s
  exit 1
end

#optsObject



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

def opts
  @opts ||= OptionParser.new
end

#startObject



21
22
23
24
25
26
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
60
61
62
63
64
65
# File 'lib/cpf_cnpj/cli.rb', line 21

def start
  options = {}

  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
    exit
  end

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

  help if options.empty?
  generate(options) if options[:generate]
  input = stdin.tty? ? arguments.first : stdin.read
  document = document_class.new(input)
  validate(document)
  format(document) if options[:format]
  strip(document) if options[:strip]
  check(document) if options[:check]
end

#strip(document) ⇒ Object



95
96
97
98
# File 'lib/cpf_cnpj/cli.rb', line 95

def strip(document)
  stdout << document.stripped
  exit
end

#validate(document) ⇒ Object



67
68
69
70
71
# File 'lib/cpf_cnpj/cli.rb', line 67

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