Sha256: 2d334a17b25609a7dbe47f5acfadcc0dcc6b794ddff3a42fde7a364b7f8b58bd

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

#encoding: UTF-8
module Str2Duck
  module Regexp

    @@year  = '\d+'
    class << self

      def datetime? obj

        answer_value= nil
        [
            /^\w+, \d+ \w+ #{@@year} \d\d:\d\d:\d\d \+\d+$/,
            /^#{@@year}-\d\d-\d\d\w\d\d:\d\d:\d\d\+\d\d:\d\d$/,
            /\w+ \w+ \d+ #{@@year} \d+:\d+:\d+ \w+\+\d+ \(\w+\)/
        ].each do |regexp|
          answer_value ||= obj =~ regexp
        end

        return !!answer_value

      end

      def date? obj

        answer_value= nil
        [
            /^#{@@year}-\d\d-\d\d$/,
            /^\w+, \d+ \w+ #{@@year}$/
        ].each do |regexp|
          answer_value ||= obj =~ regexp
        end

        return !!answer_value

      end

      def time? obj

        answer_value= nil
        [
            /^#{@@year}-\d\d-\d\d \d\d:\d\d:\d\d \+\d+$/
        ].each do |regexp|
          answer_value ||= obj =~ regexp
        end

        return !!answer_value

      end

      def true? obj
        return !!obj =~ /^true$/
      end

      def false? obj
        return !!obj =~ /^false$/
      end

      def float? obj
        case obj.to_s
          when /^\d+\.\d+$/,/^\d+,\d+$/
            return true
          else
            return false
        end
      end

      def integer? obj
        return !!obj =~ /^\d+$/
      end

      def json? obj
        begin
          JSON.parse(obj)
          return true
        rescue ::JSON::ParserError
          return false
        end
      end

      def yaml? obj
        YAML.safe_load(obj)
        return true
      rescue ::Exception
        return false
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
str2duck-1.7.0 lib/str2duck/regexp.rb