Sha256: f55c343dd3e88444f92ee148adcd6a5d369e843de399e40cf508d50a2342b67f

Contents?: true

Size: 1.09 KB

Versions: 36

Compression:

Stored size: 1.09 KB

Contents

# -*- coding: utf-8 -*-
# Original Copyright (C) Rubikichi
# Modified by TAKEUCHI Hitoshi
# You can redistribute it and/or modify it under the terms of
# the Ruby's licence.

module TMarshal
  module_function
  def dump(obj, port = nil)
    dumped = dump_text(obj)
    if port
      port.write dumped
    end
    dumped
  end

  def load(port)
#    p port
    case port
    when String
      eval port.untaint
    when IO, StringIO
      eval port.read.untaint
    else
      raise 'Wrong type!'
    end
  end

  def restore(port)
    load(port)
  end

  def dump_text(obj)
    case obj
    when String
      obj.dump
    when Array
      "[\n"+obj.collect{|x| dump_text(x)+",\n"}.join+"]"
    when Hash
      "{\n"+obj.sort_by{|e| e[0].inspect}.collect{|k,v| "#{dump_text(k)} => #{dump_text(v)},\n"}.join+"}"
    when Numeric, Module, Regexp, Symbol, TrueClass, FalseClass, NilClass, Range
      obj.inspect
    when Time
      "Time.at(#{obj.to_i})"
    else
      raise 'Wrong type!'
    end
  end
end

if __FILE__ == $0
  puts TMarshal.dump({:age => 22, :lang => 'Ruby', :man => true, :day => Time.now})
end

Version data entries

36 entries across 21 versions & 1 rubygems

Version Path
hikiutils-0.2.4 lib/hikiutils/#tmarshal.rb#
hikiutils-0.2.4 lib/hikiutils/tmarshal.rb
hikiutils-0.2.3.17 lib/hikiutils/#tmarshal.rb#
hikiutils-0.2.3.17 lib/hikiutils/tmarshal.rb
hikiutils-0.2.3.16 lib/hikiutils/tmarshal.rb
hikiutils-0.2.3.16 lib/hikiutils/#tmarshal.rb#
hikiutils-0.2.3.15 lib/hikiutils/tmarshal.rb
hikiutils-0.2.3.15 lib/hikiutils/#tmarshal.rb#
hikiutils-0.2.3.14 lib/hikiutils/#tmarshal.rb#
hikiutils-0.2.3.14 lib/hikiutils/tmarshal.rb
hikiutils-0.2.3.13 lib/hikiutils/tmarshal.rb
hikiutils-0.2.3.13 lib/hikiutils/#tmarshal.rb#
hikiutils-0.2.3.12 lib/hikiutils/tmarshal.rb
hikiutils-0.2.3.12 lib/hikiutils/#tmarshal.rb#
hikiutils-0.2.3.11 lib/hikiutils/tmarshal.rb
hikiutils-0.2.3.11 lib/hikiutils/#tmarshal.rb#
hikiutils-0.2.3.9 lib/hikiutils/tmarshal.rb
hikiutils-0.2.3.9 lib/hikiutils/#tmarshal.rb#
hikiutils-0.2.3.8 lib/hikiutils/#tmarshal.rb#
hikiutils-0.2.3.8 lib/hikiutils/tmarshal.rb