Class: NumRepr::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/numrepr/money.rb,
lib/numrepr/format.rb,
lib/numrepr/decimal.rb,
lib/numrepr/percent.rb

Constant Summary collapse

DASH =

dash for zero in decimal part

false
IDASH =

dash for zero in integer part

false
CHDASH =

dash character (default '-')

"-"
NEG =

negative values: :p, :par = parentheses; :r, :right = right minus

:l
CURSYM =

currency symbol

"$"
SYMPOS =

symbol position: :l/:left (default), :r/:right, :n/:none

:l
SYMSPC =

space before/after currency symbol

false
NUMDIGITS =
nil
POS =
false
GRP =
false
CHGRP =
","
WIDTH =
nil
DEC =

force decimal character

false
CHDEC =

character before decimal places

"."
NUMPLACES =

minimum number of places (builds trailing zeroes)

nil
ZSIGN =

respect zero sign (determinable only in BigDecimal)

false
RWIDTH =

minimum width to the right (builds trailing spaces)

nil
PERCENT =

add percent symbol

false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chars) ⇒ Format

Returns a new instance of Format.



119
120
121
# File 'lib/numrepr/format.rb', line 119

def initialize chars
  @chars = chars
end

Class Method Details

.from_d(d) ⇒ Object Also known as: from_f, from_decimal



187
188
189
190
191
192
# File 'lib/numrepr/format.rb', line 187

def from_d d
  ints, frac = d.make_digits
  i = new ints
  i.do_bd frac, (self::ZSIGN ? d.sign : d)
  i
end

.from_i(n) ⇒ Object



110
111
112
113
114
115
# File 'lib/numrepr/format.rb', line 110

def from_i n
  ints, = n.make_digits
  i = new ints
  i.do_int n.sign, true
  i
end

.from_money(m) ⇒ Object



251
252
253
254
255
256
# File 'lib/numrepr/money.rb', line 251

def from_money m
  ints, frac = m.make_digits
  i = new ints
  i.do_curr frac, m.sign
  i
end

.from_percent(p) ⇒ Object



213
214
215
216
217
# File 'lib/numrepr/percent.rb', line 213

def from_percent p
  i = from_d p.rate
  i.do_percent
  i
end

.method_missing(sym, *args, **kwargs) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/numrepr/format.rb', line 102

def method_missing sym, *args, **kwargs
  if sym =~ /\Abuild_/ then
    (send :"from_#$'", *args, **kwargs).to_s
  else
    super
  end
end

Instance Method Details

#curpostsign(n) ⇒ Object



300
301
302
303
304
305
306
307
# File 'lib/numrepr/money.rb', line 300

def curpostsign n
  if n.negative? then
    case cls::NEG
      when :p, :paren then @chars << ")"
      when :r, :right then @chars << "-"
    end
  end
end

#curpresign(n) ⇒ Object



290
291
292
293
294
295
296
297
298
# File 'lib/numrepr/money.rb', line 290

def curpresign n
  if n.negative? then
    case cls::NEG
      when :p, :paren then @chars << "("
      when :r, :right then nil
      else                 @chars << "-"
    end
  end
end

#cursym(rev) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/numrepr/money.rb', line 270

def cursym rev
  if rev then
    case cls::SYMPOS
    when :n, :none        then return
    when :r, :right       then return
    else                       nil
    end
  else
    case cls::SYMPOS
    when :n, :none        then return
    when :r, :right       then nil
    else                       return
    end
  end
  cs = cls::CURSYM
  cs = cs.reverse if rev
  @chars << " " if cls::SYMSPC
  @chars << cs
end

#dash(need) ⇒ Object



260
261
262
263
264
265
266
267
268
# File 'lib/numrepr/money.rb', line 260

def dash need
  need or return
  if @chars =~ /\A0+\z/ then
    l = @chars.length
    @chars.clear
    @chars << cls::CHDASH
    pad l, " "
  end
end

#do_bd(frac, nsig) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/numrepr/format.rb', line 201

def do_bd frac, nsig
  do_int nsig, frac
  do_decimal frac||"" do |s,f|
    if frac then
      f.pad0 cls::NUMPLACES||0
      f.group
      s.sep f.notempty?
    end
    f.pad_ cls::RWIDTH
    s.pad_ cls::RWIDTH&&1
  end
end

#do_curr(frac, nsig) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/numrepr/money.rb', line 309

def do_curr frac, nsig
  pad0 cls::NUMDIGITS||1
  dash cls::IDASH
  group
  curpresign nsig
  cursym true
  pad_ cls::WIDTH
  reverse!
  do_decimal frac do |s,f|
    f.dash cls::DASH
    f.curpostsign nsig
    f.cursym false
    f.pad_ cls::RWIDTH
    s.sep true
  end
end

#do_decimal(frac) {|sep, frc| ... } ⇒ Object

Yields:



171
172
173
174
175
176
# File 'lib/numrepr/format.rb', line 171

def do_decimal frac
  sep = cls.new ""
  frc = cls.new frac
  yield sep, frc
  @chars << sep.to_s << frc.to_s
end

#do_int(nsig, finite) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/numrepr/format.rb', line 160

def do_int nsig, finite
  if finite then
    pad0 cls::NUMDIGITS||1
    group
  end
  do_sign nsig
  pad_ cls::WIDTH
  reverse!
end

#do_percentObject



221
222
223
# File 'lib/numrepr/percent.rb', line 221

def do_percent
  @chars << "%" if cls::PERCENT
end

#do_sign(n) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/numrepr/format.rb', line 152

def do_sign n
  if    n.negative? then
    @chars << "-"
  elsif n.positive? then
    @chars << "+" if cls::POS
  end
end

#groupObject



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/numrepr/format.rb', line 140

def group
  if cls::GRP then
    l, e = 3, ""
    loop do
      e << (@chars.slice! 0, l)
      break if @chars.empty?
      e << cls::CHGRP
    end
    @chars.replace e
  end
end

#notempty?Boolean

Please load the "supplement" gem.

Returns:

  • (Boolean)


129
# File 'lib/numrepr/format.rb', line 129

def notempty? ; @chars.notempty? rescue @chars.emtpy? ? nil : @chars ; end

#pad(n, chr) ⇒ Object



135
136
137
138
# File 'lib/numrepr/format.rb', line 135

def pad n, chr
  p = n - @chars.length
  @chars << chr * p if p > 0
end

#pad0(n) ⇒ Object



133
# File 'lib/numrepr/format.rb', line 133

def pad0 n ; pad n, "0"      ; end

#pad_(n) ⇒ Object



134
# File 'lib/numrepr/format.rb', line 134

def pad_ n ; pad n, " " if n ; end

#reverse!Object



131
# File 'lib/numrepr/format.rb', line 131

def reverse! ; @chars.reverse! ; end

#sep(needs) ⇒ Object



197
198
199
# File 'lib/numrepr/format.rb', line 197

def sep needs
  @chars << cls::CHDEC if needs or cls::DEC
end

#to_sObject



123
# File 'lib/numrepr/format.rb', line 123

def to_s ; @chars ; end