Sha256: ef92385cf944cdc35a4bc2f085a33279a43fd82dbbbe2506014b8e2ebc50661c

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

require 'dm-core'

require 'time'
require 'date'

module DataMapper
  class Property
    module Legacy
      class TimeString < String

        #
        # Parses a time string.
        #
        # @param [String] value
        #   The time string.
        #
        # @return [Time, nil]
        #   The parsed time.
        #
        def load(value)
          ::Time.parse(value) unless (value.nil? || value.empty?)
        end

        #
        # Typecasts a time.
        #
        # @param [Time, Date, String, nil] value
        #   The time to typecast.
        #
        # @return [Time, nil]
        #   The typecasted time.
        #
        def typecast(value)
          if value.kind_of?(::Time)
            value
          elsif value.kind_of?(::Date)
            value.to_time
          elsif value.kind_of?(::String)
            ::Time.parse(value) unless value.empty?
          end
        end

        #
        # Dumps a time to a string.
        #
        # @param [Time, nil] value
        #   The time to dump.
        #
        # @return [String, nil]
        #   The time string.
        #
        def dump(value)
          case value
          when ::Time
            value.to_s
          when ::Date
            value.to_time.to_s
          when nil
            nil
          else
            value.to_s
          end
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dm-types-legacy-0.1.1 lib/dm-core/property/legacy/time_string.rb
dm-types-legacy-0.1.0 lib/dm-core/property/legacy/time_string.rb