rupica 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rupica.rb +205 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 97c1e1736f26f0f0b022341a05923aebc18edda0df3f2c51fe7eeb7c24673366
4
+ data.tar.gz: 28bd82c9bc98859c22c26f6c849dfbdfafa38e8c2b456c1278b12ea6abdb1821
5
+ SHA512:
6
+ metadata.gz: 43ffdee15566fa7b2dc68ac6fd9d34481e533a10793b094ac7ef2ca9638ff26e49e16bbae18aa932e56b12e2596450373f06f18e758be8f15e0d8b3a7e2df26c
7
+ data.tar.gz: 282d4446ecacfd953dc67850702550aec34ea8ce1fb209d1d55ee3a41b87c5d97ddd9bb079ddfe64fc568317dc2df650302bb50c19fbd82ee9a1536d2e03a295
data/lib/rupica.rb ADDED
@@ -0,0 +1,205 @@
1
+
2
+ # OK: Stampa di formula e risultato
3
+ # OK -- TODO: Allieamento di stringa, padding a destra
4
+ # OK -- TODO: thousand separator a scelta
5
+ # TODO: prendere il binding che fa riferimento alla zona di codice
6
+ # da dove è partita la valutazione del numero.
7
+ # TODO: formattazione interi
8
+ # TODO: formattazione stringhe
9
+ # TODO: formattazione currency con il simbolo $ o €, alla fine
10
+ # TODO: unità di misura,
11
+ # . si potrebbero togliere : se siamo dentro una stringa
12
+ # Si potrebbero inserire come unicode middle dot U+00B7
13
+ # 10·kg e poi definendo "·" come metodo ... però poi necessita una spazio
14
+ # per passare "kg" come parametro, il che è scomodo
15
+ #
16
+
17
+ # . Matches formatStrings representing floats and knows how to transform them
18
+ # . formatString: {EXPR:FORMAT}, { EXPR : FORMAT }
19
+ # . {EXPR:FORMAT} = { EXPR : FORMAT } :: white tolenrance on borders
20
+ # . FORMAT: f :: print flot with default decimal digits
21
+ # . FORMAT: .2f :: print the flot with 2 decimal digits
22
+ # . FORMAT: 10.2f :: pad with space the result on the right, make it 10 chars long
23
+ # . FORMAT: ,10.2f :: print thousand separator
24
+ # . FORMAT: =,10.2f :: print expression = result
25
+
26
+
27
+
28
+ module Rupica
29
+ nil
30
+ end
31
+
32
+ # . The module Rupica::Format is about formatting strings
33
+ # the formatting option are largely inspired by Python f-strings
34
+ module Rupica
35
+ module Format
36
+ @@thousandSeparator = ","
37
+ @@decimalSeparator = "."
38
+ def self.decimalSeparator()
39
+ @@decimalSeparator
40
+ end
41
+ def self.setDecimalSeparator(str)
42
+ @@decimalSeparator = str
43
+ end
44
+ def self.thousandSeparator()
45
+ @@thousandSeparator
46
+ end
47
+ def self.setThousandSeparator(str)
48
+ @@thousandSeparator = str
49
+ end
50
+ def self.resetDefault()
51
+ @@thousandSeparator = ","
52
+ @@decimalSeparator = "."
53
+ end
54
+ end
55
+ end
56
+
57
+ # . Transform a string using all formatters know to Rupica::Format
58
+ # . Search into the string 'str' all elements like: '{xxx:yyy}'
59
+ # . If no formatter recognizes the expression '{xxx:yyy}' then it is
60
+ # left as it is
61
+ # . eg. : Rupica::Format.format(" gello {123 : f}")
62
+ module Rupica::Format
63
+ def self.format(str)
64
+ str.gsub(/{(.*?):(.*?)}/) { |x|
65
+ if FormFloat.match(x) then
66
+ tmp = FormFloat.new(x)
67
+ tmp.format()
68
+ else
69
+ x
70
+ end
71
+ }
72
+ end
73
+ end
74
+
75
+
76
+
77
+ # . the ::String is used instead of String because in iRuby
78
+ # we are by defalt in Module Object.
79
+ puts "ATTENTION: Adding instance method 'f' to builtin class String !"
80
+ class ::String
81
+ def f()
82
+ Rupica::Format.format(self)
83
+ end
84
+ end
85
+
86
+
87
+
88
+ # The FormFloat class is to format floating point values
89
+ # It is part of the Rupica::Format module
90
+ class Rupica::Format::FormFloat
91
+ # @@floatRe = /^{(?'expr'.*?):\s*(?'thousand',)?(?'size'\d{0,20}\.\d{1,20})?f\s*}\s*$/
92
+ @@floatRe = /^{(?'expr'.*?):\s*(?'equal'=)?(?'thousand',)?(?'size'\d{0,20}\.\d{1,20})?f\s*}\s*$/
93
+ attr_reader :str, :paddingNum, :thousand, :decimalsNum, :expr, :exprEval
94
+ def self.match(str)
95
+ m = str.match /#{@@floatRe}/
96
+ end
97
+ def initialize(str)
98
+ # . if the expression has not the correct syntax raise an exception
99
+ @str = str
100
+ @paddingNum = nil
101
+ @thousand = nil
102
+ @decimalsNum = nil
103
+ @expr = nil
104
+ @exprEvaluated = nil
105
+ @equal = nil
106
+ m = str.match /#{@@floatRe}/
107
+ # . raise exception is the "str" has not a known syntax
108
+ raise "Error: unrecognized format '#{str}' " if m.nil?
109
+ @expr = m['expr'].strip
110
+ # . raise exception if 'expr' does not evaluate a float (numeric value)
111
+ begin
112
+ @exprEvaluated = eval("#{@expr}")
113
+ if not @exprEvaluated.is_a? Numeric then
114
+ raise "FormFloat.ERR: expression does not produce a numeric value" ; end
115
+ rescue Exception => ex
116
+ raise "FormFoat.ERR: the expression '#{@expr}' does not evaluate to a float. "
117
+ end
118
+ @thousand = m['thousand']
119
+ @equal = m['equal']
120
+ # . capture elements NN.MMM
121
+ if m['size'] then
122
+ m2 = m['size'].match /(?'padding'\d*)\.(?'decimals'\d+)/
123
+ @paddingNum = m2['padding']
124
+ @decimalsNum = m2['decimals']
125
+ end
126
+ end
127
+ # . convert the Numeric value to a string and set the righ number of
128
+ # decimals.
129
+ # . if there are no decimales specified then we set them t 5 values
130
+ def fixDecimals
131
+ if @decimalsNum == nil then
132
+ out = sprintf("%.5f", @exprEvaluated)
133
+ else
134
+ out = sprintf("%.#{@decimalsNum}f", @exprEvaluated)
135
+ end
136
+ end
137
+ #
138
+ def commaSeparate(str)
139
+ int,flo = str.split(/\./)
140
+ int2 = int.nil?() ? "" : int
141
+ flo2 = flo.nil?() ? "" : flo
142
+ # puts "int2: #{int2}"
143
+ # puts "flo2: #{flo2}"
144
+ int2 = int2.reverse.gsub(/(\d{3})(?=\d)/,'\1,').reverse
145
+ out = ""
146
+ if (int2 == "" and flo2 != "") then out = ".#{flo2}"
147
+ elsif (int2 != "" and flo2 == "") then out = int2
148
+ else out = "#{int2}.#{flo2}"
149
+ end
150
+ return out
151
+ end
152
+ # class method, test if an input string produce the desiderd output
153
+ def self.test(input, output, comment="", debug=false)
154
+ myObj = self.new(input)
155
+ myOut = myObj.format()
156
+ testOut = (myOut == output)
157
+ if testOut then
158
+ puts %Q@ --- TEST PASSED ------ ::: #{comment}"
159
+ IN: '#{input}'
160
+ OUT: '#{myOut}'
161
+ WANTED: '#{output}' @
162
+ else
163
+ # puts "FAILD-- IN: '#{input}' OUT: '#{myOut}' WANTED: '#{output}' ::: #{comment}"
164
+ puts %Q@ *** TEST FAILED ****** ::: #{comment}"
165
+ IN: '#{input}'
166
+ OUT: '#{myOut}'
167
+ WANTED: '#{output}' @
168
+ end
169
+ pp myObj if debug
170
+ return testOut
171
+ end
172
+ # . give the output representation of the float as e.g. "1,234.23"
173
+ # changes "." and "," to what are the set proper constants in the module
174
+ def adaptDotAndComma(str)
175
+ # . to avoid the mosti tipical problem we change firt to letters
176
+ out = str
177
+ out.gsub!(/\,/,"COMMA")
178
+ out.gsub!(/\./,"DOT")
179
+ out.gsub!(/COMMA/, Rupica::Format.thousandSeparator )
180
+ out.gsub!(/DOT/, Rupica::Format.decimalSeparator )
181
+ return out
182
+ end
183
+ # . padding right
184
+ def adaptRightPadding(str, numSpaces)
185
+ out = str.rjust(numSpaces)
186
+ return out
187
+ end
188
+ def format
189
+ # . convert @exprEvaluated to string and set the right number of decimals
190
+ out = self.fixDecimals
191
+ # . put thousand separator, if asked
192
+ out = self.commaSeparate(out) if @thousand != nil
193
+ # . adapt decimals and thousand separator
194
+ out = self.adaptDotAndComma(out)
195
+ # . pad with spaces, if asked
196
+ out = self.adaptRightPadding(out, @paddingNum.to_i) if @paddingNum != nil
197
+ # . write the expression, equal and result, if asked
198
+ out = "#{@expr} = #{out}" if @equal != nil
199
+ return out
200
+ end
201
+ end
202
+ # end
203
+
204
+
205
+
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rupica
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Dr. Nicola Mingotti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: nmingotti@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/rupica.rb
20
+ homepage: https://rubygems.org/gems/rupica
21
+ licenses:
22
+ - GPL-2.0
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.3.15
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Tools to make Ruby better for calculations.
43
+ test_files: []