Class: Itsi::Server::Config::TypedStruct::Validation
- Inherits:
-
Object
- Object
- Itsi::Server::Config::TypedStruct::Validation
- Defined in:
- lib/itsi/server/config/typed_struct.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#next ⇒ Object
Returns the value of attribute next.
-
#validations ⇒ Object
readonly
Returns the value of attribute validations.
Instance Method Summary collapse
- #&(other) ⇒ Object
- #default(value) ⇒ Object
-
#initialize(name, validations) ⇒ Validation
constructor
A new instance of Validation.
- #inspect ⇒ Object
- #validate!(value) ⇒ Object
Constructor Details
#initialize(name, validations) ⇒ Validation
Returns a new instance of Validation.
93 94 95 96 97 |
# File 'lib/itsi/server/config/typed_struct.rb', line 93 def initialize(name, validations) @name = name.to_s @validations = Array(validations) @next = nil end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
90 91 92 |
# File 'lib/itsi/server/config/typed_struct.rb', line 90 def name @name end |
#next ⇒ Object
Returns the value of attribute next.
91 92 93 |
# File 'lib/itsi/server/config/typed_struct.rb', line 91 def next @next end |
#validations ⇒ Object (readonly)
Returns the value of attribute validations.
90 91 92 |
# File 'lib/itsi/server/config/typed_struct.rb', line 90 def validations @validations end |
Instance Method Details
#&(other) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/itsi/server/config/typed_struct.rb', line 103 def &(other) tail = self tail = tail.next while tail.next tail.next = other self end |
#default(value) ⇒ Object
110 111 112 |
# File 'lib/itsi/server/config/typed_struct.rb', line 110 def default(value) [value, self] end |
#inspect ⇒ Object
99 100 101 |
# File 'lib/itsi/server/config/typed_struct.rb', line 99 def inspect "#{@name}" end |
#validate!(value) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/itsi/server/config/typed_struct.rb', line 114 def validate!(value) value = value.to_s if value.is_a?(Symbol) @validations.each do |validation| case validation when Proc validation.call(value) when Array unless !value || validation.include?(value) raise ArgumentError, "─ `#{@name}` validation failed. Invalid #{validation} value: #{value.inspect}" end when Range unless !value || validation.include?(value) raise ArgumentError, "─ `#{@name}` validation failed. Invalid #{validation} value: #{value.inspect}" end when Regexp unless !value || validation.match?(value) raise ArgumentError, "─ `#{@name}` validation failed. Invalid #{validation} value: #{value.inspect}" end when Validation validation.validate!(value) when Class if value && !value.is_a?(validation) begin value = \ if validation.eql?(Time) then Time.parse(value.to_s) elsif validation.eql?(::Date) then Date.parse(value.to_s) elsif validation.eql?(Float) then Float(value) elsif validation.eql?(Integer) then Integer(value) elsif validation.eql?(Proc) raise ArgumentError, "Invalid #{validation} value: #{value.inspect}" unless value.is_a?(Proc) elsif validation.eql?(String) || validation.eql?(Symbol) unless value.is_a?(String) || value.is_a?(Symbol) raise ArgumentError, "Invalid #{validation} value: #{value.inspect}" end if validation.eql?(String) value.to_s elsif validation.eql?(Symbol) value.to_s.to_sym end else validation.new(value) end rescue StandardError => e raise ArgumentError, "─ `#{@name}` Validation Failed. Invalid #{validation.to_s.split("::").last} value: #{value.inspect}. Failure reason: \n └─ #{e.}" end end end end if self.next self.next.validate!(value) else value end end |