serialize-rails 0.0.1
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/MIT-LICENSE +20 -0
- data/README.rdoc +24 -0
- data/Rakefile +38 -0
- data/lib/serialize-rails.rb +5 -0
- data/lib/serialize-rails/coders/gzip_column.rb +42 -0
- data/lib/serialize-rails/coders/json_column.rb +43 -0
- data/lib/serialize-rails/coders/marshal_column.rb +41 -0
- data/lib/serialize-rails/coders/xml_column.rb +47 -0
- data/lib/serialize-rails/serialization.rb +69 -0
- data/lib/serialize-rails/version.rb +3 -0
- data/test/active_record_helper.rb +12 -0
- data/test/gzip_column_test.rb +29 -0
- data/test/json_column_test.rb +48 -0
- data/test/marshal_column_test.rb +48 -0
- data/test/schema.rb +25 -0
- data/test/serialization_test.rb +35 -0
- data/test/test_helper.rb +12 -0
- data/test/xml_column_test.rb +51 -0
- metadata +136 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 Jan Berdajs
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
= Serialize for Rails
|
2
|
+
|
3
|
+
Rails already supports serialization of attributes, but it can only do Yaml.
|
4
|
+
This gem upgrades the default Rails serialize method so you can serialize into
|
5
|
+
yaml, json, xml and with marshal (Ruby binary format). It also supports gzipping
|
6
|
+
the output if you want it to take up less space.
|
7
|
+
|
8
|
+
Format can be one of the following:
|
9
|
+
|
10
|
+
:yaml, :json, :xml, :marshal
|
11
|
+
|
12
|
+
Yaml is the default, and serialize will behave just like normal if you don't use
|
13
|
+
any of the new options.
|
14
|
+
|
15
|
+
Example usage:
|
16
|
+
|
17
|
+
class Mouse < ActiveRecord::Base
|
18
|
+
serialize :info, Hash, :format => :json
|
19
|
+
serialize :data, :format => :xml, :gzip => true
|
20
|
+
end
|
21
|
+
|
22
|
+
When you use gzip you __must__ use a binary-compatible column type such
|
23
|
+
as `BLOB` (MySQL) or `bytea` (PostgreSQL). If you don't use gzip, `text` is
|
24
|
+
recommended to allow strings of arbitrary length.
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'SerializeRails'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
|
30
|
+
Rake::TestTask.new(:test) do |t|
|
31
|
+
t.libs << 'lib'
|
32
|
+
t.libs << 'test'
|
33
|
+
t.pattern = 'test/**/*_test.rb'
|
34
|
+
t.verbose = false
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
task :default => :test
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'zlib'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Coders # :nodoc:
|
5
|
+
class GzipColumn # :nodoc:
|
6
|
+
RESCUE_ERRORS = [ ArgumentError ]
|
7
|
+
|
8
|
+
attr_accessor :wrapped_coder
|
9
|
+
|
10
|
+
def initialize(wrap_coder)
|
11
|
+
@wrapped_coder = wrap_coder
|
12
|
+
end
|
13
|
+
|
14
|
+
def object_class
|
15
|
+
wrapped_coder.object_class
|
16
|
+
end
|
17
|
+
|
18
|
+
def object_class=(value)
|
19
|
+
wrapped_coder.object_class = value
|
20
|
+
end
|
21
|
+
|
22
|
+
def dump(obj)
|
23
|
+
obj = wrapped_coder.dump(obj)
|
24
|
+
return if obj.nil?
|
25
|
+
return obj unless obj.is_a?(String)
|
26
|
+
|
27
|
+
Zlib::Deflate.deflate obj
|
28
|
+
end
|
29
|
+
|
30
|
+
def load(data)
|
31
|
+
return object_class.new if object_class != Object && data.nil?
|
32
|
+
return data unless data.is_a?(String)
|
33
|
+
begin
|
34
|
+
ungzipped_data = Zlib::Inflate.inflate(data)
|
35
|
+
wrapped_coder.load(ungzipped_data)
|
36
|
+
rescue *RESCUE_ERRORS
|
37
|
+
data
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Coders # :nodoc:
|
5
|
+
class JSONColumn # :nodoc:
|
6
|
+
RESCUE_ERRORS = [ ArgumentError, JSON::JSONError ]
|
7
|
+
|
8
|
+
attr_accessor :object_class
|
9
|
+
|
10
|
+
def initialize(object_class = Object)
|
11
|
+
@object_class = object_class
|
12
|
+
end
|
13
|
+
|
14
|
+
def dump(obj)
|
15
|
+
return if obj.nil?
|
16
|
+
|
17
|
+
unless obj.is_a?(object_class)
|
18
|
+
raise SerializationTypeMismatch,
|
19
|
+
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
|
20
|
+
end
|
21
|
+
JSON.dump obj
|
22
|
+
end
|
23
|
+
|
24
|
+
def load(json)
|
25
|
+
return object_class.new if object_class != Object && json.nil?
|
26
|
+
return json unless json.is_a?(String)
|
27
|
+
begin
|
28
|
+
obj = JSON.load(json)
|
29
|
+
|
30
|
+
unless obj.is_a?(object_class) || obj.nil?
|
31
|
+
raise SerializationTypeMismatch,
|
32
|
+
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
|
33
|
+
end
|
34
|
+
obj ||= object_class.new if object_class != Object
|
35
|
+
|
36
|
+
obj
|
37
|
+
rescue *RESCUE_ERRORS
|
38
|
+
json
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Coders # :nodoc:
|
3
|
+
class MarshalColumn # :nodoc:
|
4
|
+
RESCUE_ERRORS = [ ArgumentError, TypeError ]
|
5
|
+
|
6
|
+
attr_accessor :object_class
|
7
|
+
|
8
|
+
def initialize(object_class = Object)
|
9
|
+
@object_class = object_class
|
10
|
+
end
|
11
|
+
|
12
|
+
def dump(obj)
|
13
|
+
return if obj.nil?
|
14
|
+
|
15
|
+
unless obj.is_a?(object_class)
|
16
|
+
raise SerializationTypeMismatch,
|
17
|
+
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
|
18
|
+
end
|
19
|
+
Marshal.dump obj
|
20
|
+
end
|
21
|
+
|
22
|
+
def load(data)
|
23
|
+
return object_class.new if object_class != Object && data.nil?
|
24
|
+
return data unless data.is_a?(String)
|
25
|
+
begin
|
26
|
+
obj = Marshal.load(data)
|
27
|
+
|
28
|
+
unless obj.is_a?(object_class) || obj.nil?
|
29
|
+
raise SerializationTypeMismatch,
|
30
|
+
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
|
31
|
+
end
|
32
|
+
obj ||= object_class.new if object_class != Object
|
33
|
+
|
34
|
+
obj
|
35
|
+
rescue *RESCUE_ERRORS
|
36
|
+
data
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'ox'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Coders # :nodoc:
|
5
|
+
class XMLColumn # :nodoc:
|
6
|
+
RESCUE_ERRORS = [ ArgumentError, SyntaxError ]
|
7
|
+
|
8
|
+
attr_accessor :object_class, :options
|
9
|
+
|
10
|
+
def initialize(object_class = Object, options = nil)
|
11
|
+
@object_class = object_class
|
12
|
+
@options = options || Hash.new
|
13
|
+
default_options = { :load => Hash.new, :dump => Hash.new }
|
14
|
+
@options = default_options.merge(@options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def dump(obj)
|
18
|
+
return if obj.nil?
|
19
|
+
|
20
|
+
unless obj.is_a?(object_class)
|
21
|
+
raise SerializationTypeMismatch,
|
22
|
+
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
|
23
|
+
end
|
24
|
+
Ox.dump(obj, options[:dump])
|
25
|
+
end
|
26
|
+
|
27
|
+
def load(xml)
|
28
|
+
return object_class.new if object_class != Object && xml.nil?
|
29
|
+
return xml unless xml.is_a?(String)
|
30
|
+
begin
|
31
|
+
default_load_opts = { :mode => :object }
|
32
|
+
obj = Ox.load(xml, default_load_opts.merge(options[:load]))
|
33
|
+
|
34
|
+
unless obj.is_a?(object_class) || obj.nil?
|
35
|
+
raise SerializationTypeMismatch,
|
36
|
+
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
|
37
|
+
end
|
38
|
+
obj ||= object_class.new if object_class != Object
|
39
|
+
|
40
|
+
obj
|
41
|
+
rescue *RESCUE_ERRORS
|
42
|
+
xml
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# Must trigger autoloading of the class
|
2
|
+
if ActiveRecord::AttributeMethods::Serialization.nil?
|
3
|
+
raise "ActiveRecord not loaded"
|
4
|
+
end
|
5
|
+
|
6
|
+
module ActiveRecord
|
7
|
+
module AttributeMethods
|
8
|
+
module Serialization
|
9
|
+
module ClassMethods
|
10
|
+
# If you have an attribute that needs to be saved to the database as an
|
11
|
+
# object, and retrieved as the same object, then specify the name of that
|
12
|
+
# attribute using this method and it will be handled automatically. The
|
13
|
+
# serialization is done through YAML. If +class_name+ is specified, the
|
14
|
+
# serialized object must be of that class on retrieval or
|
15
|
+
# <tt>SerializationTypeMismatch</tt> will be raised.
|
16
|
+
#
|
17
|
+
# ==== Parameters
|
18
|
+
#
|
19
|
+
# * +attr_name+ - The field name that should be serialized.
|
20
|
+
# * +class_name+ - Optional, class name that the object type should be equal to.
|
21
|
+
#
|
22
|
+
# ==== Example
|
23
|
+
#
|
24
|
+
# # Serialize a preferences attribute.
|
25
|
+
# class User < ActiveRecord::Base
|
26
|
+
# serialize :preferences
|
27
|
+
# end
|
28
|
+
def serialize(attr_name, *args)
|
29
|
+
include Behavior if defined? Behavior # Rails 4
|
30
|
+
|
31
|
+
options = args.shift
|
32
|
+
if options.is_a? Hash
|
33
|
+
class_name = Object
|
34
|
+
else
|
35
|
+
class_name = options || Object
|
36
|
+
options = args.shift || Hash.new
|
37
|
+
end
|
38
|
+
|
39
|
+
default_options = {
|
40
|
+
:format => :yaml,
|
41
|
+
:gzip => false
|
42
|
+
}
|
43
|
+
options = default_options.merge(options)
|
44
|
+
|
45
|
+
coder = if [:load, :dump].all? { |x| class_name.respond_to?(x) }
|
46
|
+
class_name
|
47
|
+
else
|
48
|
+
case options[:format]
|
49
|
+
when :json
|
50
|
+
Coders::JSONColumn.new(class_name)
|
51
|
+
when :marshal
|
52
|
+
Coders::MarshalColumn.new(class_name)
|
53
|
+
when :xml
|
54
|
+
Coders::XMLColumn.new(class_name, options[:ox_options])
|
55
|
+
else
|
56
|
+
Coders::YAMLColumn.new(class_name)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
coder = Coders::GzipColumn.new(coder) if options[:gzip]
|
61
|
+
|
62
|
+
# merge new serialized attribute and create new hash to ensure that each class in inheritance hierarchy
|
63
|
+
# has its own hash of own serialized attributes
|
64
|
+
self.serialized_attributes = serialized_attributes.merge(attr_name.to_s => coder)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
2
|
+
ActiveRecord::Migration.verbose = false
|
3
|
+
load "schema.rb"
|
4
|
+
|
5
|
+
def get_raw_info
|
6
|
+
raw_data = ActiveRecord::Base.connection.select_all("SELECT info FROM mice")
|
7
|
+
raw_data.first["info"]
|
8
|
+
end
|
9
|
+
|
10
|
+
def clean_db
|
11
|
+
ActiveRecord::Base.connection.select_all("DELETE FROM mice")
|
12
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'serialize-rails/coders/gzip_column'
|
3
|
+
require 'serialize-rails/coders/json_column'
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module Coders
|
7
|
+
class JSONColumnTest < ActiveSupport::TestCase
|
8
|
+
def test_initialize_takes_class
|
9
|
+
coder = GzipColumn.new(JSONColumn.new(Object))
|
10
|
+
assert_equal Object, coder.object_class
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_gzip_works
|
14
|
+
json_coder = JSONColumn.new(Object)
|
15
|
+
coder = GzipColumn.new(json_coder)
|
16
|
+
|
17
|
+
test_array = ["hello"] * 5
|
18
|
+
json_output = json_coder.dump(test_array)
|
19
|
+
gzipped_output = coder.dump(test_array)
|
20
|
+
assert_operator json_output.length, :>, gzipped_output.length
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_dump_and_load
|
24
|
+
coder = GzipColumn.new(JSONColumn.new(Object))
|
25
|
+
assert_equal([1, "x"], coder.load(coder.dump([1, "x"])))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'serialize-rails/coders/json_column'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module Coders
|
6
|
+
class JSONColumnTest < ActiveSupport::TestCase
|
7
|
+
def test_initialize_takes_class
|
8
|
+
coder = JSONColumn.new(Object)
|
9
|
+
assert_equal Object, coder.object_class
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_type_mismatch_on_different_classes_on_dump
|
13
|
+
coder = JSONColumn.new(Array)
|
14
|
+
assert_raises(SerializationTypeMismatch) do
|
15
|
+
coder.dump("a")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_type_mismatch_on_different_classes
|
20
|
+
coder = JSONColumn.new(Array)
|
21
|
+
assert_raises(SerializationTypeMismatch) do
|
22
|
+
coder.load "{}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_nil_is_ok
|
27
|
+
coder = JSONColumn.new
|
28
|
+
assert_nil coder.load "null"
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_returns_new_with_different_class
|
32
|
+
coder = JSONColumn.new SerializationTypeMismatch
|
33
|
+
assert_equal SerializationTypeMismatch, coder.load("null").class
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_load_handles_other_classes
|
37
|
+
coder = JSONColumn.new
|
38
|
+
assert_equal [], coder.load([])
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_load_swallows_json_exceptions
|
42
|
+
coder = JSONColumn.new
|
43
|
+
bad_json = 'x'
|
44
|
+
assert_equal bad_json, coder.load(bad_json)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'serialize-rails/coders/marshal_column'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module Coders
|
6
|
+
class MarshalColumnTest < ActiveSupport::TestCase
|
7
|
+
def test_initialize_takes_class
|
8
|
+
coder = MarshalColumn.new(Object)
|
9
|
+
assert_equal Object, coder.object_class
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_type_mismatch_on_different_classes_on_dump
|
13
|
+
coder = MarshalColumn.new(Array)
|
14
|
+
assert_raises(SerializationTypeMismatch) do
|
15
|
+
coder.dump("a")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_type_mismatch_on_different_classes
|
20
|
+
coder = MarshalColumn.new(Array)
|
21
|
+
assert_raises(SerializationTypeMismatch) do
|
22
|
+
coder.load Marshal.dump(Hash.new)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_nil_is_ok
|
27
|
+
coder = MarshalColumn.new
|
28
|
+
assert_nil coder.load Marshal.dump(nil)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_returns_new_with_different_class
|
32
|
+
coder = MarshalColumn.new SerializationTypeMismatch
|
33
|
+
assert_equal SerializationTypeMismatch, coder.load(Marshal.dump(nil)).class
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_load_handles_other_classes
|
37
|
+
coder = MarshalColumn.new
|
38
|
+
assert_equal [], coder.load([])
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_load_swallows_marshal_exceptions
|
42
|
+
coder = MarshalColumn.new
|
43
|
+
bad_marshal = '{}'
|
44
|
+
assert_equal bad_marshal, coder.load(bad_marshal)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/test/schema.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :mice, :force => true do |t|
|
3
|
+
t.text :info
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class MouseDefaults < ActiveRecord::Base
|
8
|
+
self.table_name = "mice"
|
9
|
+
serialize :info
|
10
|
+
end
|
11
|
+
|
12
|
+
class MouseDefaultsArray < ActiveRecord::Base
|
13
|
+
self.table_name = "mice"
|
14
|
+
serialize :info, Array
|
15
|
+
end
|
16
|
+
|
17
|
+
class MouseJsonArray < ActiveRecord::Base
|
18
|
+
self.table_name = "mice"
|
19
|
+
serialize :info, Array, :format => :json
|
20
|
+
end
|
21
|
+
|
22
|
+
class MouseJsonArrayGzip < ActiveRecord::Base
|
23
|
+
self.table_name = "mice"
|
24
|
+
serialize :info, Array, :format => :json, :gzip => true
|
25
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'active_record_helper'
|
3
|
+
require 'serialize-rails/coders/gzip_column'
|
4
|
+
require 'serialize-rails/coders/json_column'
|
5
|
+
require 'serialize-rails/serialization'
|
6
|
+
|
7
|
+
# mostly already tested in AR tests, and did not change much
|
8
|
+
class SerializationTest < ActiveSupport::TestCase
|
9
|
+
def teardown
|
10
|
+
clean_db
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_defaults_to_yaml
|
14
|
+
MouseDefaults.create! :info => { 1 => "x" }
|
15
|
+
assert_equal("---\n1: x\n", get_raw_info)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_yaml_array
|
19
|
+
assert_raises(ActiveRecord::SerializationTypeMismatch) do
|
20
|
+
MouseDefaultsArray.create! :info => { 1 => "x" }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_json_array
|
25
|
+
MouseJsonArray.create! :info => Array.new
|
26
|
+
assert_equal("[]", get_raw_info.gsub(/\s+/, ""))
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_json_array_gzip
|
30
|
+
test_array = ["hello"] * 5
|
31
|
+
MouseJsonArrayGzip.create! :info => test_array
|
32
|
+
json_for_array = ActiveRecord::Coders::JSONColumn.new(Array).dump(test_array)
|
33
|
+
assert_operator get_raw_info.length, :<, json_for_array.length
|
34
|
+
end
|
35
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'active_support/all'
|
6
|
+
require 'active_record'
|
7
|
+
require 'serialize-rails'
|
8
|
+
require 'active_record/errors'
|
9
|
+
# require 'mocha'
|
10
|
+
|
11
|
+
# Load support files
|
12
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'serialize-rails/coders/xml_column'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module Coders
|
6
|
+
class XMLColumnTest < ActiveSupport::TestCase
|
7
|
+
XML_NIL = Ox.dump(nil)
|
8
|
+
XML_EMPTY_HASH = Ox.dump({})
|
9
|
+
|
10
|
+
def test_initialize_takes_class
|
11
|
+
coder = XMLColumn.new(Object)
|
12
|
+
assert_equal Object, coder.object_class
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_type_mismatch_on_different_classes_on_dump
|
16
|
+
coder = XMLColumn.new(Array)
|
17
|
+
assert_raises(SerializationTypeMismatch) do
|
18
|
+
coder.dump("a")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_type_mismatch_on_different_classes
|
23
|
+
coder = XMLColumn.new(Array)
|
24
|
+
assert_raises(SerializationTypeMismatch) do
|
25
|
+
coder.load XML_EMPTY_HASH
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_nil_is_ok
|
30
|
+
coder = XMLColumn.new
|
31
|
+
assert_nil coder.load XML_NIL
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_returns_new_with_different_class
|
35
|
+
coder = XMLColumn.new SerializationTypeMismatch
|
36
|
+
assert_equal SerializationTypeMismatch, coder.load(XML_NIL).class
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_load_handles_other_classes
|
40
|
+
coder = XMLColumn.new
|
41
|
+
assert_equal [], coder.load([])
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_load_swallows_xml_exceptions
|
45
|
+
coder = XMLColumn.new
|
46
|
+
bad_xml = 'x'
|
47
|
+
assert_equal bad_xml, coder.load(bad_xml)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serialize-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jan Berdajs
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.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: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: ox
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sqlite3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: pry
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description:
|
79
|
+
email:
|
80
|
+
- mrbrdo@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- lib/serialize-rails/coders/gzip_column.rb
|
86
|
+
- lib/serialize-rails/coders/json_column.rb
|
87
|
+
- lib/serialize-rails/coders/marshal_column.rb
|
88
|
+
- lib/serialize-rails/coders/xml_column.rb
|
89
|
+
- lib/serialize-rails/serialization.rb
|
90
|
+
- lib/serialize-rails/version.rb
|
91
|
+
- lib/serialize-rails.rb
|
92
|
+
- MIT-LICENSE
|
93
|
+
- Rakefile
|
94
|
+
- README.rdoc
|
95
|
+
- test/active_record_helper.rb
|
96
|
+
- test/gzip_column_test.rb
|
97
|
+
- test/json_column_test.rb
|
98
|
+
- test/marshal_column_test.rb
|
99
|
+
- test/schema.rb
|
100
|
+
- test/serialization_test.rb
|
101
|
+
- test/test_helper.rb
|
102
|
+
- test/xml_column_test.rb
|
103
|
+
homepage: https://github.com/mrbrdo/serialize-rails
|
104
|
+
licenses: []
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.24
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: Rails attribute serialization into yaml, json, xml and with ruby marshal.
|
127
|
+
test_files:
|
128
|
+
- test/active_record_helper.rb
|
129
|
+
- test/gzip_column_test.rb
|
130
|
+
- test/json_column_test.rb
|
131
|
+
- test/marshal_column_test.rb
|
132
|
+
- test/schema.rb
|
133
|
+
- test/serialization_test.rb
|
134
|
+
- test/test_helper.rb
|
135
|
+
- test/xml_column_test.rb
|
136
|
+
has_rdoc:
|