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_hashObject (readonly)

Returns the value of attribute args_hash.



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

def args_hash
  @args_hash
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

#dispatch(target) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/optitron/response.rb', line 66

def dispatch(target)
  if valid?
    target.params = params
    target.send(command.to_sym, *args)
  else
    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)


62
63
64
# File 'lib/optitron/response.rb', line 62

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
58
59
60
# File 'lib/optitron/response.rb', line 34

def validate
  compile_params
  @params_array.each { |(key, value)| key.run.call(params[key.name], self) if key.run }
  @args_array = @args_with_tokens.map { |(arg, val)| 
    begin
      [arg, arg.validate(val)]
    rescue
      add_error('invalid', arg.name)
      [arg, val]
    end
  }
  @args_hash = Hash[@args_array.map{|(arg, val)| [arg.name, val]}]
  
  @args = @args_array.inject([]) {|args, (arg, val)| arg.greedy? ? args.concat(val) : args.push(val); args}
  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.lit)
      end
    end
  end
end