Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9091

Show
Ignore:
Timestamp:
03/26/08 02:43:38 (5 months ago)
Author:
nzkoz
Message:

Standardise the implementation of the extensions to BigDecimal. Closes #10608 [mikong]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/core_ext/bigdecimal.rb

    r7719 r9091  
    11require 'bigdecimal' 
    22require 'active_support/core_ext/bigdecimal/conversions' 
     3 
     4class BigDecimal#:nodoc: 
     5  include ActiveSupport::CoreExtensions::BigDecimal::Conversions 
     6end 
  • trunk/activesupport/lib/active_support/core_ext/bigdecimal/conversions.rb

    r8877 r9091  
    11require 'yaml' 
    22 
    3 class BigDecimal #:nodoc: 
    4   alias :_original_to_s :to_s 
    5   def to_s(format="F") 
    6     _original_to_s(format) 
    7   end 
    8  
    9   yaml_as "tag:yaml.org,2002:float" 
    10   def to_yaml( opts = {} ) 
    11     YAML::quick_emit( nil, opts ) do |out| 
    12       # This emits the number without any scientific notation. 
    13       # I prefer it to using self.to_f.to_s, which would lose precision. 
    14       # 
    15       # Note that YAML allows that when reconsituting floats 
    16       # to native types, some precision may get lost. 
    17       # There is no full precision real YAML tag that I am aware of. 
    18       str = self.to_s 
    19       if str == "Infinity" 
    20         str = ".Inf" 
    21       elsif str == "-Infinity" 
    22         str = "-.Inf" 
    23       elsif str == "NaN" 
    24         str = ".NaN" 
     3module ActiveSupport #:nodoc: 
     4  module CoreExtensions #:nodoc: 
     5    module BigDecimal #:nodoc: 
     6      module Conversions 
     7        def self.included(base) #:nodoc: 
     8          base.instance_eval do 
     9            alias_method :_original_to_s, :to_s 
     10            alias_method :to_s, :to_formatted_s 
     11          end 
     12        end 
     13         
     14        def to_formatted_s(format="F") 
     15          _original_to_s(format) 
     16        end 
     17         
     18        yaml_as "tag:yaml.org,2002:float" 
     19        def to_yaml( opts = {} ) 
     20          YAML::quick_emit( nil, opts ) do |out| 
     21            # This emits the number without any scientific notation. 
     22            # I prefer it to using self.to_f.to_s, which would lose precision. 
     23            # 
     24            # Note that YAML allows that when reconsituting floats 
     25            # to native types, some precision may get lost. 
     26            # There is no full precision real YAML tag that I am aware of. 
     27            str = self.to_s 
     28            if str == "Infinity" 
     29              str = ".Inf" 
     30            elsif str == "-Infinity" 
     31              str = "-.Inf" 
     32            elsif str == "NaN" 
     33              str = ".NaN" 
     34            end 
     35            out.scalar( "tag:yaml.org,2002:float", str, :plain ) 
     36          end 
     37        end 
    2538      end 
    26       out.scalar( "tag:yaml.org,2002:float", str, :plain ) 
    2739    end 
    2840  end