Sha256: 6509cd4fe419565913a27e7b4075ec539c22d70cf746dc27c1f2b1b3d3e305d8

Contents?: true

Size: 696 Bytes

Versions: 3

Compression:

Stored size: 696 Bytes

Contents

module DeadSimpleDb

  class SdbDate

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

    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?(Date)
      @casted ||= begin
        if @value_before_cast.respond_to?(:to_date)
          @value_before_cast.to_date
        elsif Date.respond_to?(:strptime)
          Date.strptime(@value_before_cast, @opts[:format])
        else
          Date.new(*(@value_before_cast.split('-').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_date.rb
hungryblank-dead_simple_db-0.0.2 lib/dead_simple_db/datatypes/sdb_date.rb
hungryblank-dead_simple_db-0.0.3 lib/dead_simple_db/datatypes/sdb_date.rb