Module: Enotype

Defined in:
lib/enotype/de.rb,
lib/enotype/en.rb,
lib/enotype/es.rb

Class Method Summary collapse

Class Method Details

.boolean(value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/enotype/de.rb', line 14

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) ⇒ Object



25
26
27
28
29
# File 'lib/enotype/de.rb', line 25

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) ⇒ Object



31
32
33
# File 'lib/enotype/de.rb', line 31

def self.comma_separated(value)
  value.split(',', -1).map { |item| item.strip  }
end

.date(value) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/enotype/de.rb', line 35

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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/enotype/de.rb', line 47

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) ⇒ Object



65
66
67
68
69
# File 'lib/enotype/de.rb', line 65

def self.email(value)
  raise 'Eine valide Email Adresse ist erforderlich, zum Beispiel \'[email protected]\'.' unless value.match(EMAIL_REGEXP)

  value
end

.float(value) ⇒ Object



71
72
73
74
75
# File 'lib/enotype/de.rb', line 71

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) ⇒ Object



77
78
79
80
81
# File 'lib/enotype/de.rb', line 77

def self.integer(value)
  raise 'Eine Ganzzahl ist erforderlich, zum Beispiel \'42\' oder \'-21\'.' unless value.match(INTEGER_REGEXP)

  value.to_i
end

.json(value) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/enotype/de.rb', line 83

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) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/enotype/de.rb', line 91

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

.slug(value) ⇒ Object



99
100
101
102
103
# File 'lib/enotype/de.rb', line 99

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) ⇒ Object



105
106
107
108
109
# File 'lib/enotype/de.rb', line 105

def self.url(value)
  raise 'Eine valide URL ist erforderlich, zum Beispiel \'https://eno-lang.org\'.' unless value.match(URL_REGEXP)

  value
end