Class: TableSchema::Types::String

Inherits:
Base
  • Object
show all
Defined in:
lib/tableschema/types/string.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#cast, #initialize, #test

Methods included from Helpers

#deep_symbolize_keys, #get_class_for_type, #type_class_lookup

Constructor Details

This class inherits a constructor from TableSchema::Types::Base

Class Method Details

.supported_constraintsObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/tableschema/types/string.rb', line 9

def self.supported_constraints
  [
    'required',
    'unique',
    'pattern',
    'enum',
    'minLength',
    'maxLength',
  ]
end

Instance Method Details

#cast_binary(value) ⇒ Object



63
64
65
66
67
68
# File 'lib/tableschema/types/string.rb', line 63

def cast_binary(value)
  value = cast_default(value)
  Base64.strict_decode64(value)
rescue ArgumentError
  raise TableSchema::InvalidBinary.new("#{value} is not a valid binary string")
end

#cast_default(value) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/tableschema/types/string.rb', line 28

def cast_default(value)
  if value.is_a?(type)
    return value
  else
    raise TableSchema::InvalidCast.new("#{value} is not a #{name}")
  end
end

#cast_email(value) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/tableschema/types/string.rb', line 36

def cast_email(value)
  value = cast_default(value)
  if (value =~ email_pattern) != nil
    value
  else
    raise TableSchema::InvalidEmail.new("#{value} is not a valid email")
  end
end

#cast_uri(value) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/tableschema/types/string.rb', line 45

def cast_uri(value)
  value = cast_default(value)
  if (value =~ URI::regexp) != nil
    value
  else
    raise TableSchema::InvalidURI.new("#{value} is not a valid uri")
  end
end

#cast_uuid(value) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/tableschema/types/string.rb', line 54

def cast_uuid(value)
  value = cast_default(value)
  if UUID.validate(value)
    value
  else
    raise TableSchema::InvalidUUID.new("#{value} is not a valid UUID")
  end
end

#email_patternObject



24
25
26
# File 'lib/tableschema/types/string.rb', line 24

def email_pattern
  /[^@]+@[^@]+\.[^@]+/
end

#nameObject



5
6
7
# File 'lib/tableschema/types/string.rb', line 5

def name
  'string'
end

#typeObject



20
21
22
# File 'lib/tableschema/types/string.rb', line 20

def type
  ::String
end