satt 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/satt.rb +23 -25
  2. metadata +22 -6
@@ -4,11 +4,11 @@ class Satt
4
4
  class InvalidArgument < RuntimeError; end
5
5
 
6
6
  def self.dump(obj)
7
- MessagePack.dump(Satt::Primitive::Dumper.new.dump(obj))
7
+ MessagePack.pack(Satt::Primitive::Dumper.new.dump(obj))
8
8
  end
9
9
 
10
10
  def self.load(blob)
11
- Satt::Primitive::Loader.new.load(MessagePack.load(blob))
11
+ Satt::Primitive::Loader.new.load(MessagePack.unpack(blob))
12
12
  end
13
13
  end
14
14
 
@@ -108,55 +108,53 @@ class Satt
108
108
  raise InvalidArgument unless priv.last.class == objclass
109
109
  end
110
110
 
111
- # TODO: allocate object immediatelly and add to cache
112
- # even if ivars not present (but come later), error if two entries
113
- # with same id and both with ivars
114
- if priv.length == 2
115
- return fetch_obj(priv[1])
111
+ if priv.length == 2 and cache_has(priv[1])
112
+ return cache_get(priv[1])
116
113
  end
117
114
 
115
+ obj = cache_has(priv[1]) ? cache_get(priv[1]) : cache_put(priv[1], objclass.allocate)
116
+
118
117
  if objclass == Array
119
- return cache_obj(priv[1], priv[2].map{ |e| load(e) })
118
+ priv[2].each_with_index do |e,i|
119
+ obj[i] = load(e)
120
+ end
121
+ return obj
120
122
  end
121
123
 
122
124
  if objclass == Hash
123
- return cache_obj(priv[1], priv[2].reduce(Hash.new) { |hash, (key, value)|
125
+ return priv[2].reduce(obj) { |hash, (key, value)|
124
126
  hash[load(key)] = load(value)
125
127
  hash
126
- })
128
+ }
127
129
  end
128
130
 
129
131
  if objclass == String
130
132
  raise InvalidArgument if priv.last.class != String
131
- return cache_obj(priv[1], priv[2])
133
+ return obj.gsub!(/\A.*\z/, priv[2])
132
134
  end
133
135
 
134
136
  if priv.last.class != Hash
135
137
  raise InvalidArgument, priv.last.inspect
136
138
  end
137
139
 
138
- return build_and_cache_obj(objclass, priv[1], priv[2])
140
+ priv[2].each do |(var, val)|
141
+ obj.instance_variable_set "@#{var}".to_sym, load(val)
142
+ end
143
+ return obj
139
144
  end
140
145
 
141
146
  private
142
147
 
143
- def fetch_obj(ref)
144
- return @objs[ref] if @objs[ref]
145
- raise InvalidArgument, "can't find object with reference id #{ref.to_s}"
148
+ def cache_has(ref)
149
+ @objs.key?(ref)
146
150
  end
147
151
 
148
- def cache_obj(ref, obj)
149
- return @objs[ref] = obj unless @objs[ref]
150
- raise InvalidArgument, "an object with reference id #{ref.to_s} already exists"
152
+ def cache_get(ref)
153
+ @objs[ref]
151
154
  end
152
155
 
153
- def build_and_cache_obj(objclass, ref, ivars)
154
- # Cache first, only then go deeper, to avoid following circular references
155
- obj = cache_obj(ref, objclass.allocate)
156
- ivars.each do |(var, val)|
157
- obj.instance_variable_set "@#{var}".to_sym, load(val)
158
- end
159
- return obj
156
+ def cache_put(ref, obj)
157
+ @objs[ref] = obj
160
158
  end
161
159
 
162
160
  def get_class(id)
metadata CHANGED
@@ -1,16 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: satt
3
3
  version: !ruby/object:Gem::Version
4
+ version: 0.0.2
4
5
  prerelease:
5
- version: 0.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Florian Weingarten
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-24 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-05-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: msgpack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description: Serializing arbitrary Ruby objects with MessagePack
15
31
  email: flo@hackvalue.de
16
32
  executables: []
@@ -18,24 +34,24 @@ extensions: []
18
34
  extra_rdoc_files: []
19
35
  files:
20
36
  - lib/satt.rb
21
- homepage: http://rubygems.org/gems/satt
37
+ homepage: https://github.com/fw42/satt/
22
38
  licenses: []
23
39
  post_install_message:
24
40
  rdoc_options: []
25
41
  require_paths:
26
42
  - lib
27
43
  required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
28
45
  requirements:
29
46
  - - ! '>='
30
47
  - !ruby/object:Gem::Version
31
48
  version: '0'
32
- none: false
33
49
  required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
34
51
  requirements:
35
52
  - - ! '>='
36
53
  - !ruby/object:Gem::Version
37
54
  version: '0'
38
- none: false
39
55
  requirements: []
40
56
  rubyforge_project:
41
57
  rubygems_version: 1.8.23