Class: Choosy::Verifier

Inherits:
Object
  • Object
show all
Defined in:
lib/choosy/verifier.rb

Instance Method Summary collapse

Instance Method Details

#convert!(option, result) ⇒ Object



81
82
83
84
85
86
# File 'lib/choosy/verifier.rb', line 81

def convert!(option, result)
  value = result[option.name]
  if exists? value
    result[option.name] = Converter.convert(option.cast_to, value)
  end
end

#populate!(option, result) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/choosy/verifier.rb', line 65

def populate!(option, result)
  return if special?(option)

  if !result.options.has_key?(option.name) # Not already set
    if !option.default_value.nil? # Has default?
      result[option.name] = option.default_value
    elsif option.cast_to == :boolean
      result[option.name] = false
    elsif option.arity.max > 1
      result[option.name] = []
    else
      result[option.name] = nil
    end
  end
end

#required?(option, result) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/choosy/verifier.rb', line 59

def required?(option, result)
  if option.required? && result[option.name].nil?
    raise ValidationError.new("required option missing: '#{option.long_flag}'")
  end
end

#restricted?(option, result) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/choosy/verifier.rb', line 88

def restricted?(option, result)
  return unless option.restricted?
  
  value = result[option.name]
  if option.arity.max > 1
    value.each do |val|
      check(option.allowable_values, val)
    end
  else
    check(option.allowable_values, value)
  end
end

#validate!(option, result) ⇒ Object



101
102
103
104
105
106
# File 'lib/choosy/verifier.rb', line 101

def validate!(option, result)
  value = result[option.name]
  if option.validation_step && exists?(value)
    option.validation_step.call(value, result.options)
  end
end

#verify!(result) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/choosy/verifier.rb', line 3

def verify!(result)
  result.command.options.each do |option|
    required?(option, result)
    populate!(option, result)
    convert!(option, result)
    restricted?(option, result)
    validate!(option, result)
  end

  verify_arguments!(result)
end

#verify_arguments!(result) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/choosy/verifier.rb', line 23

def verify_arguments!(result)
  if result.command.is_a?(Choosy::Command)
    prefix = if result.subresult?
               "#{result.command.name}: "
             else
               ""
             end

    if result.command.arguments
      arguments = result.command.arguments

      result.args.map! do |value|
        Converter.convert(arguments.cast_to, value)
      end

      if result.args.length < arguments.arity.min
        raise Choosy::ValidationError.new("#{prefix}too few arguments (minimum is #{arguments.arity.min})")
      elsif result.args.length > arguments.arity.max
        raise Choosy::ValidationError.new("#{prefix}too many arguments (max is #{arguments.arity.max}): '#{result.args[arguments.arity.max]}'")
      end 

      result.args.each do |val|
        check(arguments.allowable_values, val)
      end if arguments.restricted?

      if arguments.validation_step
        arguments.validation_step.call(result.args, result.options)
      end
    else
      if result.args.length > 0
        raise Choosy::ValidationError.new("#{prefix}no arguments allowed: #{result.args.join(' ')}")
      end
    end
  end
end

#verify_special!(result) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/choosy/verifier.rb', line 15

def verify_special!(result)
  result.command.options.each do |option|
    if special?(option)
      validate!(option, result) 
    end
  end
end