Sha256: f043063009f7708d4138b60370f0b9c3e4de9fa88bc0c8c34492fbfc636e10ec

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

#
# Overload the class string to convert monetdb to ruby types.
#

class String

  def getInt
    to_i
  end

  def getFloat
    to_f
  end

  def getString
    gsub(/^"/, "").gsub(/"$/, "")
  end

  # Convert from HEX to the origianl binary data.
  def getBlob
    blob = ""
    scan(/../) { |tuple| blob += tuple.hex.chr }
    blob
  end

  # Ruby currently supports only time formatted timestamps; treat TIME as string.
  def getTime
    gsub(/^"/, "").gsub(/"$/, "")
  end

  # Ruby currently supports only date formatted timestamps; treat DATE as string.
  def getDate
    gsub(/^"/, "").gsub(/"$/, "")
  end

  def getDateTime
    date = split(" ")[0].split("-")
    time = split(" ")[1].split(":")
    Time.gm(date[0], date[1], date[2], time[0], time[1], time[2])
  end

  def getChar
    # Ruby < 1.9 does not have a char datatype
    begin
      ord
    rescue
      self
    end
  end

  def getBool
    if %w(1 y t true).include?(self)
      true
    elsif %w(0 n f false).include?(self)
      false
    end
  end

  def getNull
    if upcase == "NONE"
      nil
    else
      raise "Unknown value"
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
monetdb-0.1.3 lib/monetdb/core_ext/string.rb
monetdb-0.1.2 lib/monetdb/core_ext/string.rb
monetdb-0.1.1 lib/monetdb/core_ext/string.rb
monetdb-0.1.0 lib/monetdb/core_ext/string.rb