Module: TMarshal

Defined in:
lib/hikiutils/tmarshal.rb

Overview

Original Copyright © Rubikichi Modified by TAKEUCHI Hitoshi You can redistribute it and/or modify it under the terms of the Ruby’s licence.

Class Method Summary collapse

Class Method Details

.dump(obj, port = nil) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/hikiutils/tmarshal.rb', line 8

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

.dump_text(obj) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hikiutils/tmarshal.rb', line 31

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

.load(port) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/hikiutils/tmarshal.rb', line 16

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

.restore(port) ⇒ Object



27
28
29
# File 'lib/hikiutils/tmarshal.rb', line 27

def restore(port)
  load(port)
end