Class: Optitron::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/optitron/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, tokens) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
11
12
13
# File 'lib/optitron/response.rb', line 5

def initialize(parser, tokens)
  @parser, @tokens = parser, tokens
  @params_array = []
  @args_with_tokens = []
  @args = []
  @command = nil
  @errors = []
  @params = {}
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/optitron/response.rb', line 3

def args
  @args
end

#args_with_tokensObject (readonly)

Returns the value of attribute args_with_tokens.



3
4
5
# File 'lib/optitron/response.rb', line 3

def args_with_tokens
  @args_with_tokens
end

#commandObject

Returns the value of attribute command.



4
5
6
# File 'lib/optitron/response.rb', line 4

def command
  @command
end

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/optitron/response.rb', line 3

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/optitron/response.rb', line 3

def params
  @params
end

#params_arrayObject (readonly)

Returns the value of attribute params_array.



3
4
5
# File 'lib/optitron/response.rb', line 3

def params_array
  @params_array
end

Instance Method Details

#add_error(type, field = nil) ⇒ Object



15
16
17
# File 'lib/optitron/response.rb', line 15

def add_error(type, field = nil)
  @errors << [type, field]
end

#compile_paramsObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/optitron/response.rb', line 23

def compile_params
  @params_array.each do |(key, value)|
    begin
      params[key.name] = key.validate(value)
    rescue
      add_error('invalid', key.name)
      params[key.name] = value
    end
  end
end

#dispatchObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/optitron/response.rb', line 59

def dispatch
  raise unless @parser.target
  if valid?
    dispatch_args = params.empty? ? args : args + [params]
    @parser.target.send(command.to_sym, *dispatch_args)
  else
    puts @parser.help
    puts "\nErrors:"
    puts error_messages.join("\n")
  end
end

#error_messagesObject



19
20
21
# File 'lib/optitron/response.rb', line 19

def error_messages
  @errors.map{|(error, field)| field ? "#{field} is #{error}".capitalize : error.capitalize}
end

#valid?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/optitron/response.rb', line 71

def valid?
  @errors.empty?
end

#validateObject



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/optitron/response.rb', line 34

def validate
  compile_params
  @args = @args_with_tokens.map { |(arg, tok)| 
    begin
      tok.is_a?(Array) ? tok.map{ |t| arg.validate(t.val) } : arg.validate(tok.val)
    rescue
      add_error('invalid', arg.name)
      tok.is_a?(Array) ? tok.map{ |t| t.val } : tok.val
    end
  }
  @args.flatten!
  unless @tokens.empty?
    @tokens.select{|t| t.respond_to?(:name)}.each do |named_token|
      @tokens.delete(named_token)
      add_error('unrecognized', named_token.name)
    end
    
    if @errors.empty?
      @tokens.each do |token|
        add_error('unrecognized', token.val)
      end
    end
  end
end