extenso_pt 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.project_alt.json +4 -0
- data/.rubocop.yml +1 -0
- data/.rubocop_todo.yml +74 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +1 -1
- data/Rakefile +8 -6
- data/bin/console +4 -3
- data/extenso_pt.gemspec +23 -21
- data/lib/extenso_pt/version.rb +3 -1
- data/lib/extenso_pt.rb +168 -131
- metadata +14 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10ea3b7fbc48b315312ba6ff3a297884a7eb715bbee0e5b0ff1332672fe66be8
|
4
|
+
data.tar.gz: 77f82a40052f904705bf3fa6e0ffc328006ef22c837fa11fe279317312783a9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a1756b097562327fb5540533ec05a1bba78d1d2614106fc33690b276fc05274c899158c28c81169c165d0414dfd927423a3612421c72a769ccef9ee553c09ec
|
7
|
+
data.tar.gz: 6f9be8e11f8db30bbaedc8ff1e372e665e8d11fdcf160c0bff2563436b7a1963ecc44d5df2236401ad95b408862b65d7ad6736691780c1fd9ceaadba6a4ccaae
|
data/.project_alt.json
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2019-09-25 11:13:11 +0100 using RuboCop version 0.74.0.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 1
|
10
|
+
#Lint/RequireParentheses:
|
11
|
+
# Exclude:
|
12
|
+
# - 'lib/extenso_pt.rb'
|
13
|
+
|
14
|
+
# Offense count: 4
|
15
|
+
#Metrics/AbcSize:
|
16
|
+
# Max: 58
|
17
|
+
|
18
|
+
# Offense count: 3
|
19
|
+
Metrics/CyclomaticComplexity:
|
20
|
+
Max: 17
|
21
|
+
|
22
|
+
# Offense count: 3
|
23
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
24
|
+
Metrics/MethodLength:
|
25
|
+
Max: 24
|
26
|
+
|
27
|
+
# Offense count: 3
|
28
|
+
Metrics/PerceivedComplexity:
|
29
|
+
Max: 19
|
30
|
+
|
31
|
+
# Offense count: 1
|
32
|
+
Naming/ConstantName:
|
33
|
+
Exclude:
|
34
|
+
- 'lib/extenso_pt.rb'
|
35
|
+
|
36
|
+
# Offense count: 39
|
37
|
+
# Configuration parameters: AllowedChars.
|
38
|
+
Style/AsciiComments:
|
39
|
+
Exclude:
|
40
|
+
- 'lib/extenso_pt.rb'
|
41
|
+
- 'test/extenso_pt_test.rb'
|
42
|
+
|
43
|
+
# Offense count: 7
|
44
|
+
Style/ClassVars:
|
45
|
+
Exclude:
|
46
|
+
- 'lib/extenso_pt.rb'
|
47
|
+
|
48
|
+
# Offense count: 6
|
49
|
+
Style/Documentation:
|
50
|
+
Exclude:
|
51
|
+
- 'spec/**/*'
|
52
|
+
- 'test/**/*'
|
53
|
+
- 'lib/extenso_pt.rb'
|
54
|
+
|
55
|
+
# Offense count: 2
|
56
|
+
Style/NestedTernaryOperator:
|
57
|
+
Exclude:
|
58
|
+
- 'lib/extenso_pt.rb'
|
59
|
+
|
60
|
+
# Offense count: 10
|
61
|
+
# Cop supports --auto-correct.
|
62
|
+
# Configuration parameters: AutoCorrect, EnforcedStyle, IgnoredMethods.
|
63
|
+
# SupportedStyles: predicate, comparison
|
64
|
+
Style/NumericPredicate:
|
65
|
+
Exclude:
|
66
|
+
- 'spec/**/*'
|
67
|
+
- 'lib/extenso_pt.rb'
|
68
|
+
|
69
|
+
# Offense count: 53
|
70
|
+
# Cop supports --auto-correct.
|
71
|
+
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
72
|
+
# URISchemes: http, https
|
73
|
+
Metrics/LineLength:
|
74
|
+
Max: 317
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
3
5
|
|
4
6
|
Rake::TestTask.new(:test) do |t|
|
5
|
-
t.libs <<
|
6
|
-
t.libs <<
|
7
|
-
t.test_files = FileList[
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb']
|
8
10
|
end
|
9
11
|
|
10
|
-
task :
|
12
|
+
task default: :test
|
data/bin/console
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'extenso_pt'
|
5
6
|
|
6
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
8
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +11,5 @@ require "extenso_pt"
|
|
10
11
|
# require "pry"
|
11
12
|
# Pry.start
|
12
13
|
|
13
|
-
require
|
14
|
+
require 'irb'
|
14
15
|
IRB.start(__FILE__)
|
data/extenso_pt.gemspec
CHANGED
@@ -1,36 +1,38 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
2
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
-
require
|
5
|
+
require 'extenso_pt/version'
|
4
6
|
|
5
7
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name =
|
8
|
+
spec.name = 'extenso_pt'
|
7
9
|
spec.version = ExtensoPt::VERSION
|
8
|
-
spec.authors = [
|
9
|
-
spec.email = [
|
10
|
+
spec.authors = ['Hernâni Rodrigues Vaz']
|
11
|
+
spec.email = ['hernanirvaz@gmail.com']
|
10
12
|
|
11
|
-
spec.summary =
|
12
|
-
spec.description = spec.summary
|
13
|
+
spec.summary = 'Produz valores monetários por extenso em portugês de portugal ou brasil.'
|
14
|
+
spec.description = spec.summary + ' Os valores podem ser um numerico, uma string de digitos ou um conjunto destes (array, range, hash). O extenso pode ser produzido na escala longa (utilizada em todos os países lusófonos) ou na escala curta (utilizada no Brasil). Pode ainda escolher a moeda e a fração da moeda.'
|
13
15
|
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
16
|
+
spec.homepage = 'https://github.com/hernanilr/extenso_pt'
|
17
|
+
spec.license = 'MIT'
|
16
18
|
|
17
|
-
spec.metadata[
|
18
|
-
#spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
19
|
-
spec.metadata[
|
20
|
-
#spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
21
|
-
#spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
19
|
+
spec.metadata['yard.run'] = 'yard' # use "yard" to build full HTML docs.
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
22
|
+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
23
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
22
24
|
|
23
25
|
# Specify which files should be added to the gem when it is released.
|
24
26
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
-
spec.files
|
27
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
28
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
29
|
end
|
28
|
-
spec.bindir =
|
30
|
+
spec.bindir = 'exe'
|
29
31
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
-
spec.require_paths = [
|
32
|
+
spec.require_paths = ['lib']
|
31
33
|
|
32
|
-
spec.add_development_dependency
|
33
|
-
spec.add_development_dependency
|
34
|
-
spec.add_development_dependency
|
35
|
-
spec.add_development_dependency
|
34
|
+
spec.add_development_dependency 'bigdecimal', '~> 1.4.4'
|
35
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
36
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
37
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
36
38
|
end
|
data/lib/extenso_pt/version.rb
CHANGED
data/lib/extenso_pt.rb
CHANGED
@@ -1,71 +1,154 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'extenso_pt/version'
|
4
|
+
require 'bigdecimal/util'
|
3
5
|
|
4
6
|
# @author Hernâni Rodrigues Vaz
|
5
7
|
module ExtensoPt
|
6
8
|
class Error < StandardError; end
|
7
9
|
|
8
|
-
LC
|
9
|
-
A0020={pt: [
|
10
|
-
|
11
|
-
A0100={pt: [
|
12
|
-
|
13
|
-
A1000={pt: [
|
14
|
-
|
15
|
-
A1e24={pt: [
|
16
|
-
|
17
|
-
|
18
|
-
# Produz o extenso
|
10
|
+
LC = %i[pt br].freeze
|
11
|
+
A0020 = { pt: ['', 'UM', 'DOIS', 'TRÊS', 'QUATRO', 'CINCO', 'SEIS', 'SETE', 'OITO', 'NOVE', 'DEZ', 'ONZE', 'DOZE', 'TREZE', 'CATORZE', 'QUINZE', 'DEZASSEIS', 'DEZASSETE', 'DEZOITO', 'DEZANOVE'],
|
12
|
+
br: ['', 'UM', 'DOIS', 'TRES', 'QUATRO', 'CINCO', 'SEIS', 'SETE', 'OITO', 'NOVE', 'DEZ', 'ONZE', 'DOZE', 'TREZE', 'QUATORZE', 'QUINZE', 'DEZESSEIS', 'DEZESSETE', 'DEZOITO', 'DEZENOVE'] }.freeze
|
13
|
+
A0100 = { pt: ['', '', 'VINTE', 'TRINTA', 'QUARENTA', 'CINQUENTA', 'SESSENTA', 'SETENTA', 'OITENTA', 'NOVENTA'],
|
14
|
+
br: ['', '', 'VINTE', 'TRINTA', 'QUARENTA', 'CINQUENTA', 'SESSENTA', 'SETENTA', 'OITENTA', 'NOVENTA'] }.freeze
|
15
|
+
A1000 = { pt: ['', 'CEM', 'CENTO', 'DUZENTOS', 'TREZENTOS', 'QUATROCENTOS', 'QUINHENTOS', 'SEISCENTOS', 'SETECENTOS', 'OITOCENTOS', 'NOVECENTOS'],
|
16
|
+
br: ['', 'CEM', 'CENTO', 'DUZENTOS', 'TREZENTOS', 'QUATROCENTOS', 'QUINHENTOS', 'SEISCENTOS', 'SETECENTOS', 'OITOCENTOS', 'NOVECENTOS'] }.freeze
|
17
|
+
A1e24 = { pt: ['', 'MIL', ' MILHÃO', ' MIL MILHÃO', ' BILIÃO', ' MIL BILIÃO', ' TRILIÃO', ' MIL TRILIÃO', '', ' MIL', ' MILHÕES', ' MIL MILHÕES', ' BILIÕES', ' MIL BILIÕES', ' TRILIÕES', ' MIL TRILIÕES'],
|
18
|
+
br: ['', 'MIL', ' MILHÃO', ' BILHÃO', ' TRILHÃO', ' QUADRILHÃO', ' QUINTILHÃO', ' SEXTILHÃO', '', ' MIL', ' MILHÕES', ' BILHÕES', ' TRILHÕES', ' QUADRILHÕES', ' QUINTILHÕES', ' SEXTILHÕES'] }.freeze
|
19
|
+
|
20
|
+
# Produz o extenso das centenas em portugûes de portugal ou brasil.
|
21
|
+
#
|
22
|
+
# @param [Integer] mil o valor dum grupo 3 digitos a converter
|
23
|
+
# @return [String] o extenso das centenas
|
24
|
+
def self.e900(mil)
|
25
|
+
A1000[@@lc][(mil > 100 ? 1 : 0) + mil / 100] + (mil > 100 && mil % 100 > 0 ? ' E ' : '')
|
26
|
+
end
|
27
|
+
|
28
|
+
# Produz o extenso das dezenas em portugûes de portugal ou brasil.
|
29
|
+
#
|
30
|
+
# @param [Integer] mil o valor dum grupo 3 digitos a converter
|
31
|
+
# @return [String] o extenso das dezenas
|
32
|
+
def self.e90(mil)
|
33
|
+
A0100[@@lc][mil % 100 / 10] + (mil > 20 && mil % 10 > 0 ? ' E ' : '')
|
34
|
+
end
|
35
|
+
|
36
|
+
# Produz o extenso das unidades em portugûes de portugal ou brasil.
|
19
37
|
#
|
20
38
|
# @param [Integer] mil o valor dum grupo 3 digitos a converter
|
21
|
-
# @
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
39
|
+
# @return [String] o extenso das unidades
|
40
|
+
def self.e9(cem)
|
41
|
+
A0020[@@lc][(cem < 20 ? cem : cem % 10)]
|
42
|
+
end
|
43
|
+
|
44
|
+
# Produz o qualificador grupo de 3 digitos em portugûes de portugal ou brasil.
|
45
|
+
#
|
46
|
+
# @param [Integer] pos posição actual nos grupos 3 digitos do valor monetário
|
47
|
+
# @return [String] o qualificador grupo de 3 digitos
|
48
|
+
def self.e1e24(pos)
|
49
|
+
A1e24[@@lc][@@ai[pos] > 0 ? @@ai[pos] > 1 ? 8 + pos : 0 + pos : 0]
|
50
|
+
end
|
51
|
+
|
52
|
+
# Produz o extenso da parte fracionária dum valor monetário em portugûes de portugal ou brasil.
|
53
|
+
#
|
54
|
+
# @return [String] o extenso da parte fracionária dum valor monetário
|
55
|
+
def self.ef99
|
56
|
+
e90(@@nf) + e9(@@nf) + (@@nf == 1 ? ' ' + @@cs : @@nf > 1 ? ' ' + @@cp : '')
|
57
|
+
end
|
58
|
+
|
59
|
+
# Produz final da moeda em portugûes de portugal ou brasil.
|
60
|
+
#
|
61
|
+
# @return [String] o final da moeda
|
62
|
+
def self.efim
|
63
|
+
# soma todos os grupos do valor monetário
|
64
|
+
sto = @@s6 + @@m6
|
65
|
+
|
66
|
+
(@@s6 == 0 && @@m6 > 0 ? ' DE' : '') + # proposição DE se zero nos primeiros 6 digitos e maiores que 1e6
|
67
|
+
(sto == 1 ? ' ' + @@ms : sto > 1 ? ' ' + @@mp : '') + # singular/plural da moeda
|
68
|
+
(sto > 0 && @@nf > 0 ? ' E ' : '') + # proposição E entre parte inteira e parte fracionária
|
69
|
+
ef99 # extenso da parte fracionária dum valor monetário
|
70
|
+
end
|
71
|
+
|
72
|
+
# Produz o extenso grupo 3 digitos em portugûes de portugal ou brasil.
|
73
|
+
#
|
74
|
+
# @param [Integer] pos posição actual nos grupos 3 digitos do valor monetário
|
75
|
+
# @return [String] o extenso grupo 3 digitos
|
76
|
+
def self.edg3(pos)
|
77
|
+
# extenso do grupo actual 3 digitos
|
78
|
+
pos == 1 && @@ai[pos] == 1 ? e1e24(pos) : e900(@@ai[pos]) + e90(@@ai[pos]) + e9(@@ai[pos] % 100) + e1e24(pos)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Produz separação entre grupos 3 digitos
|
82
|
+
#
|
83
|
+
# @param [Integer] pos posição actual nos grupos 3 digitos do valor monetário
|
84
|
+
# @return [String] separação entre grupos 3 digitos
|
85
|
+
def self.esep(pos)
|
86
|
+
pos > 0 ? @@ai[pos - 1] > 100 ? ' ' : @@ai[pos - 1] > 0 ? ' E ' : '' : ''
|
87
|
+
end
|
88
|
+
|
89
|
+
# Parametrizar grupos 3 digitos para controle de singular/plural
|
90
|
+
#
|
91
|
+
def self.pdg3
|
92
|
+
@@s6 = @@ai[0].to_i + @@ai[1].to_i * 2 # soma grupos 1,2 (primeiros 6 digitos)
|
93
|
+
@@m6 = @@ai[2..-1].to_a.inject(:+).to_i * 2 # soma grupos 3.. (digitos acima de 6)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Parametrizar parte inteira/fracionária do valor monetário
|
97
|
+
#
|
98
|
+
# @param [String] dig string de digitos do valor monetário
|
99
|
+
def self.pintfra(dig)
|
100
|
+
# parte inteira do valor monetário => array grupos 3 digitos ex: 123022.12 => [22, 123]
|
101
|
+
@@ai = dig[/^\d+/].to_s.reverse.scan(/\d{1,3}/).map { |i| i.reverse.to_i }
|
102
|
+
|
103
|
+
# parte fracionária do valor monetário ex: 123022.12 => 12
|
104
|
+
# arredondada a 2 casas decimais (cêntimos/centavos)
|
105
|
+
@@nf = (dig[/\.\d*/].to_f * 100).round
|
106
|
+
end
|
107
|
+
|
108
|
+
private_class_method :pintfra, :pdg3, :esep, :edg3, :e1e24, :e900, :e90, :e9, :ef99, :efim
|
109
|
+
|
110
|
+
# Parametrizar moeda inferindo singular a partir do plural
|
111
|
+
#
|
112
|
+
# @param [Hash] moeda as opcoes para parametrizar a moeda/fração
|
113
|
+
# @option moeda [Symbol] :lc locale do extenso - portugûes de portugal (:pt) ou brasil (:br)
|
114
|
+
# @option moeda [String] :msingular moeda no singular - pode ser inferido do plural menos "S"
|
115
|
+
# @option moeda [String] :fsingular fração no singular - pode ser inferido do plural menos "S"
|
116
|
+
# @option moeda [String] :mplural moeda no plural
|
117
|
+
# @option moeda [String] :fplural fração no plural
|
118
|
+
def self.isingular(moeda)
|
119
|
+
@@ms = moeda[:msingular] || (moeda[:mplural].to_s[-1] == 'S' ? moeda[:mplural][0..-2] : 'EURO')
|
120
|
+
@@cs = moeda[:fsingular] || (moeda[:fplural].to_s[-1] == 'S' ? moeda[:fplural][0..-2] : 'CÊNTIMO')
|
121
|
+
end
|
122
|
+
|
123
|
+
# Parametrizar moeda inferindo plural a partir do singular
|
124
|
+
#
|
125
|
+
# @param [Hash] moeda as opcoes para parametrizar a moeda/fração
|
126
|
+
# @option moeda [Symbol] :lc locale do extenso - portugûes de portugal (:pt) ou brasil (:br)
|
127
|
+
# @option moeda [String] :msingular moeda no singular
|
128
|
+
# @option moeda [String] :fsingular fração no singular
|
129
|
+
# @option moeda [String] :mplural moeda no plural - pode ser inferido do singular mais "S"
|
130
|
+
# @option moeda [String] :fplural fração no plural - pode ser inferido do singular mais "S"
|
131
|
+
def self.iplural(moeda)
|
132
|
+
@@mp = moeda[:mplural] || @@ms + 'S'
|
133
|
+
@@cp = moeda[:fplural] || @@cs + 'S'
|
28
134
|
end
|
29
135
|
|
30
136
|
# Produz o extenso dum valor monetário em portugûes de portugal ou brasil.
|
31
137
|
#
|
32
138
|
# @param [Integer] pos posição actual nos grupos 3 digitos do valor monetário
|
33
139
|
# @param [String] ext extenso actual em construção
|
34
|
-
# @return [String] o extenso dum valor monetário
|
35
|
-
def self.enumerico(pos=0,ext=
|
36
|
-
|
140
|
+
# @return [String] o extenso dum valor monetário
|
141
|
+
def self.enumerico(pos = 0, ext = '')
|
37
142
|
# testa fim do valor monetário
|
38
|
-
if
|
39
|
-
|
40
|
-
|
41
|
-
s06=@@ai[0].to_i+@@ai[1].to_i*2 # grupos 1,2 (primeiros 6 digitos)
|
42
|
-
sm6=@@ai[2..-1].to_a.inject(:+).to_i*2 # grupos 3.. (digitos acima de 6)
|
43
|
-
|
44
|
-
if (s06+sm6+@@nf==0)
|
45
|
-
"ZERO "+@@mp # caso especial de zero
|
46
|
-
else
|
47
|
-
ext+=" DE" if (sm6>0&&s06==0) # proposição DE para >1e6 e zero nos primeiros 6 digitos
|
48
|
-
ext+=" "+@@ms if (s06+sm6==1) # singular da moeda
|
49
|
-
ext+=" "+@@mp if (s06+sm6>1) # plural da moeda
|
50
|
-
ext+=" E " if (s06+sm6>0&&@@nf>0) # proposição E entre parte inteira e parte fracionária
|
51
|
-
ext+=e999(@@nf) # extenso da parte fracionária
|
52
|
-
ext+=" "+@@cs if (@@nf==1) # singular da parte fracionária
|
53
|
-
ext+=" "+@@cp if (@@nf>1) # plural da parte fracionária
|
54
|
-
ext
|
55
|
-
end
|
56
|
-
else
|
57
|
-
# tratamento do grupo actual 3 digitos
|
58
|
-
dg3 =e999(@@ai[pos],pos) # extenso
|
59
|
-
dg3+=A1e24[@@lc][@@ai[pos]>0?@@ai[pos]>1?8+pos:pos:0] # qualificador
|
60
|
-
|
61
|
-
# convenção de separação com grupo anterior 3 digitos
|
62
|
-
if (pos>0)
|
63
|
-
dg3+=" E " if (@@ai[pos-1]<101&&@@ai[pos-1]>0) # grupo < 101 => proposição E
|
64
|
-
dg3+=" " if (@@ai[pos-1]>100) # grupo > 100 => espaço
|
65
|
-
end
|
143
|
+
if pos >= @@ai.count
|
144
|
+
# parametrizar grupos 3 digitos (@@s6, @@m6) para controle de singular/plural
|
145
|
+
pdg3
|
66
146
|
|
147
|
+
# caso especial zero
|
148
|
+
@@s6 == 0 && @@m6 == 0 && @@nf == 0 ? 'ZERO ' + @@mp : ext + efim
|
149
|
+
else
|
67
150
|
# tratamento do proximo grupo 3 digitos
|
68
|
-
enumerico(pos+1,
|
151
|
+
enumerico(pos + 1, edg3(pos) + esep(pos) + ext)
|
69
152
|
end
|
70
153
|
end
|
71
154
|
|
@@ -73,36 +156,24 @@ module ExtensoPt
|
|
73
156
|
#
|
74
157
|
# @param [Object] objeto o objeto a converter pode ser (String, Float, Integer, Array, Range, Hash) ou uma mistura
|
75
158
|
# @return [String, Array<String>, Hash<String>] extenso se o objecto for (String, Float, Integer), Array dos extensos se o objecto for (Array, Range) ou Hash dos extensos se o objecto for (Hash)
|
76
|
-
def self.
|
77
|
-
|
78
|
-
if (obj.kind_of?Hash)
|
159
|
+
def self.pobjeto(obj)
|
160
|
+
if obj.is_a?(Hash)
|
79
161
|
# converte os valores do Hash nos seus extensos mantendo as chaves - devolve um Hash
|
80
|
-
obj.map{|k,v|[k,
|
81
|
-
elsif
|
162
|
+
obj.map { |k, v| [k, pobjeto(v)] }.to_h
|
163
|
+
elsif obj.respond_to? :to_a
|
82
164
|
# converte o objecto num Array e converte os valores do Array nos seus extensos - devolve um Array
|
83
|
-
obj.to_a.map{|
|
165
|
+
obj.to_a.map { |v| pobjeto(v) }
|
84
166
|
else
|
85
|
-
# converte objeto numa string de digitos
|
167
|
+
# converte objeto numa string de digitos
|
86
168
|
# usa bigdecimal/util para evitar aritmética binária (tem problemas com valores >1e12)
|
87
169
|
# qualquer valor não convertivel (ex: texto) resulta em "0.0"
|
88
|
-
|
89
|
-
|
90
|
-
#
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
# arredondada a 2 casas decimais (cêntimos/centavos)
|
96
|
-
# esta variavel é usada no processo de conversão
|
97
|
-
@@nf=(svalor[/\.\d*/].to_f*100).round
|
98
|
-
|
99
|
-
# valores >1e24 não são tratados
|
100
|
-
if (@@ai.count>8)
|
101
|
-
""
|
102
|
-
else
|
103
|
-
# extenso dum numerico
|
104
|
-
enumerico
|
105
|
-
end
|
170
|
+
sdigitos = obj.to_d.to_s('F')
|
171
|
+
|
172
|
+
# parametrizar parte inteira/fracionária (@@ai, @@nf) do valor monetário
|
173
|
+
pintfra(sdigitos)
|
174
|
+
|
175
|
+
# processar extenso - valores superiores a 1e24 não são tratados
|
176
|
+
sdigitos[/^\d+/].length <= 24 ? enumerico : ''
|
106
177
|
end
|
107
178
|
end
|
108
179
|
|
@@ -110,63 +181,29 @@ module ExtensoPt
|
|
110
181
|
#
|
111
182
|
# @param [Hash] moeda as opcoes para parametrizar a moeda/fração
|
112
183
|
# @option moeda [Symbol] :lc locale do extenso - portugûes de portugal (:pt) ou brasil (:br)
|
113
|
-
# @option moeda [String] :msingular moeda no singular
|
114
|
-
# @option moeda [String] :fsingular fração no singular
|
115
|
-
# @option moeda [String] :mplural moeda no plural
|
116
|
-
# @option moeda [String] :fplural fração no plural
|
184
|
+
# @option moeda [String] :msingular moeda no singular
|
185
|
+
# @option moeda [String] :fsingular fração no singular
|
186
|
+
# @option moeda [String] :mplural moeda no plural
|
187
|
+
# @option moeda [String] :fplural fração no plural
|
117
188
|
# @return [String, Array<String>, Hash<String>] extenso se o objecto for (String, Float, Integer), Array dos extensos se o objecto for (Array, Range) ou Hash dos extensos se o objecto for (Hash)
|
118
|
-
def extenso(moeda={lc:
|
189
|
+
def extenso(moeda = { lc: :pt, msingular: 'EURO', fsingular: 'CÊNTIMO' })
|
190
|
+
# parametrização por defeito para :br
|
191
|
+
moeda = { lc: :br, msingular: 'REAL', mplural: 'REAIS', fsingular: 'CENTAVO' } if moeda[:lc] == :br && !moeda[:msingular] && !moeda[:mplural]
|
119
192
|
|
120
|
-
#
|
121
|
-
|
193
|
+
# somente [:pt, :br]
|
194
|
+
@@lc = LC.include?(moeda[:lc]) ? moeda[:lc] : :pt
|
122
195
|
|
123
|
-
#
|
124
|
-
|
125
|
-
|
126
|
-
@@lc=moeda[:lc]
|
127
|
-
else
|
128
|
-
@@lc=:pt
|
129
|
-
end
|
130
|
-
|
131
|
-
# inferir singular da moeda a partir do plural
|
132
|
-
# estas variaveis são usadas no processo de conversão
|
133
|
-
if (moeda[:msingular])
|
134
|
-
@@ms=moeda[:msingular]
|
135
|
-
elsif (moeda[:mplural].to_s[-1]=="S")
|
136
|
-
@@ms=moeda[:mplural][0..-2]
|
137
|
-
else
|
138
|
-
@@ms="EURO"
|
139
|
-
end
|
140
|
-
# inferir singular da fração a partir do plural
|
141
|
-
if (moeda[:fsingular])
|
142
|
-
@@cs=moeda[:fsingular]
|
143
|
-
elsif (moeda[:fplural].to_s[-1]=="S")
|
144
|
-
@@cs=moeda[:fplural][0..-2]
|
145
|
-
else
|
146
|
-
@@cs="CÊNTIMO"
|
147
|
-
end
|
148
|
-
|
149
|
-
# inferir plural da moeda a partir do singular
|
150
|
-
# estas variaveis são usadas no processo de conversão
|
151
|
-
if (moeda[:mplural])
|
152
|
-
@@mp=moeda[:mplural]
|
153
|
-
else
|
154
|
-
@@mp=@@ms+"S"
|
155
|
-
end
|
156
|
-
# inferir plural da fração a partir do singular
|
157
|
-
if (moeda[:fplural])
|
158
|
-
@@cp=moeda[:fplural]
|
159
|
-
else
|
160
|
-
@@cp=@@cs+"S"
|
161
|
-
end
|
196
|
+
# parametrizar moeda
|
197
|
+
ExtensoPt.isingular(moeda)
|
198
|
+
ExtensoPt.iplural(moeda)
|
162
199
|
|
163
|
-
#
|
164
|
-
ExtensoPt.
|
200
|
+
# processar objeto
|
201
|
+
ExtensoPt.pobjeto(self)
|
165
202
|
end
|
166
203
|
end
|
167
|
-
class Hash; include ExtensoPt;end
|
168
|
-
class Array; include ExtensoPt;end
|
169
|
-
class Range; include ExtensoPt;end
|
170
|
-
class Float; include ExtensoPt;end
|
171
|
-
class Integer;include ExtensoPt;end
|
172
|
-
class String; include ExtensoPt;end
|
204
|
+
class Hash; include ExtensoPt; end
|
205
|
+
class Array; include ExtensoPt; end
|
206
|
+
class Range; include ExtensoPt; end
|
207
|
+
class Float; include ExtensoPt; end
|
208
|
+
class Integer; include ExtensoPt; end
|
209
|
+
class String; include ExtensoPt; end
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: extenso_pt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hernâni Rodrigues Vaz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: bigdecimal
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.4.4
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 1.4.4
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '1.17'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '1.17'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,19 +53,19 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: '10.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: '10.0'
|
69
69
|
description: Produz valores monetários por extenso em portugês de portugal ou brasil.
|
70
70
|
Os valores podem ser um numerico, uma string de digitos ou um conjunto destes (array,
|
71
71
|
range, hash). O extenso pode ser produzido na escala longa (utilizada em todos os
|
@@ -78,6 +78,9 @@ extensions: []
|
|
78
78
|
extra_rdoc_files: []
|
79
79
|
files:
|
80
80
|
- ".gitignore"
|
81
|
+
- ".project_alt.json"
|
82
|
+
- ".rubocop.yml"
|
83
|
+
- ".rubocop_todo.yml"
|
81
84
|
- ".travis.yml"
|
82
85
|
- Gemfile
|
83
86
|
- Gemfile.lock
|