Module: Semi::Variable

Defined in:
lib/semi/variable.rb

Constant Summary collapse

@@var_types =

Variable types are ordered from most to least specific

{ 'url'       =>   Semi::Variables::Url,
'path'      =>   Semi::Variables::Path,
'boolean'   =>   Semi::Variables::Boolean,
'integer'   =>   Semi::Variables::Integer,
'string'    =>   Semi::Variables::String, }

Class Method Summary collapse

Class Method Details

.expand(val, dict) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/semi/variable.rb', line 54

def self.expand(val, dict)
  unless ['Semi::Variables::String', 'String'].include? val.class.to_s
    return val
  end

  check = true
  while check
    # Look for simple variable expansion
    if match = /\$\{?(\w+)\}?/.match(val)
      val = match.pre_match + dict[match[1].downcase] + match.post_match
    elsif match = /\$\((.*)\)/.match(val)
      val = match.pre_match + `#{match[1]} 2>/dev/null` + match.post_match
    else
      # no more matches... we must be done...
      check = false
    end
  end

  return val
end

.import(val, hints = nil) ⇒ Object



22
23
24
25
26
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
# File 'lib/semi/variable.rb', line 22

def self.import(val, hints=nil)
  # If hints have been supplied, try to create from them
  unless hints.nil?
    hints = [hints].flatten.select {|h| @@var_types.include? h }
    if hints.count > 0
      return @@var_types[hints[0]].new(val)
    end
  end

  # look for the obsure patterns before returning a string var
  case
  when Semi::Variables::Url::validate(val)
    return Semi::Variables::Url.new(val)
  when Semi::Variables::Path.validate(val)
    return Semi::Variables::Path.new(val)
  when Semi::Variables::Boolean.validate(val)
    return Semi::Variables::Boolean.new(val)
  when Semi::Variables::Integer.validate(val)
    return Semi::Variables::Integer.new(val)
  when val.class == 1.class   # Fixnum vs Integer
    return Semi::Variables::Integer.new(val)
  when val.class == TrueClass
    return Semi::Variables::Boolean.new(val)
  when val.class == FalseClass
    return Semi::Variables::Boolean.new(val)
  else
    return Semi::Variables::String.new(val)
  end

end