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
- #options ⇒ Object
- #opts ⇒ Object
- #process_command ⇒ Object
- #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.
84 85 86 |
# File 'lib/cpf_cnpj/cli.rb', line 84 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
104 105 106 107 108 109 |
# File 'lib/cpf_cnpj/cli.rb', line 104 def format(document) return unless [: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() 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
118 119 120 121 122 123 |
# File 'lib/cpf_cnpj/cli.rb', line 118 def help(run = .empty?) return unless run stderr << opts.to_s exit 1 end |
#input ⇒ Object
61 62 63 |
# File 'lib/cpf_cnpj/cli.rb', line 61 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
125 126 127 |
# File 'lib/cpf_cnpj/cli.rb', line 125 def opts @opts ||= OptionParser.new end |
#process_command ⇒ Object
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. = "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
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cpf_cnpj/cli.rb', line 65 def start process_command help generate() 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 [: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 |