From b3f2bcbeb78df01a6c7a2e357f9a355d2bc96a5c Mon Sep 17 00:00:00 2001 From: Dan Kubb Date: Wed, 20 Jul 2011 10:39:40 -0700 Subject: [PATCH] Added to_string methods to typecasting subclasses * Updated methods to use to_string instead of calling value.to_s inline --- lib/virtus/typecast/big_decimal.rb | 14 ++++++++++++++ lib/virtus/typecast/date.rb | 16 +++++++++++++++- lib/virtus/typecast/date_time.rb | 18 ++++++++++++++++-- lib/virtus/typecast/float.rb | 16 +++++++++++++++- lib/virtus/typecast/time.rb | 18 ++++++++++++++++-- 5 files changed, 76 insertions(+), 6 deletions(-) diff --git a/lib/virtus/typecast/big_decimal.rb b/lib/virtus/typecast/big_decimal.rb index f4c68f84..efbec553 100644 --- a/lib/virtus/typecast/big_decimal.rb +++ b/lib/virtus/typecast/big_decimal.rb @@ -5,6 +5,20 @@ module Typecast # class BigDecimal < Object + # Typecast given value to String + # + # @example + # Virtus::Typecast::BigDecimal.to_string(BigDecimal('1.0')) # => "1.0" + # + # @param [BigDecimal] value + # + # @return [String] + # + # @api public + def self.to_string(value) + value.to_s('F') + end + # Creates a Fixnum instance from a BigDecimal # # @example diff --git a/lib/virtus/typecast/date.rb b/lib/virtus/typecast/date.rb index 9b613d4c..9c8caede 100644 --- a/lib/virtus/typecast/date.rb +++ b/lib/virtus/typecast/date.rb @@ -5,12 +5,26 @@ module Typecast # class Date < Object + # Typecast given value to String + # + # @example + # Virtus::Typecast::Date.to_string(date) # => "2011-07-20" + # + # @param [Date] value + # + # @return [String] + # + # @api public + def self.to_string(value) + value.to_s + end + # @api public def self.to_datetime(value) if value.respond_to?(:to_datetime) value.to_datetime else - String.to_datetime(value.to_s) + String.to_datetime(to_string(value)) end end diff --git a/lib/virtus/typecast/date_time.rb b/lib/virtus/typecast/date_time.rb index bbc139da..cb152469 100644 --- a/lib/virtus/typecast/date_time.rb +++ b/lib/virtus/typecast/date_time.rb @@ -5,12 +5,26 @@ module Typecast # class DateTime < Object + # Typecast given value to String + # + # @example + # Virtus::Typecast::DateTime.to_string(date_time) # => "2011-07-20T10:30:41-07:00" + # + # @param [DateTime] value + # + # @return [String] + # + # @api public + def self.to_string(value) + value.to_s + end + # @api public def self.to_date(value) if value.respond_to?(:to_date) value.to_date else - String.to_date(value.to_s) + String.to_date(to_string(value)) end end @@ -19,7 +33,7 @@ def self.to_time(value) if value.respond_to?(:to_time) value.to_time else - String.to_time(value.to_s) + String.to_time(to_string(value)) end end diff --git a/lib/virtus/typecast/float.rb b/lib/virtus/typecast/float.rb index caef920e..967c72c7 100644 --- a/lib/virtus/typecast/float.rb +++ b/lib/virtus/typecast/float.rb @@ -5,6 +5,20 @@ module Typecast # class Float < Object + # Typecast given value to String + # + # @example + # Virtus::Typecast::Fixnum.to_string(0.10) # => "0.10" + # + # @param [Fixnum] value + # + # @return [String] + # + # @api public + def self.to_string(value) + value.to_s + end + # @api public def self.to_i(value) value.to_i @@ -12,7 +26,7 @@ def self.to_i(value) # @api public def self.to_d(value) - value.to_s.to_d + to_string(value).to_d end end # class Float diff --git a/lib/virtus/typecast/time.rb b/lib/virtus/typecast/time.rb index 8dc300c0..3a588a8e 100644 --- a/lib/virtus/typecast/time.rb +++ b/lib/virtus/typecast/time.rb @@ -5,12 +5,26 @@ module Typecast # class Time < Object + # Typecast given value to String + # + # @example + # Virtus::Typecast::Time.to_string(time) # => "Wed Jul 20 10:30:41 -0700 2011" + # + # @param [Time] value + # + # @return [String] + # + # @api public + def self.to_string(value) + value.to_s + end + # @api public def self.to_datetime(value) if value.respond_to?(:to_datetime) value.to_datetime else - String.to_datetime(value.to_s) + String.to_datetime(to_string(value)) end end @@ -19,7 +33,7 @@ def self.to_date(value) if value.respond_to?(:to_date) value.to_date else - String.to_date(value.to_s) + String.to_date(to_string(value)) end end