Module: Patriot::Util::Param
- Included in:
- Command::Base
- Defined in:
- lib/patriot/util/param.rb
Overview
namespace for parameter handling functions
Instance Method Summary collapse
-
#eval_attr(attr_val) ⇒ Object
replace parameter values in command attribute valeus.
-
#eval_string_attr(str, vars = {}) ⇒ String
evaluate variables in a string expression.
Instance Method Details
#eval_attr(attr_val) ⇒ Object
replace parameter values in command attribute valeus
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/patriot/util/param.rb', line 8 def eval_attr(attr_val) if attr_val.is_a?(Hash) entries = {} attr_val.each{|k,v| entries[eval_attr(k)] = eval_attr(v) } return entries elsif attr_val.is_a?(Array) return attr_val.map{|e| eval_attr(e)} elsif attr_val.is_a?(String) return eval_string_attr(attr_val) else # only evaluate attributes in String return attr_val end end |
#eval_string_attr(str, vars = {}) ⇒ String
evaluate variables in a string expression
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 58 59 60 61 |
# File 'lib/patriot/util/param.rb', line 27 def eval_string_attr(str, vars = {}) s = StringScanner.new(str) s.scan(/(.*?)\#\{/m) # retrun immediatelly if variables are not contained return str unless s.matched? prefix = s[1] nest = 1 # depth of parenthesis var = "" # variable expression prev_rest = s.rest while nest > 0 tmp = s.scan(/(.*?)[\{\}]/m) # for hash objects, etc if s.matched? if /.*?\{/ =~ tmp nest = nest + 1 var << s[0] else nest = nest - 1 if nest > 0 var << s[0] else # does not include the last parenthesis indicates end of the variable var << s[1] end end end raise "infinte loop #{str} : rest #{s.rest} : #{nest}" if prev_rest == s.rest prev_rest = s.rest end # evaluate the variable var_binding = build_var_binding(vars) var_binding = binding if var_binding.nil? evaled_var = eval var, var_binding # farther variables are handled by next invocation return "#{prefix}#{evaled_var}#{eval_string_attr(prev_rest, vars)}" end |