Class: Optitron::Response
- Inherits:
-
Object
- Object
- Optitron::Response
- Defined in:
- lib/optitron/response.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#args_hash ⇒ Object
readonly
Returns the value of attribute args_hash.
-
#args_with_tokens ⇒ Object
readonly
Returns the value of attribute args_with_tokens.
-
#command ⇒ Object
Returns the value of attribute command.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#params_array ⇒ Object
readonly
Returns the value of attribute params_array.
Instance Method Summary collapse
- #add_error(type, field = nil) ⇒ Object
- #compile_params ⇒ Object
- #dispatch(target) ⇒ Object
- #error_messages ⇒ Object
-
#initialize(parser, tokens) ⇒ Response
constructor
A new instance of Response.
- #valid? ⇒ Boolean
- #validate ⇒ Object
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
#args ⇒ Object (readonly)
Returns the value of attribute args.
3 4 5 |
# File 'lib/optitron/response.rb', line 3 def args @args end |
#args_hash ⇒ Object (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_tokens ⇒ Object (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 |
#command ⇒ Object
Returns the value of attribute command.
4 5 6 |
# File 'lib/optitron/response.rb', line 4 def command @command end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
3 4 5 |
# File 'lib/optitron/response.rb', line 3 def errors @errors end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
3 4 5 |
# File 'lib/optitron/response.rb', line 3 def params @params end |
#params_array ⇒ Object (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_params ⇒ Object
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 .join("\n") end end |
#error_messages ⇒ Object
19 20 21 |
# File 'lib/optitron/response.rb', line 19 def @errors.map{|(error, field)| field ? "#{field} is #{error}".capitalize : error.capitalize} end |
#valid? ⇒ Boolean
62 63 64 |
# File 'lib/optitron/response.rb', line 62 def valid? @errors.empty? end |
#validate ⇒ Object
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 |