enotype
A cross-language type library
require 'enotype'
Enotype::color('#fff') # returns "#fff"
Enotype::color('#xyz') # raises "A color is required, for instance '#B6D918', '#fff' or '#01b'."
require 'enotype/es' # with localized error messages
Enotype::color('#xyz') # raises "Se requiere un color, por ejemplo '#B6D918', '#fff' o '#01b'."
Installation
Either add it to your Gemfile
:
gem 'enotype'
Or install it manually:
gem install enotype
Features
- Validation and conversion of
string
representations into language-native types. - Implemented as a collection of minimalist functions, so called loaders.
- Zero-cost localization (currently
de
,en
,es
) through statically generated code. - Generically usable in a multitude of contexts through a plain and simple design.
- Standard type library for the eno notation language.
Documentation
boolean
require 'enotype'
Enotype::boolean('true') # returns true
'true'
returns true
.
'false'
returns false
.
'yes'
returns true
.
'no'
returns false
.
'nope'
raises an exception.
color
require 'enotype'
Enotype::color('#abcdef') # returns '#abcdef'
'#abcdef'
returns '#abcdef'
.
'#ABCDEF'
returns '#ABCDEF'
.
'#012345'
returns '#012345'
.
'#678'
returns '#678'
.
'#89a'
returns '#89a'
.
'#ab'
raises an exception.
'#abcd'
raises an exception.
'#abcde'
raises an exception.
'#bcdefg'
raises an exception.
'blue'
raises an exception.
comma_separated
require 'enotype'
Enotype::comma_separated('one,two,three') # returns ['one', 'two', 'three']
'one,two,three'
returns ['one', 'two', 'three']
.
' one,two,three '
returns ['one', 'two', 'three']
.
'one , two , three'
returns ['one', 'two', 'three']
.
' one , two , three '
returns ['one', 'two', 'three']
.
',,'
returns ['', '', '']
.
'one two three'
returns ['one two three']
.
'one;two;three'
returns ['one;two;three']
.
' '
returns ['']
.
date
require 'enotype'
Enotype::date('1992-02-02') # returns Time.utc(1992, 2, 2)
'1992-02-02'
returns Time.utc(1992, 2, 2)
.
'1990'
raises an exception.
'1991-01'
raises an exception.
'1993-03-03T1920+01:00'
raises an exception.
'1994-04-04T1920:30+01:00'
raises an exception.
'1995-05-05T1920:30.45+01:00'
raises an exception.
'1996-06-06T0815:30-05:00'
raises an exception.
'1997-07-07T1315:30Z'
raises an exception.
'2002 12 14'
raises an exception.
'2002-12-14 20:15'
raises an exception.
'January'
raises an exception.
'13:00'
raises an exception.
datetime
require 'enotype'
Enotype::datetime('1990') # returns Time.utc(1990, 1, 1)
'1990'
returns Time.utc(1990, 1, 1)
.
'1991-01'
returns Time.utc(1991, 1, 1)
.
'1992-02-02'
returns Time.utc(1992, 2, 2)
.
'1993-03-03T19:20+01:00'
returns Time.utc(1993, 3, 3, 18, 20)
.
'1994-04-04T19:20:30+01:00'
returns Time.new(1994, 4, 4, 19, 20, 30, '+01:00')
.
'1995-05-05T19:20:30.450+01:00'
returns Time.new(1995, 5, 5, 19, 20, 30.450, '+01:00')
.
'1996-06-06T08:15:30-05:00'
returns Time.new(1996, 6, 6, 8, 15, 30, '-05:00')
.
'1997-07-07T13:15:30Z'
returns Time.utc(1997, 7, 7, 13, 15, 30)
.
'2002 12 14'
raises an exception.
'2002-12-14 20:15'
raises an exception.
'January'
raises an exception.
'13:00'
raises an exception.
require 'enotype'
Enotype::email('[email protected]') # returns '[email protected]'
'[email protected]'
returns '[email protected]'
.
'john.doe@eno-lang'
raises an exception.
'@eno-lang.org'
raises an exception.
'[email protected]'
raises an exception.
float
require 'enotype'
Enotype::float('42') # returns 42.0
'42'
returns 42.0
.
'-42'
returns -42.0
.
'42.0'
returns 42.0
.
'42,0'
raises an exception.
'4 2.0'
raises an exception.
'fortytwo'
raises an exception.
integer
require 'enotype'
Enotype::integer('42') # returns 42
'42'
returns 42
.
'-42'
returns -42
.
'42.0'
raises an exception.
'42,0'
raises an exception.
'4 2'
raises an exception.
'fortytwo'
raises an exception.
json
require 'enotype'
Enotype::json('{ "valid": true }') # returns { 'valid' => true }
'{ "valid": true }'
returns { 'valid' => true }
.
'42'
returns 42
.
'["valid", true]'
returns ['valid', true]
.
'invalid'
raises an exception.
'{ invalid: true }'
raises an exception.
'{ "invalid": true, }'
raises an exception.
lat_lng
require 'enotype'
Enotype::lat_lng('48.205870, 16.413690') # returns { lat: 48.205870, lng: 16.413690 }
'48.205870, 16.413690'
returns { lat: 48.205870, lng: 16.413690 }
.
'41.25, -120.9762'
returns { lat: 41.25, lng: -120.9762 }
.
'-31.96, 115.84'
returns { lat: -31.96, lng: 115.84 }
.
'90, 0'
returns { lat: 90, lng: 0 }
.
' 0 , 0 '
returns { lat: 0, lng: 0 }
.
'-0,-0'
returns { lat: -0, lng: -0 }
.
'1000,10'
raises an exception.
'10,1000'
raises an exception.
'48.205870,'
raises an exception.
', 16.413690'
raises an exception.
'48,205870, 16,413690'
raises an exception.
slug
require 'enotype'
Enotype::slug('eno-lang-article') # returns 'eno-lang-article'
'eno-lang-article'
returns 'eno-lang-article'
.
'eno_lang_article'
returns 'eno_lang_article'
.
'eno-lang-article!'
raises an exception.
'%eno-lang-article'
raises an exception.
'eno lang article'
raises an exception.
'enö-läng-ärticle'
raises an exception.
'énó-láng-ártíclé'
raises an exception.
url
require 'enotype'
Enotype::url('http://www.valid.com') # returns 'http://www.valid.com'
'http://www.valid.com'
returns 'http://www.valid.com'
.
'https://valid.com'
returns 'https://valid.com'
.
'https://www.valid.com'
returns 'https://www.valid.com'
.
'invalid'
raises an exception.
'www.invalid'
raises an exception.
'www.invalid.com'
raises an exception.
'htp://www.invalid.com'
raises an exception.
'http:/invalid.com'
raises an exception.
'https//invalid.com'
raises an exception.
'https://invalid'
raises an exception.