Class: RJR::Arguments
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/rjr/util/args.rb
Overview
Encapsulates a list of JSON-RPC method arguments as sent from the client to the server, in addition to providing various helper / utility methods.
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
Instance Method Summary collapse
-
#extract(map) ⇒ Object
Extract groups of values from argument list.
-
#initialize(args = {}) ⇒ Arguments
constructor
A new instance of Arguments.
-
#specifier_for(tag) ⇒ Object
Return specifier corresponding given tag.
-
#specifies?(tag) ⇒ Boolean
Return boolean if tag appears in argument list.
-
#validate!(*acceptable) ⇒ Object
Validate arguments against acceptable values.
Constructor Details
#initialize(args = {}) ⇒ Arguments
Returns a new instance of Arguments.
21 22 23 |
# File 'lib/rjr/util/args.rb', line 21 def initialize(args={}) @args = args[:args] || [] end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
17 18 19 |
# File 'lib/rjr/util/args.rb', line 17 def args @args end |
Instance Method Details
#extract(map) ⇒ Object
Extract groups of values from argument list
Groups are generated by comparing arguments to keys in the specified map and on matches extracting the # of following arguments specified by the map values
Note arguments / keys are converted to strings before comparison
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/rjr/util/args.rb', line 93 def extract(map) # clone map hash, swap keys for string map = Hash[map] map.keys.each { |k| map[k.to_s] = map[k] map.delete(k) unless k.is_a?(String) } groups = [] i = 0 while(i < length) do val = self[i] i += 1 next unless !!map.has_key?(val) num = map[val] group = [val] 0.upto(num-1) do |j| group << self[i] i += 1 end groups << group end groups end |
#specifier_for(tag) ⇒ Object
Return specifier corresponding given tag. The specifier is defined as the value appearing in the argument list immediately after the tag
140 141 142 143 |
# File 'lib/rjr/util/args.rb', line 140 def specifier_for(tag) return nil unless specifies?(tag) self[index(tag) + 1] end |
#specifies?(tag) ⇒ Boolean
Return boolean if tag appears in argument list. Simple wrapper around includes setting up the scope of argument ‘specifiers’
128 129 130 |
# File 'lib/rjr/util/args.rb', line 128 def specifies?(tag) include?(tag) end |
#validate!(*acceptable) ⇒ Object
Validate arguments against acceptable values.
Raises error if value is found which is not on list of acceptable values.
If acceptable values are hash’s, keys are compared and on matches the values are used as the # of following arguments to skip in the validator
Note args / acceptable params are converted to strings before comparison
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rjr/util/args.rb', line 45 def validate!(*acceptable) i = 0 if acceptable.first.is_a?(Hash) # clone acceptable hash, swap keys for string acceptable = Hash[acceptable.first] acceptable.keys.each { |k| acceptable[k.to_s] = acceptable[k] acceptable.delete(k) unless k.is_a?(String) } # compare acceptable against arguments, raising error if issue found while(i < length) do val = self[i] passed = acceptable.has_key?(val) raise ArgumentError, "#{val} not an acceptable arg" unless passed skip = acceptable[val] i += (skip + 1) end else # clone acceptable array, swap values for string acceptable = Array.new(acceptable).map { |a| a.to_s } # compare acceptable against arguments, raising error if issue found while(i < length) do val = self[i].to_s passed = acceptable.include?(val) raise ArgumentError, "#{val} not an acceptable arg" unless passed i += 1 end end nil end |