marcandre-packable 1.1.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +4 -0
- data/LICENSE +26 -0
- data/README.rdoc +3 -1
- data/VERSION.yml +3 -3
- data/lib/packable/extensions/io.rb +6 -3
- data/lib/packable/packers.rb +1 -0
- data/lib/packable.rb +2 -2
- data/test/packing_doc_test.rb +1 -0
- data/test/packing_test.rb +9 -4
- metadata +20 -8
- data/lib/packable/jungle_survival_kit.rb +0 -72
data/CHANGELOG.rdoc
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
= Packable --- History
|
2
2
|
|
3
|
+
== Version 1.2 - April 2nd, 2009
|
4
|
+
Compatible with ruby 1.9.1.
|
5
|
+
The 'jungle_survival_kit' is now in its own 'backports' gem.
|
6
|
+
|
3
7
|
== Version 1.1 - December 17, 2008
|
4
8
|
Fixed bug when packing objects implementing to_ary
|
5
9
|
Added inheritance of shortcuts & filters to documentation
|
data/LICENSE
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# packable library
|
2
|
+
# Copyright (c) 2008, Marc-André Lafortune.
|
3
|
+
# All rights reserved.
|
4
|
+
# Licensed under the terms of the (modified) BSD License below:
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
# * Redistributions of source code must retain the above copyright
|
9
|
+
# notice, this list of conditions and the following disclaimer.
|
10
|
+
# * Redistributions in binary form must reproduce the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer in the
|
12
|
+
# documentation and/or other materials provided with the distribution.
|
13
|
+
# * Neither the name of the author nor the
|
14
|
+
# names of its contributors may be used to endorse or promote products
|
15
|
+
# derived from this software without specific prior written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY
|
18
|
+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
22
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
23
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
24
|
+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.rdoc
CHANGED
data/VERSION.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
---
|
2
|
-
patch:
|
3
|
-
major: 1
|
4
|
-
minor:
|
2
|
+
:patch: 0
|
3
|
+
:major: 1
|
4
|
+
:minor: 2
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'enumerator'
|
2
|
+
Enumerator = Enumerable::Enumerator unless defined?(Enumerator)
|
2
3
|
|
3
4
|
module Packable
|
4
5
|
module Extensions #:nodoc:
|
@@ -37,6 +38,7 @@ module Packable
|
|
37
38
|
# Returns (or yields) a modified IO object that will always pack/unpack when writing/reading.
|
38
39
|
def packed
|
39
40
|
packedio = clone
|
41
|
+
packedio.set_encoding("ascii-8bit") if packedio.respond_to? :set_encoding
|
40
42
|
class << packedio
|
41
43
|
def << (arg)
|
42
44
|
arg = [arg, :default] unless arg.instance_of?(::Array)
|
@@ -53,7 +55,7 @@ module Packable
|
|
53
55
|
|
54
56
|
def each_with_packing(*options, &block)
|
55
57
|
return each_without_packing(*options, &block) if (Integer === options.first) || (String === options.first)
|
56
|
-
return
|
58
|
+
return Enumerator.new(self, :each_with_packing, *options) unless block_given?
|
57
59
|
yield read(*options) until eof?
|
58
60
|
end
|
59
61
|
|
@@ -62,8 +64,8 @@ module Packable
|
|
62
64
|
end
|
63
65
|
|
64
66
|
def read_with_packing(*arg)
|
65
|
-
return read_without_packing(*arg) if (arg.length == 0) || arg.first.is_a?(Numeric)
|
66
|
-
|
67
|
+
return read_without_packing(*arg) if (arg.length == 0) || (arg.first.is_a?(Numeric) && (arg.length == 1))
|
68
|
+
values = Packable::Packers.to_class_option_list(*arg).map do |klass, options, original|
|
67
69
|
if eof?
|
68
70
|
raise EOFError, "End of IO when attempting to read #{klass} with options #{original.inspect}" if @throw_on_eof
|
69
71
|
nil
|
@@ -73,6 +75,7 @@ module Packable
|
|
73
75
|
klass.read_packed(self, options)
|
74
76
|
end
|
75
77
|
end
|
78
|
+
return values.size > 1 ? values : values.first
|
76
79
|
end
|
77
80
|
|
78
81
|
def pack_and_write(*arg)
|
data/lib/packable/packers.rb
CHANGED
@@ -53,6 +53,7 @@ module Packable
|
|
53
53
|
until arg.empty? do
|
54
54
|
k, options = original = arg.shift
|
55
55
|
k, options = global_lookup(k) if k.is_a? Symbol
|
56
|
+
raise TypeError, "Expected a class or symbol: #{k.inspect}" unless k.instance_of? Class
|
56
57
|
options ||= arg.first.is_a?(Hash) ? arg.shift.tap{|o| original = [original, o]} : :default
|
57
58
|
r << [k, k.packers.finalize(options), original]
|
58
59
|
end
|
data/lib/packable.rb
CHANGED
data/test/packing_doc_test.rb
CHANGED
data/test/packing_test.rb
CHANGED
@@ -50,13 +50,18 @@ class TestingPack < Test::Unit::TestCase
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def test_io
|
53
|
-
io = StringIO.new("\000\000\000\
|
54
|
-
n, s = io >> [Fixnum, {:signed=>false}] >> [String, {:bytes => 4}]
|
55
|
-
assert_equal
|
56
|
-
assert_equal
|
53
|
+
io = StringIO.new("\000\000\000\006abcdE!")
|
54
|
+
n, s, c = io >> [Fixnum, {:signed=>false}] >> [String, {:bytes => 4}] >> :char
|
55
|
+
assert_equal 6, n
|
56
|
+
assert_equal "abcd", s
|
57
|
+
assert_equal 69, c
|
57
58
|
assert_equal "!", io.read
|
58
59
|
end
|
59
60
|
|
61
|
+
should "do basic type checking" do
|
62
|
+
assert_raise(TypeError) {"".unpack(42, :short)}
|
63
|
+
end
|
64
|
+
|
60
65
|
context "Filters" do
|
61
66
|
context "for Object" do
|
62
67
|
Object.packers.set :generic_class_writer do |packer|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: marcandre-packable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Marc-Andr\xC3\xA9 Lafortune"
|
@@ -9,18 +9,28 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-04-02 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: marcandre-backports
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
16
25
|
description: If you need to do read and write binary data, there is of course <Array::pack and String::unpack The packable library makes (un)packing nicer, smarter and more powerful.
|
17
26
|
email: github@marc-andre.ca
|
18
27
|
executables: []
|
19
28
|
|
20
29
|
extensions: []
|
21
30
|
|
22
|
-
extra_rdoc_files:
|
23
|
-
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
- LICENSE
|
24
34
|
files:
|
25
35
|
- CHANGELOG.rdoc
|
26
36
|
- README.rdoc
|
@@ -34,13 +44,13 @@ files:
|
|
34
44
|
- lib/packable/extensions/object.rb
|
35
45
|
- lib/packable/extensions/proc.rb
|
36
46
|
- lib/packable/extensions/string.rb
|
37
|
-
- lib/packable/jungle_survival_kit.rb
|
38
47
|
- lib/packable/mixin.rb
|
39
48
|
- lib/packable/packers.rb
|
40
49
|
- lib/packable.rb
|
41
50
|
- test/packing_doc_test.rb
|
42
51
|
- test/packing_test.rb
|
43
52
|
- test/test_helper.rb
|
53
|
+
- LICENSE
|
44
54
|
has_rdoc: true
|
45
55
|
homepage: http://github.com/marcandre/packable
|
46
56
|
post_install_message:
|
@@ -51,6 +61,8 @@ rdoc_options:
|
|
51
61
|
- README.rdoc
|
52
62
|
- --line-numbers
|
53
63
|
- --inline-source
|
64
|
+
- --inline-source
|
65
|
+
- --charset=UTF-8
|
54
66
|
require_paths:
|
55
67
|
- lib
|
56
68
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -67,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
79
|
version:
|
68
80
|
requirements: []
|
69
81
|
|
70
|
-
rubyforge_project:
|
82
|
+
rubyforge_project: marcandre
|
71
83
|
rubygems_version: 1.2.0
|
72
84
|
signing_key:
|
73
85
|
specification_version: 2
|
@@ -1,72 +0,0 @@
|
|
1
|
-
# Insures that the following basic utilities (standard with ruby 1.9 and/or rails) are defined
|
2
|
-
# so we can get out of the uncivilized jungle (straight ruby 1.8) alive:
|
3
|
-
# - +require_relative+
|
4
|
-
# - +try+
|
5
|
-
# - +tap+
|
6
|
-
# - +alias_method_chain+
|
7
|
-
# - &:some_symbol
|
8
|
-
|
9
|
-
# Standard in ruby 1.9. Adapted from Pragmatic's "Programming Ruby" (since their version was buggy...)
|
10
|
-
module Kernel
|
11
|
-
def require_relative(relative_feature)
|
12
|
-
file = caller.first.split(/:\d/,2).first
|
13
|
-
if /\A\((.*)\)/ =~ file # eval, etc.
|
14
|
-
raise LoadError, "require_relative is called in #{$1}"
|
15
|
-
end
|
16
|
-
require File.expand_path(relative_feature, File.dirname(file))
|
17
|
-
end unless method_defined? :require_relative
|
18
|
-
end
|
19
|
-
|
20
|
-
class Object
|
21
|
-
# Standard in rails...
|
22
|
-
def try(method_id, *args, &block)
|
23
|
-
send(method_id, *args, &block) if respond_to?(method_id, true)
|
24
|
-
end unless method_defined? :try
|
25
|
-
|
26
|
-
# Standard in ruby 1.9
|
27
|
-
def tap
|
28
|
-
yield self
|
29
|
-
self
|
30
|
-
end unless method_defined? :tap
|
31
|
-
end
|
32
|
-
|
33
|
-
class Module
|
34
|
-
# Standard in rails...
|
35
|
-
def alias_method_chain(target, feature)
|
36
|
-
# Strip out punctuation on predicates or bang methods since
|
37
|
-
# e.g. target?_without_feature is not a valid method name.
|
38
|
-
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
|
39
|
-
yield(aliased_target, punctuation) if block_given?
|
40
|
-
|
41
|
-
with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
|
42
|
-
|
43
|
-
alias_method without_method, target
|
44
|
-
alias_method target, with_method
|
45
|
-
|
46
|
-
case
|
47
|
-
when public_method_defined?(without_method)
|
48
|
-
public target
|
49
|
-
when protected_method_defined?(without_method)
|
50
|
-
protected target
|
51
|
-
when private_method_defined?(without_method)
|
52
|
-
private target
|
53
|
-
end
|
54
|
-
end unless method_defined? :alias_method_chain
|
55
|
-
end
|
56
|
-
|
57
|
-
# Standard in ruby 1.9 & rails
|
58
|
-
unless :to_proc.respond_to?(:to_proc)
|
59
|
-
class Symbol
|
60
|
-
# Turns the symbol into a simple proc, which is especially useful for enumerations. Examples:
|
61
|
-
#
|
62
|
-
# # The same as people.collect { |p| p.name }
|
63
|
-
# people.collect(&:name)
|
64
|
-
#
|
65
|
-
# # The same as people.select { |p| p.manager? }.collect { |p| p.salary }
|
66
|
-
# people.select(&:manager?).collect(&:salary)
|
67
|
-
def to_proc
|
68
|
-
Proc.new { |*args| args.shift.__send__(self, *args) }
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|