module Enotype

Public Class Methods

boolean(value) click to toggle source
# File lib/enotype/de.rb, line 15
def self.boolean(value)
  lower = value.strip.downcase

  return true if lower == 'true'
  return false if lower == 'false'
  return true if lower == 'yes'
  return false if lower == 'no'

  raise 'Ein boolescher Wert ist erforderlich - erlaubte Werte sind \'true\', \'false\', \'yes\' und \'no\'.'
end
color(value) click to toggle source
# File lib/enotype/de.rb, line 26
def self.color(value)
  raise 'Eine Farbe ist erforderlich, zum Beispiel \'#B6D918\', \'#fff\' oder \'#01b\'.' unless value.match(COLOR_REGEXP)

  value
end
comma_separated(value) click to toggle source
# File lib/enotype/de.rb, line 32
def self.comma_separated(value)
  value.split(',', -1).map { |item| item.strip  }
end
date(value) click to toggle source
# File lib/enotype/de.rb, line 36
def self.date(value)
  match = value.match(DATE_REGEXP)

  raise 'Ein valides Datum ist erforderlich, zum Beispiel \'1993-11-18\'.' unless match

  year = match[1].to_i
  month = match[2].to_i
  day = match[3].to_i

  Time.utc(year, month, day)
end
datetime(value) click to toggle source
# File lib/enotype/de.rb, line 48
def self.datetime(value)
  match = value.match(DATETIME_REGEXP)

  raise 'Ein valides Datum oder Datum und Zeit sind erforderlich, zum Beispiel \'1961-01-22\' oder \'1989-11-09T19:17Z\' (siehe https://www.w3.org/TR/NOTE-datetime).' unless match

  year = match[1].to_i
  month = match[2] ? match[2].to_i : 1
  day = match[3] ? match[3].to_i : 1
  hour = match[4] ? match[4].to_i : 0
  minute = match[5] ? match[5].to_i : 0
  second = match[6] ? match[6].to_i : 0
  fraction = match[7] ? "0.#{match[7]}".to_f : 0
  zulu = match[8]
  offset = zulu ? '+00:00' : "#{match[9] || '+'}#{match[10] || '00'}:#{match[11] || '00'}"

  Time.new(year, month, day, hour, minute, second + fraction, offset)
end
email(value) click to toggle source
# File lib/enotype/de.rb, line 66
def self.email(value)
  raise 'Eine valide Email Adresse ist erforderlich, zum Beispiel \'jane.doe@eno-lang.org\'.' unless value.match(EMAIL_REGEXP)

  value
end
float(value) click to toggle source
# File lib/enotype/de.rb, line 72
def self.float(value)
  raise 'Eine Dezimalzahl ist erforderlich, zum Beispiel \'13.0\', \'-9.159\' oder \'42\'.' unless value.match(FLOAT_REGEXP)
  
  value.to_f
end
integer(value) click to toggle source
# File lib/enotype/de.rb, line 78
def self.integer(value)
  raise 'Eine Ganzzahl ist erforderlich, zum Beispiel \'42\' oder \'-21\'.' unless value.match(INTEGER_REGEXP)

  value.to_i
end
ipv4(value) click to toggle source
# File lib/enotype/de.rb, line 84
def self.ipv4(value)
  match = value.match(IPV4_REGEXP)

  return match[1] if match &&
                     match[2].to_i.between?(0, 255) &&
                     match[3].to_i.between?(0, 255) &&
                     match[4].to_i.between?(0, 255) &&
                     match[5].to_i.between?(0, 255)

  raise 'Eine valide IPv4 Adresse ist erforderlich, zum Beispiel \'192.168.0.1\'.'
end
json(value) click to toggle source
# File lib/enotype/de.rb, line 96
def self.json(value)
  begin
    JSON.parse(value)
  rescue => err
    raise "Valides JSON ist erforderlich - die Meldung des Parsers war:\n#{err.message}"
  end
end
lat_lng(value) click to toggle source
# File lib/enotype/de.rb, line 104
def self.lat_lng(value)
  match = LAT_LNG_REGEXP.match(value)

  raise 'Ein valides Breiten-/Längengrad Koordinatenpaar ist erforderlich, zum Beispiel \'48.2093723, 16.356099\'.' unless match

  { lat: match[1].to_f, lng: match[2].to_f }
end
procs(*explicitly_requested) click to toggle source
# File lib/enotype/de.rb, line 124
def self.procs(*explicitly_requested)
  available = self.singleton_methods.reject { |name| name == :procs }

  if explicitly_requested.empty?
    available.to_h do |name|
      [name, Proc.new { |value| self.send(name, value) }]
    end
  else
    explicitly_requested.to_h do |name|
      unless available.include?(name)
        list = available.map { |name| ":#{name}" }.join(', ')
        raise "Enotype does not provide :#{name}, available are: #{list}"
      end

      [name, Proc.new { |value| self.send(name, value) }]
    end
  end
end
slug(value) click to toggle source
# File lib/enotype/de.rb, line 112
def self.slug(value)
  raise 'Ein Slug wie zum Beispiel \'blog_post\', \'eno-notation\' oder \'62-dinge\' ist erforderlich - nur die Zeichen a-z, 0-9, \'-\' und \'_\' sind erlaubt.' unless value.match(SLUG_REGEXP)

  value
end
url(value) click to toggle source
# File lib/enotype/de.rb, line 118
def self.url(value)
  raise 'Eine valide URL ist erforderlich, zum Beispiel \'https://eno-lang.org\'.' unless value.match(URL_REGEXP)

  value
end