Class: DSLCompose::DSL::Arguments::Argument::StartWithValidation
- Inherits:
-
Object
- Object
- DSLCompose::DSL::Arguments::Argument::StartWithValidation
- Defined in:
- lib/dsl_compose/dsl/arguments/argument/start_with_validation.rb
Defined Under Namespace
Classes: InvalidValueError, ValidationFailedError
Instance Method Summary collapse
- #add_values(values) ⇒ Object
-
#initialize(values) ⇒ StartWithValidation
constructor
A new instance of StartWithValidation.
- #validate!(value) ⇒ Object
Constructor Details
#initialize(values) ⇒ StartWithValidation
Returns a new instance of StartWithValidation.
14 15 16 17 |
# File 'lib/dsl_compose/dsl/arguments/argument/start_with_validation.rb', line 14 def initialize values @values = [] add_values values end |
Instance Method Details
#add_values(values) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/dsl_compose/dsl/arguments/argument/start_with_validation.rb', line 19 def add_values values # if the provided values is a single item then convert it to an array if values.is_a?(Symbol) || values.is_a?(String) values = [values] end # assert that the provided values is an array unless values.is_a? Array raise ValidationFailedError, "The value `#{values}` provided to this validator must be a Symbol/String or an Array of Symbols/Strings" end # assert that the provided values is an array of symbols/strings unless values.all? { |value| value.is_a? Symbol } || values.all? { |value| value.is_a? String } raise ValidationFailedError, "The value `#{values}` provided to this validator must be a Symbol/String or an Array of Symbols/Strings" end @values += values end |
#validate!(value) ⇒ Object
38 39 40 41 |
# File 'lib/dsl_compose/dsl/arguments/argument/start_with_validation.rb', line 38 def validate! value raise ValidationFailedError, "The argument is invalid. Expected `#{value}` to start with `#{@values}`" unless @values.filter { |v| value.start_with? v.to_s }.any? true end |