Class: SitemapGenerator::BigDecimal

Inherits:
BigDecimal
  • Object
show all
Defined in:
lib/sitemap_generator/core_ext/big_decimal.rb

Overview

Define our own class rather than modify the global class

Constant Summary collapse

YAML_TAG =
'tag:yaml.org,2002:float'
YAML_MAPPING =
{ 'Infinity' => '.Inf', '-Infinity' => '-.Inf', 'NaN' => '.NaN' }
DEFAULT_STRING_FORMAT =
'F'

Instance Method Summary collapse

Instance Method Details

#encode_with(coder) ⇒ Object



30
31
32
33
# File 'lib/sitemap_generator/core_ext/big_decimal.rb', line 30

def encode_with(coder)
  string = to_s
  coder.represent_scalar(nil, YAML_MAPPING[string] || string)
end

#to_dObject



35
36
37
# File 'lib/sitemap_generator/core_ext/big_decimal.rb', line 35

def to_d
  self
end

#to_formatted_s(format = DEFAULT_STRING_FORMAT) ⇒ Object Also known as: to_s



40
41
42
# File 'lib/sitemap_generator/core_ext/big_decimal.rb', line 40

def to_formatted_s(format = DEFAULT_STRING_FORMAT)
  _original_to_s(format)
end

#to_yaml(opts = {}) ⇒ Object

This emits the number without any scientific notation. This is better than self.to_f.to_s since it doesn’t lose precision.

Note that reconstituting YAML floats to native floats may lose precision.



21
22
23
24
25
26
27
28
# File 'lib/sitemap_generator/core_ext/big_decimal.rb', line 21

def to_yaml(opts = {})
  return super if defined?(YAML::ENGINE) && !YAML::ENGINE.syck?

  YAML.quick_emit(nil, opts) do |out|
    string = to_s
    out.scalar(YAML_TAG, YAML_MAPPING[string] || string, :plain)
  end
end