Sha256: fb3c2c0507bdd133eeeb0e7cd2268611b49c794a9214c1565a4c6cdcc8364c9e

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

#encoding: UTF-8
module Str2Duck
  module Format
    class << self

      def datetime obj
        if Str2Duck::Regexp.datetime?(obj)
          if defined? DateTime
            return DateTime.parse obj
          else
            time_parts= obj.scan(/\d+/).map(&:to_i)
            if time_parts.count == 8
              2.times{time_parts.pop}
            elsif time_parts.count == 6
              return nil
            end
            return Time.new(*time_parts)
          end
        end
        nil
      end

      def date obj
        if Str2Duck::Regexp.date?(obj)
          begin
            return Date.parse obj
          rescue NoMethodError
            time_parts= obj.scan(/\d+/).map(&:to_i)
            return Time.new(*time_parts)
          end
        end
        nil
      end

      def time obj
        if Str2Duck::Regexp.time?(obj)
          begin
            return Time.parse obj
          rescue NoMethodError
            time_parts= obj.scan(/\d+/).map(&:to_i)
            1.times{time_parts.pop}
            return Time.new(*time_parts)
          end
        end
        nil
      end

      def true obj
        if Str2Duck::Regexp.true?(obj)
          return true
        end
        nil
      end

      def false obj
        if Str2Duck::Regexp.false?(obj)
          return false
        end
        nil
      end

      def float obj
        if Str2Duck::Regexp.float?(obj)
          return obj.to_f
        end
        nil
      end

      def integer obj
        if Str2Duck::Regexp.integer?(obj)
          return obj.to_i
        end
        nil
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
str2duck-1.0.1 lib/str2duck/format.rb
str2duck-1.0.0 lib/str2duck/format.rb