Class: Taipo::TypeElement::Constraint Private
- Inherits:
-
Object
- Object
- Taipo::TypeElement::Constraint
- 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
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
'#'
Instance Attribute Summary collapse
-
#name ⇒ Object
private
The name of the constraint.
-
#value ⇒ Object
private
The value of the constraint.
Instance Method Summary collapse
-
#constrain?(arg) ⇒ Boolean
private
Check if
arg
is within this constraint. -
#initialize(name: nil, value: nil) ⇒ Constraint
constructor
private
Initialize a new constraint.
-
#parse_value(v) ⇒ Object
private
Parse
v
and convert to the appropriate form if necessary. -
#to_s ⇒ String
private
Return the String representation of this constraint.
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
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
#name ⇒ 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.
The name of the constraint
20 21 22 |
# File 'lib/taipo/type_element/constraint.rb', line 20 def name @name end |
#value ⇒ 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.
The value of the constraint
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
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
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_s ⇒ String
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
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 |