Method: Taipo::TypeElement::Constraint#parse_value

Defined in:
lib/taipo/type_element/constraint.rb

#parse_value(v) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse v and convert to the appropriate form if necessary

Parameters:

  • v (Object)

    the value

Returns:

  • (Object)

    the parsed value

Raises:

  • (::TypeError)

    if the value is not appropriate for this type of constraint

Since:

  • 1.0.0



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/taipo/type_element/constraint.rb', line 101

def parse_value(v)
  return nil if v == nil

  case @name
  when Constraint::METHOD
    v
  when 'format'
    return v if v.is_a? Regexp
    msg = 'The value cannot be cast to a regular expression.'
    raise ::TypeError, msg unless v[0] == '/' && v[-1] == '/'
    Regexp.new v[1, v.length-2]
  when 'len', 'max', 'min'
    return v if v.is_a? Integer
    msg = 'The value cannot be cast to an Integer.'
    raise ::TypeError, msg unless v == v.to_i.to_s
    v.to_i
  when 'val'
    v
  end
end