Class: Money::Formatter
- Inherits:
-
Object
- Object
- Money::Formatter
- Defined in:
- lib/money/money/formatter.rb,
sig/lib/money/money/formatter.rbs
Constant Summary collapse
- DEFAULTS =
{ thousands_separator: "", decimal_mark: ".", }.freeze
Instance Attribute Summary collapse
-
#currency ⇒ Currency
readonly
Returns the value of attribute currency.
-
#money ⇒ Money
readonly
Returns the value of attribute money.
-
#rules ⇒ FormattingRules
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
- #append_currency_symbol(formatted_number) ⇒ string
- #append_sign(formatted_number) ⇒ string
- #decimal_mark ⇒ string (also: #separator)
- #extract_whole_and_decimal_parts ⇒ Array[string]
- #format_decimal_part(value) ⇒ nil, string
- #format_number ⇒ string
- #format_whole_part(value) ⇒ Object
- #free_text ⇒ untyped, "free"
- #html_wrap(string, class_name) ⇒ ::String
-
#initialize(money, *rules) ⇒ String
constructor
Creates a formatted price string according to several rules.
- #lookup(key) ⇒ Object
- #lookup_default(key) ⇒ Object
- #show_free_text? ⇒ Boolean
- #symbol_value_from(rules) ⇒ untyped, ""
- #thousands_separator ⇒ string (also: #delimiter)
- #to_s ⇒ string
Constructor Details
#initialize(money, *rules) ⇒ String
Creates a formatted price string according to several rules.
with the symbol (if present) and %n will be replaced with the number.
Note that the default rules can be defined through Money.default_formatting_rules hash.
190 191 192 193 194 |
# File 'lib/money/money/formatter.rb', line 190 def initialize(money, *rules) @money = money @currency = money.currency @rules = FormattingRules.new(@currency, *rules) end |
Instance Attribute Details
#currency ⇒ Currency (readonly)
Returns the value of attribute currency.
221 222 223 |
# File 'lib/money/money/formatter.rb', line 221 def currency @currency end |
#money ⇒ Money (readonly)
Returns the value of attribute money.
221 222 223 |
# File 'lib/money/money/formatter.rb', line 221 def money @money end |
#rules ⇒ FormattingRules (readonly)
Returns the value of attribute rules.
221 222 223 |
# File 'lib/money/money/formatter.rb', line 221 def rules @rules end |
Instance Method Details
#append_currency_symbol(formatted_number) ⇒ string
273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/money/money/formatter.rb', line 273 def append_currency_symbol(formatted_number) if rules[:with_currency] currency_part = if rules[:html_wrap] html_wrap(currency.to_s, "currency") else currency.to_s end "#{formatted_number} #{currency_part}" else formatted_number end end |
#append_sign(formatted_number) ⇒ string
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/money/money/formatter.rb', line 246 def append_sign(formatted_number) sign = money.negative? ? "-" : "" if rules[:sign_positive] == true && money.positive? sign = "+" end if rules[:sign_before_symbol] == true sign_before = sign sign = "" end symbol_value = symbol_value_from(rules) if symbol_value && !symbol_value.empty? if rules[:html_wrap] symbol_value = html_wrap(symbol_value, "currency-symbol") end rules[:format] .gsub("%u", [sign_before, symbol_value].join) .gsub("%n", [sign, formatted_number].join) else "#{sign_before}#{sign}#{formatted_number}" end end |
#decimal_mark ⇒ string Also known as: separator
212 213 214 |
# File 'lib/money/money/formatter.rb', line 212 def decimal_mark lookup :decimal_mark end |
#extract_whole_and_decimal_parts ⇒ Array[string]
307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/money/money/formatter.rb', line 307 def extract_whole_and_decimal_parts fractional = money.fractional.abs # Round the infinite precision part if needed fractional = fractional.round if rules[:rounded_infinite_precision] # Translate subunits into units fractional_units = BigDecimal(fractional) / currency.subunit_to_unit # Split the result and return whole and decimal parts separately fractional_units.to_s("F").split(".") end |
#format_decimal_part(value) ⇒ nil, string
320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/money/money/formatter.rb', line 320 def format_decimal_part(value) return nil if currency.decimal_places == 0 && !Money.default_infinite_precision return nil if rules[:no_cents] return nil if rules[:no_cents_if_whole] && value.to_i == 0 # Pad value, making up for missing zeroes at the end value = value.ljust(currency.decimal_places, "0") # Drop trailing zeros if needed value.gsub!(/0*$/, "") if rules[:drop_trailing_zeros] value.empty? ? nil : value end |
#format_number ⇒ string
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/money/money/formatter.rb', line 223 def format_number whole_part, decimal_part = extract_whole_and_decimal_parts # Format whole and decimal parts separately decimal_part = format_decimal_part(decimal_part) whole_part = format_whole_part(whole_part) # Assemble the final formatted amount if rules[:html_wrap] if decimal_part.nil? html_wrap(whole_part, "whole") else [ html_wrap(whole_part, "whole"), html_wrap(decimal_mark, "decimal-mark"), html_wrap(decimal_part, "decimal"), ].join end else [whole_part, decimal_part].compact.join(decimal_mark) end end |
#format_whole_part(value) ⇒ Object
300 301 302 303 304 305 |
# File 'lib/money/money/formatter.rb', line 300 def format_whole_part(value) # Apply thousands_separator value.gsub(rules[:delimiter_pattern]) do |digit_to_delimit| "#{digit_to_delimit}#{thousands_separator}" end end |
#free_text ⇒ untyped, "free"
296 297 298 |
# File 'lib/money/money/formatter.rb', line 296 def free_text rules[:display_free].respond_to?(:to_str) ? rules[:display_free] : "free" end |
#html_wrap(string, class_name) ⇒ ::String
292 293 294 |
# File 'lib/money/money/formatter.rb', line 292 def html_wrap(string, class_name) "<span class=\"money-#{class_name}\">#{string}</span>" end |
#lookup(key) ⇒ Object
334 335 336 337 338 |
# File 'lib/money/money/formatter.rb', line 334 def lookup(key) return rules[key] || DEFAULTS[key] if rules.has_key?(key) lookup_default key end |
#lookup_default(key) ⇒ Object
340 341 342 |
# File 'lib/money/money/formatter.rb', line 340 def lookup_default(key) (Money.locale_backend && Money.locale_backend.lookup(key, currency)) || DEFAULTS[key] end |
#show_free_text? ⇒ Boolean
288 289 290 |
# File 'lib/money/money/formatter.rb', line 288 def show_free_text? money.zero? && rules[:display_free] end |
#symbol_value_from(rules) ⇒ untyped, ""
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/money/money/formatter.rb', line 344 def symbol_value_from(rules) if rules.has_key?(:symbol) if rules[:symbol] == true if rules[:disambiguate] && currency.disambiguate_symbol currency.disambiguate_symbol else money.symbol end elsif rules[:symbol] rules[:symbol] else "" end elsif rules[:html_wrap] currency.html_entity == "" ? currency.symbol : currency.html_entity elsif rules[:disambiguate] && currency.disambiguate_symbol currency.disambiguate_symbol else money.symbol end end |
#thousands_separator ⇒ string Also known as: delimiter
204 205 206 207 208 209 210 |
# File 'lib/money/money/formatter.rb', line 204 def thousands_separator val = lookup :thousands_separator return val unless val == true lookup_default :thousands_separator end |
#to_s ⇒ string
196 197 198 199 200 201 202 |
# File 'lib/money/money/formatter.rb', line 196 def to_s return free_text if show_free_text? result = format_number formatted = append_sign(result) append_currency_symbol(formatted) end |