packed-model 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -44,7 +44,8 @@ module PackedModel
44
44
  case options[:type]
45
45
  when :string
46
46
  options[:strip] = true
47
- options[:pack_directive] = 'Z*' # null terminated string
47
+ options[:null] = true
48
+ options[:pack_directive] = 'Z*'
48
49
  when :integer, :int
49
50
  options[:default] ||= 0 # pack 'N' directive requires a value for an integer
50
51
  options[:bytesize] = 4
@@ -58,12 +59,16 @@ module PackedModel
58
59
  options[:default] = options[:value]
59
60
  options[:bytesize] = 4
60
61
  options[:pack_directive] = 'N' # network byte order
62
+ options[:unpack_callback] ||= create_marker_unpack_callback(options[:index], options[:value])
61
63
  when :custom
62
64
  raise "missing pack_directive for #{name}" unless options[:pack_directive]
63
65
  else
64
66
  raise "Unknown type #{options[:type]}"
65
67
  end
66
68
 
69
+ add_unpack_callback(options[:unpack_callback]) if options[:unpack_callback].is_a?(Proc)
70
+ add_unpack_callback(create_strip_unpack_callback(options[:index], options[:null])) if options[:strip]
71
+
67
72
  class_eval do
68
73
  attrs = self.fields << options
69
74
  ks = self.keys << options[:name]
@@ -212,21 +217,20 @@ module PackedModel
212
217
 
213
218
  def unpack(str)
214
219
  @values = str.unpack(self.class.pack_string)
215
- self.fields.each do |field|
216
- case field[:type]
217
- when :string
218
- if @values[field[:index]].empty?
219
- @values[field[:index]] = nil
220
- else
221
- @values[field[:index]].strip!
222
- end
223
- when :char
224
- @values[field[:index]].strip! if field[:strip]
225
- when :marker
226
- marker_value = @values[field[:index]]
227
- raise BadMarkerException.new("unpack failed, invalid marker value (#{marker_value}) expected #{field[:value]}") unless marker_value == field[:value]
228
- end
229
- end
220
+ self.unpack_callbacks.each { |c| c.call self } if self.unpack_callbacks
221
+ end
222
+
223
+ def self.add_unpack_callback(callback)
224
+ @unpack_callbacks ||= []
225
+ @unpack_callbacks << callback
226
+ end
227
+
228
+ def self.unpack_callbacks
229
+ @unpack_callbacks
230
+ end
231
+
232
+ def unpack_callbacks
233
+ self.class.unpack_callbacks
230
234
  end
231
235
 
232
236
  private
@@ -242,6 +246,28 @@ module PackedModel
242
246
  def self.pack_string
243
247
  ""
244
248
  end
249
+
250
+ def self.create_marker_unpack_callback(index, value)
251
+ Proc.new do |m|
252
+ raise BadMarkerException.new("unpack failed, invalid marker value (#{m.values[index]}) expected #{value}") unless m.values[index] == value
253
+ end
254
+ end
255
+
256
+ def self.create_strip_unpack_callback(index, null)
257
+ if null
258
+ Proc.new do |m|
259
+ if m.values[index].empty?
260
+ m.values[index] = nil
261
+ else
262
+ m.values[index].strip!
263
+ end
264
+ end
265
+ else
266
+ Proc.new do |m|
267
+ m.values[index].strip!
268
+ end
269
+ end
270
+ end
245
271
  end
246
272
  end
247
273
 
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{packed-model}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Doug Youch"]
12
+ s.date = %q{2013-05-15}
13
+ s.description = %q{Used to minimize storage space required to store list of data}
14
+ s.email = %q{doug@sessionm.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/packed_model.rb",
28
+ "lib/packed_model/base.rb",
29
+ "lib/packed_model/errors.rb",
30
+ "lib/packed_model/list.rb",
31
+ "packed-model.gemspec",
32
+ "spec/helper.rb",
33
+ "spec/packed_model/base_spec.rb",
34
+ "spec/packed_model/list_spec.rb",
35
+ "test/helper.rb",
36
+ "test/test_packed_model_base.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/dyouch5@yahoo.com/packed-model}
39
+ s.licenses = ["MIT"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.6}
42
+ s.summary = %q{PackedModel stores model data in a binary string}
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
50
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
51
+ s.add_development_dependency(%q<rcov>, [">= 0"])
52
+ s.add_development_dependency(%q<rspec>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
56
+ s.add_dependency(%q<rcov>, [">= 0"])
57
+ s.add_dependency(%q<rspec>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ s.add_dependency(%q<rspec>, [">= 0"])
64
+ end
65
+ end
66
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Doug Youch
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2013-05-02 00:00:00 -04:00
17
+ date: 2013-05-15 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -89,6 +89,7 @@ files:
89
89
  - lib/packed_model/base.rb
90
90
  - lib/packed_model/errors.rb
91
91
  - lib/packed_model/list.rb
92
+ - packed-model.gemspec
92
93
  - spec/helper.rb
93
94
  - spec/packed_model/base_spec.rb
94
95
  - spec/packed_model/list_spec.rb