Module: Enotype
- Defined in:
- lib/enotype/de.rb,
lib/enotype/en.rb,
lib/enotype/es.rb
Class Method Summary collapse
- .boolean(value) ⇒ Object
- .color(value) ⇒ Object
- .comma_separated(value) ⇒ Object
- .date(value) ⇒ Object
- .datetime(value) ⇒ Object
- .email(value) ⇒ Object
- .float(value) ⇒ Object
- .integer(value) ⇒ Object
- .json(value) ⇒ Object
- .lat_lng(value) ⇒ Object
- .url(value) ⇒ Object
Class Method Details
.boolean(value) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/enotype/de.rb', line 13 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
24 25 26 27 28 |
# File 'lib/enotype/de.rb', line 24 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
30 31 32 |
# File 'lib/enotype/de.rb', line 30 def self.comma_separated(value) value.split(',', -1).map { |item| item.strip } end |
.date(value) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/enotype/de.rb', line 34 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/enotype/de.rb', line 46 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
64 65 66 67 68 |
# File 'lib/enotype/de.rb', line 64 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
70 71 72 73 74 |
# File 'lib/enotype/de.rb', line 70 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
76 77 78 79 80 |
# File 'lib/enotype/de.rb', line 76 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
82 83 84 85 86 87 88 |
# File 'lib/enotype/de.rb', line 82 def self.json(value) begin JSON.parse(value) rescue => err raise "Valides JSON ist erforderlich - die Meldung des Parsers war:\n#{err.}" end end |
.lat_lng(value) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/enotype/de.rb', line 90 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 |
.url(value) ⇒ Object
98 99 100 101 102 |
# File 'lib/enotype/de.rb', line 98 def self.url(value) raise 'Eine valide URL ist erforderlich, zum Beispiel \'https://eno-lang.org\'.' unless value.match(URL_REGEXP) value end |