Skip to content

Commit

Permalink
Added to_string methods to typecasting subclasses
Browse files Browse the repository at this point in the history
* Updated methods to use to_string instead of calling value.to_s inline
  • Loading branch information
dkubb committed Jul 20, 2011
1 parent 8610f8d commit b3f2bcb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
14 changes: 14 additions & 0 deletions lib/virtus/typecast/big_decimal.rb
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion lib/virtus/typecast/date.rb
Expand Up @@ -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

Expand Down
18 changes: 16 additions & 2 deletions lib/virtus/typecast/date_time.rb
Expand Up @@ -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

Expand All @@ -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

Expand Down
16 changes: 15 additions & 1 deletion lib/virtus/typecast/float.rb
Expand Up @@ -5,14 +5,28 @@ 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
end

# @api public
def self.to_d(value)
value.to_s.to_d
to_string(value).to_d
end

end # class Float
Expand Down
18 changes: 16 additions & 2 deletions lib/virtus/typecast/time.rb
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit b3f2bcb

Please sign in to comment.