Sha256: 3de06e7f961c03c68805aa4687d3e268d74819e9d57338594ee8423053214069

Contents?: true

Size: 610 Bytes

Versions: 3

Compression:

Stored size: 610 Bytes

Contents

module DeadSimpleDb

  class SdbTime

    DEFAULT_OPTS = {:format => '%Y-%m-%d %H:%M:%S'}

    def initialize(value, opts={})
      @opts = DEFAULT_OPTS.merge(opts)
      @value_before_cast = value
    end

    def casted
      @casted = @value_before_cast if @value_before_cast.is_a?(Time)
      @casted ||= begin
        if @value_before_cast.respond_to?(:to_time)
          @value_before_cast.to_time
        else
          Time.local(*(@value_before_cast.split(/-|\s|:/).map { |n| n.to_i }))
        end
      end
    end

    def to_s
      @string ||= casted.strftime(@opts[:format])
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hungryblank-dead_simple_db-0.0.1 lib/dead_simple_db/datatypes/sdb_time.rb
hungryblank-dead_simple_db-0.0.2 lib/dead_simple_db/datatypes/sdb_time.rb
hungryblank-dead_simple_db-0.0.3 lib/dead_simple_db/datatypes/sdb_time.rb