Class: Taipo::TypeElement::Constraint Private

Inherits:
Object
  • Object
show all
Defined in:
lib/taipo/type_element/constraint.rb

Overview

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

A constraint on a type

Since:

  • 1.0.0

Constant Summary collapse

METHOD =

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

The identifier for an instance method

Since:

  • 1.0.0

'#'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, value: nil) ⇒ Constraint

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.

Initialize a new constraint

Parameters:

  • name (String|NilClass) (defaults to: nil)

    the name of the constraint (if nil, this is an instance method)

  • value (String|NilClass) (defaults to: nil)

    the value of the constraint (sometimes a Constraint is initialized before the value is known)

Raises:

  • (::TypeError)

    if name was the wrong type, or value was not of the correct type for the type of constraint

  • (::ArgumentError)

    if name was blank

Since:

  • 1.0.0



41
42
43
44
45
46
47
48
49
# File 'lib/taipo/type_element/constraint.rb', line 41

def initialize(name: nil, value: nil)
  msg = 'Argument name was not nil or a String.'
  raise ::TypeError, msg unless name.nil? || name.is_a?(String)
  msg = 'Argument name was an empty string.'
  raise ::ArgumentError, msg if !name.nil? && name.empty?

  @name = name
  @value = self.parse_value value
end

Instance Attribute Details

#nameObject

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.

The name of the constraint

Since:

  • 1.0.0



20
21
22
# File 'lib/taipo/type_element/constraint.rb', line 20

def name
  @name
end

#valueObject

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.

The value of the constraint

Since:

  • 1.0.0



26
27
28
# File 'lib/taipo/type_element/constraint.rb', line 26

def value
  @value
end

Instance Method Details

#constrain?(arg) ⇒ Boolean

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.

Check if arg is within this constraint

Parameters:

  • arg (Object)

    the object to check

Returns:

  • (Boolean)

    the result

Since:

  • 1.0.0



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/taipo/type_element/constraint.rb', line 59

def constrain?(arg)
  case @name
  when Constraint::METHOD
    arg.respond_to? @value
  when 'format'
    arg.is_a?(String) && arg =~ @value
  when 'len'
    arg.respond_to?('size') && arg.size == @value
  when 'max'
    if arg.is_a? Numeric
      arg <= @value
    else
      arg.respond_to?('size') && arg.size <= @value
    end
  when 'min'
    if arg.is_a? Numeric
      arg >= @value
    else
      arg.respond_to?('size') && arg.size >= @value
    end
  when 'val'
    if @value[0] == '"' && @value[-1] == '"'
      arg.to_s == @value.slice(1..-2)
    elsif arg.is_a? Symbol
      ":" + arg.to_s == @value
    else
      arg.to_s == @value
    end
  end
end

#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

#to_sString

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.

Return the String representation of this constraint

Returns:

  • (String)

    the representation as a String

Since:

  • 1.0.0



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/taipo/type_element/constraint.rb', line 128

def to_s
  name_string = (@name == Constraint::METHOD) ? '#' : @name + ':'
  value_string = case @name
                 when Constraint::METHOD
                   @value
                 when 'format'
                   @value.inspect
                 when 'len', 'max', 'min', 'val'
                   @value.to_s
                 end
  name_string + value_string
end