Module: Simple::Marshal

Defined in:
lib/appswarm/simple_marshal.rb

Defined Under Namespace

Classes: Buf

Constant Summary collapse

INDENT =
"  "

Class Method Summary collapse

Class Method Details

.dump(data) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/appswarm/simple_marshal.rb', line 10

def self.dump(data)
  case data
    when String
      "\""+data.gsub("\\"){"\\\\"}.gsub("\"","\\\"").gsub("\n","\\n")+"\""
    when Numeric
      data.to_s
    when Array
      "[\n"+data.map{|d|INDENT+dump(d)}.join(",\n")+"\n]"
    when Hash
      "{\n"+INDENT+data.map{|k,v|dump(k)+" => "+dump(v)}.join(",\n").gsub("\n","\n"+INDENT)+"\n}"
    when Symbol
      ":"+data.to_s
    when NilClass
      "nil"
    when FalseClass
      "false"
    when TrueClass
      "true"
    else
      o=data
      hash=nil
      n=o.class.to_s
      case o
        when Time
          hash={:data=>o.to_s}
      end
      if o.respond_to?(:members) and o.respond_to?(:values) 
        hash={}
        o.members.each{|m|
          hash[m.to_sym]=o.send(m)
        }
      end
      if hash
        "("+n+"\n"+hash.map{|k,v|INDENT+dump(k)+" => "+dump(v)}.join(",\n")+"\n)"
      else
        raise "Unknown Type to marshal #{data} #{data.class}"
      end
  end
end

.load(str) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/appswarm/simple_marshal.rb', line 59

def self.load(str)
  if false
    @@parser||=nil
    unless @@parser

      Treetop. load File.expand_path("../simple_marshal_parser.tt",__FILE__)
      @@parser=true
    end
    kcode=$KCODE
    parser = SimpleMarshalParser.new
    result = parser.parse(str)
  #pp result
    $KCODE=kcode
    if result
      pp "GOT RESULT"
      return result.sload
    else
      nil
    end
  else
    @@parser||=nil
    unless @@parser

      #Treetop.load File.expand_path("../simple_marshal_parser.tt",__FILE__)
      @@parser=true
    end
    parser = SimpleMarshalParser #.new
    result = parser.parse(str)
    if result
      #pp "GOT RESULT"
      return result.convert
    else
      nil
    end
  end
end