Sha256: 7c7d4743e09367c0560eeab03acbb983fa353bda5e0e0590e0e750781212ebf4
Contents?: true
Size: 1.45 KB
Versions: 1
Compression:
Stored size: 1.45 KB
Contents
class Gom::Core::Primitive TypeMap = { Symbol => :symbol, Fixnum => :integer, Bignum => :integer, #BigDecimal => :decimal, Float => :float, Date => :date, DateTime => :datetime, Time => :datetime, TrueClass => :boolean, FalseClass => :boolean, URI::HTTP => :uri, URI::HTTPS => :uri, URI::Generic => :uri, } TypeCodes = TypeMap.values.uniq.sort Parsers = { :date => Proc.new { |date| ::Date.parse(date) }, :datetime => Proc.new { |time| ::DateTime.parse(time) }, :float => Proc.new { |txt| txt.to_f }, :integer => Proc.new { |txt| txt.to_i }, :uri => Proc.new { |s| URI.parse(s) }, :boolean => Proc.new { |s| (s == 'true' ? true : false) }, } Formatters = Hash.new(Proc.new{|o|o.to_s}).update( :string => Proc.new { |s| s.to_s }, :date => Proc.new { |date| date.strftime('%Y-%m-%d') }, #.to_s(:db) }, :datetime => Proc.new { |time| # back and forth, trying to 'normalize' the myriad of time formats DateTime.parse(time.to_s).strftime #DateTime.parse(time.to_s).xmlschema #time.xmlschema } ) # text, type -> value def self.decode txt, type = :string parser = type && Parsers[type.to_sym] parser ? parser.call(txt) : txt end # value -> text, type def self.encode value type = TypeMap[value.class] || :string formatter = Formatters[type] [ formatter.call(value), type] end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
gom-core-0.2.7 | lib/gom/core/primitive.rb |