Class: Kafo::DataTypes::Pattern

Inherits:
Kafo::DataType show all
Defined in:
lib/kafo/data_types/pattern.rb

Instance Method Summary collapse

Methods inherited from Kafo::DataType

#condition_value, #dump_default, #multivalued?, new_from_string, parse_hash, register_type, split_arguments, #typecast, types, unregister_type

Constructor Details

#initialize(*regexes) ⇒ Pattern

Returns a new instance of Pattern.



4
5
6
7
# File 'lib/kafo/data_types/pattern.rb', line 4

def initialize(*regexes)
  @regex_strings = regexes
  @regexes = regexes.map { |r| ::Regexp.new(r) }
end

Instance Method Details

#to_sObject



9
10
11
# 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 = []) ⇒ Boolean

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
# 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