Class: StringValidator
- Inherits:
-
Object
- Object
- StringValidator
- Defined in:
- lib/stringvalidator.rb
Overview
文字列の正当性チェック
Defined Under Namespace
Classes: Error
Class Method Summary collapse
-
.valid?(rule, str) ⇒ Boolean
- rule
- ルールオブジェクト str
-
対象文字列.
-
.validate(rule, str) ⇒ Object
- rule
- ルールオブジェクト str
-
対象文字列.
Instance Method Summary collapse
-
#initialize(rule) ⇒ StringValidator
constructor
- rule
-
Hash オブジェクト。=> rule_object, ….
-
#valid?(rule, str) ⇒ Boolean
- rule
- ルールキー。initialize に与えた Hash のキー str
-
対象文字列.
-
#validate(rule, str) ⇒ Object
- rule
- ルールキー。initialize に与えた Hash のキー str
-
対象文字列.
-
#validated_rule(str) ⇒ Object
- str
-
対象文字列.
Constructor Details
#initialize(rule) ⇒ StringValidator
- rule
-
Hash オブジェクト。=> rule_object, …
137 138 139 |
# File 'lib/stringvalidator.rb', line 137 def initialize(rule) @rule = rule end |
Class Method Details
.valid?(rule, str) ⇒ Boolean
- rule
-
ルールオブジェクト
- str
-
対象文字列
validate(rule, str) が成功すれば true, そうでなければ false を返す。
127 128 129 130 131 132 133 134 |
# File 'lib/stringvalidator.rb', line 127 def self.valid?(rule, str) begin self.validate(rule, str) rescue Error return false end return true end |
.validate(rule, str) ⇒ Object
- rule
-
ルールオブジェクト
- str
-
対象文字列
str が rule に適合するか検査する。 適合しない場合、StringValidator::Error 例外が発生する。
StringValidator#validate(rule, str) と同じ。
118 119 120 |
# File 'lib/stringvalidator.rb', line 118 def self.validate(rule, str) self.new(nil).validate(rule, str) end |
Instance Method Details
#valid?(rule, str) ⇒ Boolean
- rule
-
ルールキー。initialize に与えた Hash のキー
- str
-
対象文字列
StringValidator#validate(rule, _str) が成功すれば true, そうでなければ false を返す。
300 301 302 303 304 305 306 307 |
# File 'lib/stringvalidator.rb', line 300 def valid?(rule, str) begin validate(rule, str) rescue Error return false end return true end |
#validate(rule, str) ⇒ Object
- rule
-
ルールキー。initialize に与えた Hash のキー
- str
-
対象文字列
str が rule に適合するか検査する。 適合しない場合、StringValidator::Error 例外が発生する。
rule の形式
Integer (Integer そのもの。Integer オブジェクトではない)
Integer(str) が成功した場合に str を正当とみなす。 Integer オブジェクトを返す。
Float (Float そのもの。Float オブジェクトではない)
Float(str) が成功した場合に str を正当とみなす。 Float オブジェクトを返す。
Range オブジェクト
rule が str を含んでいれば str を正当とみなす。 rule に応じて、Integer, Float, String オブジェクトを返す。
Regexp オブジェクト
str が rule に適合すれば str を正当とみなす。 str を返す。
Proc オブジェクト
rule.call(str) が例外を発生しなければ str を正当とみなす。 rule.call(str) の結果を返す。
Array オブジェクト
rule の要素をルールとして評価し、正当な要素が一つでもあれば正当とみなす。 最初に正当になったルールの結果を返す。
Hash オブジェクト
複数のルールが指定された場合は、すべてのルールを満たせば正当とみなす。 結果は、:rule(なければ :any, :all)の評価結果。:rule, :any, :all のいずれもなければ str が評価結果となる。
:any => array
-
Array と同じ。
:all => array
-
array の要素をルールとして評価し、すべての要素が正当であれば正当とみなす。 評価結果は、最初の要素のルールの評価結果。
:rule => obj
-
obj をルールとして評価する。
:length => integer or range
-
str の長さ(バイト数)が integer に一致する場合、または range 内であれば正当とみなす。
:maxlength => integer
-
str の長さ(バイト数)が integer 以下であれば正当とみなす。
:minlength => integer
-
str の長さ(バイト数)が integer 以上であれば正当とみなす。
:charlength => integer or range
-
str の長さ(文字数)が integer に一致する場合、または range 内であれば正当とみなす。文字数は $KCODE に依存する。
:maxcharlength => integer
-
str の長さ(文字数)が integer 以下であれば正当とみなす。文字数は $KCODE に依存する。
:mincharlength => integer
-
str の長さ(文字数)が integer 以上であれば正当とみなす。文字数は $KCODE に依存する。
Symbol オブジェクト
new の引数として渡したハッシュの、キー rule に対応する値をルールとして評価する。
Class オブジェクト
rule.new(str) が成功すれば正当とみなす。 rule.new(str) を返す。
other
str が rule.to_s と等しければ正当とみなす。 rule を返す。
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/stringvalidator.rb', line 198 def validate(rule, str) if rule == Integer then begin return Integer(str) rescue ArgumentError raise Error::NotInteger.new(str, rule) end end if rule == Float then begin return Float(str) rescue ArgumentError raise Error::NotFloat.new(str, rule) end end case rule when Range then if rule.first.is_a? Integer and rule.last.is_a? Integer then validate(Integer, str) raise Error::OutOfRange.new(str, rule) unless rule.include?(str.to_i) return str.to_i elsif rule.first.is_a? Numeric then validate(Float, str) raise Error::OutOfRange.new(str, rule) unless rule.include?(str.to_f) return str.to_f end raise Error::OutOfRange.new(str, rule) unless rule.include?(str) return str when Regexp then raise Error::RegexpMismatch.new(str, rule) unless rule =~ str return str when Proc then begin return rule.call(str) rescue raise Error::InvalidValue.new(str, rule) end return ret if ret when Array then rule.each do |i| begin return self.validate(i, str) rescue Error nil end end raise Error::InvalidValue.new(str, rule) when Hash then ret = {} rule.each do |k,v| case k when :any ret[k] = self.validate(v, str) when :all ret[k] = v.map{|i| self.validate(i, str)}.first when :rule ret[k] = self.validate(v, str) when :length begin self.validate v, str.length.to_s rescue Error raise Error::InvalidLength.new(str, rule) end when :maxlength raise Error::TooLong.new(str, rule) unless str.length <= v when :minlength raise Error::TooShort.new(str, rule) unless str.length >= v when :charlength begin self.validate v, str.split(//).length.to_s rescue Error raise Error::InvalidLength.new(str, rule) end when :maxcharlength raise Error::TooLong.new(str, rule) unless str.split(//).length <= v when :mincharlength raise Error::TooShort.new(str, rule) unless str.split(//).length >= v else raise ArgumentError, "Invalid key: #{k}" end end return ret[:rule] || ret[:any] || ret[:all] || str when Class then begin return rule.new(str) rescue raise Error::InvalidValue.new(str, rule) end when Symbol then raise ArgumentError, "No such rule: #{rule}" unless @rule and @rule.key? rule return self.class.validate(@rule[rule], str) else return rule if rule.to_s == str end raise Error::InvalidValue.new(str, rule) end |
#validated_rule(str) ⇒ Object
- str
-
対象文字列
initialize に与えた Hash のキーのルールを順に評価し、最初に適合したキー値を返す。 適合したルールがない場合は nil を返す。
313 314 315 316 317 318 319 320 |
# File 'lib/stringvalidator.rb', line 313 def validated_rule(str) @rule.keys.sort{|a,b|a.to_s<=>b.to_s}.each do |k| if valid?(@rule[k], str) then return k end end return nil end |