class Kafo::DataTypes::Pattern
Public Class Methods
new(*regexes)
click to toggle source
# File lib/kafo/data_types/pattern.rb, line 4 def initialize(*regexes) @regex_strings = regexes @regexes = regexes.map { |r| ::Regexp.new(r) } end
Public Instance Methods
to_s()
click to toggle source
# File lib/kafo/data_types/pattern.rb, line 9 def to_s "regexes matching #{@regex_strings.map { |r| "/#{r}/" }.join(' or ')}" end
valid?(input, errors = [])
click to toggle source
# File lib/kafo/data_types/pattern.rb, line 13 def valid?(input, errors = []) unless input.is_a?(::String) errors << "#{input.inspect} is not a valid string" return false end unless @regexes.any? { |r| r.match(input) } errors << "#{input} must match one of #{@regexes.join(', ')}" end return errors.empty? end