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

.import(val, hints = nil) ⇒ Object



21
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
# File 'lib/semi/variable.rb', line 21

def self.import(val, hints=nil)
  # If hints have been supplied, try to create from them
  if not 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 == Fixnum
    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