Class: DSLCompose::DSL::Arguments::Argument::EndWithValidation

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl_compose/dsl/arguments/argument/end_with_validation.rb

Defined Under Namespace

Classes: InvalidValueError, ValidationFailedError

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ EndWithValidation

Returns a new instance of EndWithValidation.



14
15
16
17
# File 'lib/dsl_compose/dsl/arguments/argument/end_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/end_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/end_with_validation.rb', line 38

def validate! value
  raise ValidationFailedError, "The argument is invalid. Expected `#{value}` to end with `#{@values}`" unless @values.filter { |v| value.end_with? v.to_s }.any?
  true
end