Class: Kafo::DataTypes::Pattern
- Inherits:
-
Kafo::DataType
- Object
- Kafo::DataType
- Kafo::DataTypes::Pattern
- Defined in:
- lib/kafo/data_types/pattern.rb
Instance Method Summary collapse
-
#initialize(*regexes) ⇒ Pattern
constructor
A new instance of Pattern.
- #to_s ⇒ Object
- #valid?(input, errors = []) ⇒ Boolean
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_s ⇒ Object
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
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 |