php-serialization 0.2.3 → 0.3.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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.3.0
@@ -1,17 +1,27 @@
1
1
  $:.unshift(File.dirname(__FILE__))
2
+ require 'php_serialization/serializer'
2
3
  require 'php_serialization/unserializer'
3
4
 
4
5
  module PhpSerialization
5
6
  class << self
7
+ def serializer
8
+ @serializer ||= Serializer.new
9
+ end
10
+
6
11
  def unserializer
7
12
  @unserializer ||= Unserializer.new
8
13
  end
9
14
 
10
15
  def load(str)
11
- unserializer.load(str)
16
+ unserializer.run(str)
17
+ end
18
+
19
+ def dump(object)
20
+ serializer.run(object)
12
21
  end
13
22
 
14
23
  alias :unserialize :load
15
24
  alias :restore :load
25
+ alias :serialize :dump
16
26
  end
17
27
  end
@@ -0,0 +1,31 @@
1
+ module PhpSerialization
2
+ class Serializer
3
+ def run(object)
4
+ case object
5
+ when NilClass then
6
+ "N;"
7
+ when TrueClass then
8
+ "b:1;"
9
+ when FalseClass then
10
+ "b:0;"
11
+ when Integer then
12
+ "i:#{object};"
13
+ when Float then
14
+ "d:#{object};"
15
+ when String then
16
+ "s:#{object.length}:\"#{object}\";"
17
+ when Array then
18
+ idx = -1
19
+ items = object.map { |item| "#{run(idx += 1)}#{run(item)}" }.join
20
+ "a:#{object.length}:{#{items}};"
21
+ when Hash then
22
+ items = object.map { |key,value| "#{run(key)}#{run(value)}" }.join
23
+ "a:#{object.length}:{#{items}};"
24
+ else
25
+ klass_name = object.class.name
26
+ attributes = object.instance_variables.map { |var_name| "#{run(var_name.gsub(/^@/, ''))}#{run(object.instance_variable_get(var_name))}" }
27
+ "O:#{klass_name.length}:\"#{klass_name}\":#{object.instance_variables.length}:{#{attributes}};"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -12,15 +12,11 @@ module PhpSerialization
12
12
  class Unserializer < Racc::Parser
13
13
 
14
14
  module_eval(<<'...end unserializer.y/module_eval...', 'unserializer.y', 69)
15
- def self.load(string)
16
- new.load(string)
17
- end
18
-
19
15
  def initialize(tokenizer_klass = Tokenizer)
20
16
  @tokenizer_klass = tokenizer_klass
21
17
  end
22
18
 
23
- def load(string)
19
+ def run(string)
24
20
  @tokenizer = @tokenizer_klass.new(string)
25
21
  yyparse(@tokenizer, :each)
26
22
  return @object
@@ -65,16 +65,12 @@ end
65
65
  ---- header ----
66
66
  require 'php_serialization/tokenizer'
67
67
 
68
- ---- inner ----
69
- def self.load(string)
70
- new.load(string)
71
- end
72
-
68
+ ---- inner ----
73
69
  def initialize(tokenizer_klass = Tokenizer)
74
70
  @tokenizer_klass = tokenizer_klass
75
71
  end
76
72
 
77
- def load(string)
73
+ def run(string)
78
74
  @tokenizer = @tokenizer_klass.new(string)
79
75
  yyparse(@tokenizer, :each)
80
76
  return @object
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{php-serialization}
8
- s.version = "0.2.3"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rodrigo Kochenburger"]
12
- s.date = %q{2009-10-02}
12
+ s.date = %q{2009-10-05}
13
13
  s.description = %q{Pure Ruby implementation of php's methods: serialize() and unserializer()}
14
14
  s.email = %q{divoxx@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -27,10 +27,12 @@ Gem::Specification.new do |s|
27
27
  "features/step_definitions/ruby_php_serialization_steps.rb",
28
28
  "features/support/env.rb",
29
29
  "lib/php_serialization.rb",
30
+ "lib/php_serialization/serializer.rb",
30
31
  "lib/php_serialization/tokenizer.rb",
31
32
  "lib/php_serialization/unserializer.rb",
32
33
  "lib/php_serialization/unserializer.y",
33
34
  "php-serialization.gemspec",
35
+ "spec/serialization_spec.rb",
34
36
  "spec/spec.opts",
35
37
  "spec/spec_helper.rb",
36
38
  "spec/unserialization_spec.rb"
@@ -41,7 +43,8 @@ Gem::Specification.new do |s|
41
43
  s.rubygems_version = %q{1.3.4}
42
44
  s.summary = %q{PHP's serialization implementation for ruby}
43
45
  s.test_files = [
44
- "spec/spec_helper.rb",
46
+ "spec/serialization_spec.rb",
47
+ "spec/spec_helper.rb",
45
48
  "spec/unserialization_spec.rb"
46
49
  ]
47
50
 
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe "Serialization" do
4
+ it "should serialize a integer" do
5
+ PhpSerialization.dump(10).should == "i:10;"
6
+ end
7
+
8
+ it "should serialize a string" do
9
+ PhpSerialization.dump("Name").should == 's:4:"Name";'
10
+ end
11
+
12
+ it "should serialize true" do
13
+ PhpSerialization.dump(true).should == 'b:1;'
14
+ end
15
+
16
+ it "should serialize false" do
17
+ PhpSerialization.dump(false).should == 'b:0;'
18
+ end
19
+
20
+ it "should serialize nil" do
21
+ PhpSerialization.dump(nil).should == 'N;'
22
+ end
23
+
24
+ it "should unzerialize an array" do
25
+ PhpSerialization.dump([true, "foo"]).should == 'a:2:{i:0;b:1;i:1;s:3:"foo";};'
26
+ end
27
+
28
+ it "should serialize a hash" do
29
+ PhpSerialization.dump("name" => "Rodrigo", "age" => 23).should == 'a:2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};'
30
+ end
31
+
32
+ it "should serialize object with class existant" do
33
+ class Person
34
+ attr_accessor :name, :age
35
+ end
36
+
37
+ person = Person.new
38
+ person.name = "Rodrigo"
39
+ person.age = 23
40
+
41
+ PhpSerialization.dump(person).should == 'O:6:"Person":2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};'
42
+
43
+ Object.send(:remove_const, :Person)
44
+ end
45
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: php-serialization
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Kochenburger
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-02 00:00:00 -03:00
12
+ date: 2009-10-05 00:00:00 -03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -62,10 +62,12 @@ files:
62
62
  - features/step_definitions/ruby_php_serialization_steps.rb
63
63
  - features/support/env.rb
64
64
  - lib/php_serialization.rb
65
+ - lib/php_serialization/serializer.rb
65
66
  - lib/php_serialization/tokenizer.rb
66
67
  - lib/php_serialization/unserializer.rb
67
68
  - lib/php_serialization/unserializer.y
68
69
  - php-serialization.gemspec
70
+ - spec/serialization_spec.rb
69
71
  - spec/spec.opts
70
72
  - spec/spec_helper.rb
71
73
  - spec/unserialization_spec.rb
@@ -98,5 +100,6 @@ signing_key:
98
100
  specification_version: 3
99
101
  summary: PHP's serialization implementation for ruby
100
102
  test_files:
103
+ - spec/serialization_spec.rb
101
104
  - spec/spec_helper.rb
102
105
  - spec/unserialization_spec.rb