Sha256: fca15fc39a7efac630e60d0c9ed0743778d5f6ee8a614ec6e9fcae1a407f4b44

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

# encoding: utf-8

module Rubymisc
  module Regex
    autoload :Manual, File.expand_path('../regex/manual', __FILE__)

    class << self
      def email
        email_name_regex  = '[\w\.%\+\-]+'.freeze
        domain_head_regex = '(?:[A-Z0-9\-]+\.)+'.freeze
        domain_tld_regex  = '(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)'.freeze
        /\A#{email_name_regex}@#{domain_head_regex}#{domain_tld_regex}\z/i
      end

      def phone
        /\A
          (?:
            (?<prefix>\d)             # prefix digit
            [ \-\.]?                  # optional separator
          )?
          (?:
            \(?(?<areacode>\d{3})\)?  # area code
            [ \-\.]                   # separator
          )?
          (?<trunk>\d{3})             # trunk
          [ \-\.]                     # separator
          (?<line>\d{4})              # line
          (?:\ ?x?                    # optional space or 'x'
            (?<extension>\d+)         # extension
          )?
        \z/x
      end

      def url
        /\A((https?):(([A-Za-z0-9$_.+!*(),;\/?:@&~=-])|%[A-Za-z0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;\/?:@&~=%-]*))?([A-Za-z0-9$_+!*();\/?:~-]))\z/
      end

      def zip
        /\A\d{5}(?:-\d{4})?\z/
      end

      def ipv4
        ip_octet = '(\d|[01]?\d\d|2[0-4]\d|25[0-5])'.freeze
        /\A#{ip_octet}\.#{ip_octet}\.#{ip_octet}\.#{ip_octet}\z/
      end

      def mac_address
        /\A(\h{2}:){5}\h{2}\z/
      end

      def hexcode
        /\A#(\h{3}){1,2}\z/
      end

      def usd
        /\A\$(\d{1,3}(\,\d{3})*|\d+)(\.\d{2})?\z/
      end
    end

    code = <<-CODE
      def man; Manual.man; end
    CODE

    singleton_class.module_eval code
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubymisc-0.1.0 lib/rubymisc/regex.rb