Module: Spark::CommandValidator

Included in:
CommandBuilder
Defined in:
lib/spark/command_validator.rb

Instance Method Summary collapse

Instance Method Details

#valid?(value, options) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
# File 'lib/spark/command_validator.rb', line 8

def valid?(value, options)
  begin
    validate(value, options)
    return true
  rescue
    return false
  end
end

#validate(value, options) ⇒ Object



4
5
6
# File 'lib/spark/command_validator.rb', line 4

def validate(value, options)
  validate_type(value, options[:type])
end

#validate_size(array1, array2) ⇒ Object



27
28
29
30
31
# File 'lib/spark/command_validator.rb', line 27

def validate_size(array1, array2)
  if array1.size != array2.size
    error "Wrong number of arguments (#{array1.size} for #{array2.size})"
  end
end

#validate_type(value, types) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/spark/command_validator.rb', line 17

def validate_type(value, types)
  types = [types] if !types.is_a?(Array)

  types.each do |type|
    return if value.is_a?(type)
  end

  error "Value: #{value} should be a #{types.join(' or ')} but is #{value.class}."
end