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


15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Object


26
27
28
29
30
# 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) ⇒ Object


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

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

.date(value) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object


66
67
68
69
70
# 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) ⇒ Object


72
73
74
75
76
# 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) ⇒ Object


78
79
80
81
82
# 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) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object


96
97
98
99
100
101
102
# 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) ⇒ Object


104
105
106
107
108
109
110
# 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) ⇒ Object


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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) ⇒ Object


112
113
114
115
116
# 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) ⇒ Object


118
119
120
121
122
# 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