Sha256: f701f71382d763315eda0a92bac15dfa5b28d39996fa585b5ebe46ce51063d00

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require 'semi/variables/string'
require 'semi/variables/integer'
require 'semi/variables/boolean'
require 'semi/variables/path'
require 'semi/variables/url'

module Semi

  class VariableError < RuntimeError; end 

  module Variable

    # Variable types are ordered from most to least specific
    @@var_types = { 'url'       =>   Semi::Variables::Url,
                    'path'      =>   Semi::Variables::Path,
                    'boolean'   =>   Semi::Variables::Boolean,
                    'integer'   =>   Semi::Variables::Integer,
                    'string'    =>   Semi::Variables::String, }

    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 == 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

    


  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
semi-0.5.0 lib/semi/variable.rb