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 ⇒ Object
-
#initialize(document_class, arguments, stdin, stdout, stderr) ⇒ CLI
constructor
A new instance of CLI.
- #opts ⇒ 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.
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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
3 4 5 |
# File 'lib/cpf_cnpj/cli.rb', line 3 def arguments @arguments end |
#document_class ⇒ Object (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 |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
3 4 5 |
# File 'lib/cpf_cnpj/cli.rb', line 3 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
3 4 5 |
# File 'lib/cpf_cnpj/cli.rb', line 3 def stdin @stdin end |
#stdout ⇒ Object (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_name ⇒ Object
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_name ⇒ Object
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() document = document_class.new(document_class.generate) if [:strip] stdout << document.stripped else stdout << document.formatted end exit end |
#help ⇒ Object
100 101 102 103 |
# File 'lib/cpf_cnpj/cli.rb', line 100 def help stderr << opts.to_s exit 1 end |
#opts ⇒ Object
105 106 107 |
# File 'lib/cpf_cnpj/cli.rb', line 105 def opts @opts ||= OptionParser.new end |
#start ⇒ Object
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 = {} 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 exit end opts.parse!(arguments) opts.permute!(arguments) help if .empty? generate() if [:generate] input = stdin.tty? ? arguments.first : stdin.read document = document_class.new(input) validate(document) format(document) if [:format] strip(document) if [:strip] check(document) if [: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 |