Sha256: c4590ac6ab5964fc0d9eb56d1d42c4036ff7074b2168db0f135f1d43de6e56da

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require 'uri'
require 'date'
require 'LIVR/Util'

module Special
  def self.email(args)
    email_re = %r(^([\w\-_+]+(?:\.[\w\-_+]+)*)@((?:[\w\-]+\.)*\w[\w\-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)i

    lambda do |value, unuse, unuse_|
      return if value.nil? or value.eql?('')
      return 'FORMAT_ERROR' unless value.kind_of? String

      return 'WRONG_EMAIL' unless value =~ email_re
      return 'WRONG_EMAIL' if value =~ %r(\@.*\@)
      return 'WRONG_EMAIL' if value =~ %r(\@.*_)
    end
  end

  def self.url(args)
    url_re = %r(\A(http|https):\/\/([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}|(25[0-5]|2[0-4]\d|[0-1]?\d?\d)
      (\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}|localhost)(:[0-9]{1,5})?(\/.*)?\z)ix

    lambda do |value, unuse, unuse_|
      return if value.nil? or value.eql?('')
      return 'FORMAT_ERROR' unless value.kind_of? String
      return 'WRONG_URL' unless value =~ url_re
    end
  end

  def self.iso_date(args)
    date_re = %r(^(\d{4})-(\d{2})-(\d{2})$)

    lambda do |value, unuse, unuse_|
      return if value.nil? or value.eql?('')
      return 'FORMAT_ERROR' unless value.kind_of? String
      return 'WRONG_DATE'   unless value =~ date_re
      begin
        DateTime.strptime(value, '%F')
        return nil
      rescue
        return 'WRONG_DATE'
      end
    end
  end

  def self.equal_to_field(args)
    field = args.shift

    lambda do |value, params, unuse|
      return if value.nil? or value.eql?('')
      return 'FORMAT_ERROR' unless Util.is_string_or_number?(value)
      return 'FIELDS_NOT_EQUAL' unless value == params[field]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
livr-0.0.2 lib/LIVR/Rules/Special.rb