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



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

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

.dump_text(obj) ⇒ Object



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

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



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

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

.restore(port) ⇒ Object



29
30
31
# File 'lib/hikiutils/tmarshal.rb', line 29

def restore(port)
  load(port)
end