Sha256: 5397597536345bd78344930becd52d785d23361f10472dea22ac6caac869fcf5

Contents?: true

Size: 1.47 KB

Versions: 8

Compression:

Stored size: 1.47 KB

Contents

module Gom
  module Core
  end
end

class Gom::Core::Primitive

  Types = {
      "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,
  }

  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) },
  }

  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 = Types[value.class.name] || :string
    formatter = Formatters[type]
    [ formatter.call(value), type]
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gom-core-0.2.5 lib/gom/core/primitive.rb
gom-core-0.2.4 lib/gom/core/primitive.rb
gom-core-0.2.3 lib/gom/core/primitive.rb
gom-core-0.2.2 lib/gom/core/primitive.rb
gom-core-0.2.1 lib/gom/core/primitive.rb
gom-core-0.2.0 lib/gom/core/primitive.rb
gom-core-0.1.1 lib/gom/core/primitive.rb
gom-core-0.1.0 lib/gom/core/primitive.rb