Module: MasterView::ParserHelpers

Included in:
TemplateProcessing::SAXParserListener
Defined in:
lib/masterview/parser_helpers.rb

Instance Method Summary collapse

Instance Method Details

#parse_eval_into_array(value) ⇒ Object

returns an array of args by parsing and evaling the str value passed in uses evaling, so can’t have any variables only simple strings, numbers, booleans



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/masterview/parser_helpers.rb', line 5

def parse_eval_into_array(value)
  return [] if value.nil? || value.empty?
  val = value.strip
  args = []
  until val.empty?
    if val =~ /^[:'"%\[{&*]/ #starts with quote or ruby lang char
      v = nil
      val = '{'+val+'}' if val =~ /^:/ #starts with colon, assume hash so wrap with brackets
      eval 'v = '+ val #rest is all evaled
      if v.is_a? Array
        args += v
      else 
        args << v
      end
      break
    else
      unquoted_string = val.slice!( /^[^,]+/ ) #pull off everything up to a comma
      unquoted_string.strip!
      args.push unquoted_string
      val.slice!( /^,/ ) #strip off comma if exists
      val.strip!
    end
  end
  args
end

#parse_eval_into_hash(value, default_key) ⇒ Object

returns a hash, for values that are not already part of hash it adds them using default_key uses evaling so it cannot have any variables or non-simple types



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/masterview/parser_helpers.rb', line 33

def parse_eval_into_hash(value, default_key)
  h = {}
  a = parse_eval_into_array(value)
  a.each do |v|
    if v.is_a?(Hash)
      h.merge!(v)
    else #it adds any additional non-hash args using default key, if key,val exists, it changes to array and appends
      prev = h[default_key]
      if prev.nil? #nil just add it
        h[default_key] = v
      elsif prev.is_a?(Array) #was array, concat
        h[default_key] = prev+v
      else #anything else, make it into array
        h[default_key] = [prev, v]
      end
    end
  end
  h
end