IceDragon200

[RUBY]Notetag (C-Style)

May 12th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.03 KB | None | 0 0
  1. # // 05/11/2012
  2. # // 05/11/2012
  3. module Notetag
  4.   def self.str2bool(str)
  5.     case str.upcase
  6.     when "TRUE", "YES", "T", "Y" ; true
  7.     when "FALSE", "NO", "F", "N" ; false
  8.     else                         ; nil
  9.     end
  10.   end
  11.   @data_types = {
  12.     # // [keywords..] => proc { |s| parse_to_proper_type }
  13.     ["string" ,"str" ]=> proc {|s|String(s)  },
  14.     ["integer","int" ]=> proc {|s|Integer(s) },
  15.     ["boolean","bool"]=> proc {|s|str2bool(s)},
  16.     ["float"  ,"flt" ]=> proc {|s|Float(s)   },
  17.     ["hex"]           => proc {|s|s.hex      }
  18.   }
  19.   str = "(a-)?(%s):(.*)" % @data_types.keys.collect{|a|"(?:"+a.join("|")+?)}.join("|")
  20.   DTREGEX = Regexp.new(str,?i)
  21.   @data_types.enum2keys!
  22.   def self.parse_dtstr(dtstr)
  23.     mtch = dtstr.match DTREGEX
  24.     is_array, dt_type, value = mtch[1,3]
  25.     is_array = !!is_array
  26.     parser = @data_types[dt_type.downcase]
  27.     #puts "is_array?[%s] dt_type[%s] value[%s]" % [is_array, dt_type, value]
  28.     (is_array ? value.split(?,) : Array(value)).collect{|n|parser.call(n)}
  29.   end
  30. end
Advertisement
Add Comment
Please, Sign In to add comment