Class: CpfCnpj::CLI
- Inherits:
-
Object
- Object
- CpfCnpj::CLI
- Defined in:
- lib/cpf_cnpj/cli.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#document_class ⇒ Object
readonly
Returns the value of attribute document_class.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
- #bin_name ⇒ Object
-
#check(_document) ⇒ Object
No-op method.
- #document_name ⇒ Object
- #format(document) ⇒ Object
- #generate(options) ⇒ Object
- #help(run = options.empty?) ⇒ Object
-
#initialize(document_class, arguments, stdin, stdout, stderr) ⇒ CLI
constructor
A new instance of CLI.
-
#input ⇒ Object
rubocop:enable Metrics/MethodLength, Metrics/AbcSize.
- #options ⇒ Object
- #opts ⇒ Object
-
#process_command ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize.
- #start ⇒ Object
- #strip(document) ⇒ Object
- #validate(document) ⇒ Object
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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
5 6 7 |
# File 'lib/cpf_cnpj/cli.rb', line 5 def arguments @arguments end |
#document_class ⇒ Object (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 |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
5 6 7 |
# File 'lib/cpf_cnpj/cli.rb', line 5 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
5 6 7 |
# File 'lib/cpf_cnpj/cli.rb', line 5 def stdin @stdin end |
#stdout ⇒ Object (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_name ⇒ Object
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_name ⇒ Object
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 [: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() return unless [:generate] document = document_class.new(document_class.generate) output = if [: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 = .empty?) return unless run stderr << opts.to_s exit 1 end |
#input ⇒ Object
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 |
#options ⇒ Object
23 24 25 |
# File 'lib/cpf_cnpj/cli.rb', line 23 def @options ||= {} end |
#opts ⇒ Object
126 127 128 |
# File 'lib/cpf_cnpj/cli.rb', line 126 def opts @opts ||= OptionParser.new end |
#process_command ⇒ Object
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. = "Usage: #{bin_name} [options] [#{document_name} number]" opts.separator "" opts.separator "Specific options:" opts.on("-c", "--check", "Check if #{document_name} is valid") do [:check] = true end opts.on("-g", "--generate", "Generate a new #{document_name}") do [:generate] = true end opts.on("-f", "--format", "Format #{document_name} with separators") do [:format] = true end opts.on("-s", "--strip", "Remove #{document_name} separators") do [: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 |
#start ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cpf_cnpj/cli.rb', line 67 def start process_command help generate() 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 [: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 |