Froze rails gems
[depot.git] / vendor / rails / activesupport / lib / active_support / core_ext / bigdecimal / conversions.rb
1 require 'yaml'
2
3 module ActiveSupport #:nodoc:
4 module CoreExtensions #:nodoc:
5 module BigDecimal #:nodoc:
6 module Conversions
7 DEFAULT_STRING_FORMAT = 'F'.freeze
8 YAML_TAG = 'tag:yaml.org,2002:float'.freeze
9 YAML_MAPPING = { 'Infinity' => '.Inf', '-Infinity' => '-.Inf', 'NaN' => '.NaN' }
10
11 def self.included(base) #:nodoc:
12 base.class_eval do
13 alias_method :_original_to_s, :to_s
14 alias_method :to_s, :to_formatted_s
15
16 yaml_as YAML_TAG
17 end
18 end
19
20 def to_formatted_s(format = DEFAULT_STRING_FORMAT)
21 _original_to_s(format)
22 end
23
24 # This emits the number without any scientific notation.
25 # This is better than self.to_f.to_s since it doesn't lose precision.
26 #
27 # Note that reconstituting YAML floats to native floats may lose precision.
28 def to_yaml(opts = {})
29 YAML.quick_emit(nil, opts) do |out|
30 string = to_s
31 out.scalar(YAML_TAG, YAML_MAPPING[string] || string, :plain)
32 end
33 end
34 end
35 end
36 end
37 end