active_record_serialize_json 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +5 -0
- data/Rakefile +2 -1
- data/VERSION +1 -1
- data/lib/active_record/serialize_json/version.rb +1 -1
- data/lib/active_record/serialize_json.rb +10 -1
- data/test/serialize_json_test.rb +16 -4
- metadata +16 -3
data/CHANGES
CHANGED
data/Rakefile
CHANGED
@@ -53,7 +53,8 @@ if defined? Gem
|
|
53
53
|
s.email = "flori@ping.de"
|
54
54
|
s.homepage = "http://flori.github.com/#{PKG_NAME}"
|
55
55
|
s.rubyforge_project = "#{PKG_NAME}"
|
56
|
-
s.add_dependency 'json',
|
56
|
+
s.add_dependency 'json', '~>1.4'
|
57
|
+
s.add_dependency 'activerecord', '~>2.3'
|
57
58
|
end
|
58
59
|
|
59
60
|
Rake::GemPackageTask.new(spec) do |pkg|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module SerializeJSON
|
3
3
|
# ActiveRecord::SerializeJSON version
|
4
|
-
VERSION = '0.0.
|
4
|
+
VERSION = '0.0.1'
|
5
5
|
VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
|
6
6
|
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
|
7
7
|
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_record/base'
|
1
2
|
require 'json'
|
2
3
|
|
3
4
|
module ActiveRecord
|
@@ -49,10 +50,18 @@ module ActiveRecord
|
|
49
50
|
before_save sj
|
50
51
|
after_save sj
|
51
52
|
|
53
|
+
unless respond_to?(:serialize_json_attributes)
|
54
|
+
cattr_accessor :serialize_json_attributes
|
55
|
+
self.serialize_json_attributes = {}
|
56
|
+
end
|
57
|
+
serialize_json_attributes[sj.attribute] = sj
|
58
|
+
|
52
59
|
class_eval do
|
53
60
|
define_method(:after_find) do
|
54
61
|
super if defined? super
|
55
|
-
|
62
|
+
self.class.serialize_json_attributes.each do |attribute, sj|
|
63
|
+
__send__(:"#{attribute}=", sj.deserialize(self))
|
64
|
+
end
|
56
65
|
end
|
57
66
|
end
|
58
67
|
end
|
data/test/serialize_json_test.rb
CHANGED
@@ -38,11 +38,15 @@ class SerializeJsonTest < Test::Unit::TestCase
|
|
38
38
|
|
39
39
|
class Foo < ActiveRecord::Base
|
40
40
|
serialize_json :bar
|
41
|
+
serialize_json :baz
|
41
42
|
end
|
42
43
|
|
43
44
|
def setup
|
44
45
|
ActiveRecord::Schema.define(:version => 1) do
|
45
|
-
create_table(:foos, :force => true)
|
46
|
+
create_table(:foos, :force => true) do |t|
|
47
|
+
t.string :bar
|
48
|
+
t.string :baz
|
49
|
+
end
|
46
50
|
end
|
47
51
|
end
|
48
52
|
|
@@ -52,26 +56,34 @@ class SerializeJsonTest < Test::Unit::TestCase
|
|
52
56
|
end
|
53
57
|
end
|
54
58
|
|
59
|
+
def test_class_methods
|
60
|
+
assert_equal [ :bar, :baz ], Foo.serialize_json_attributes.keys.sort_by(&:to_s)
|
61
|
+
assert_kind_of Method, ActiveRecord::Base.method(:serialize_json)
|
62
|
+
end
|
63
|
+
|
55
64
|
def test_hash
|
56
|
-
foo = Foo.new(:bar => { 'bar' => 'baz' })
|
65
|
+
foo = Foo.new(:bar => { 'bar' => 'baz' }, :baz => [ true ])
|
57
66
|
assert foo.save
|
58
67
|
foo_again = Foo.find(foo.id)
|
59
68
|
assert_kind_of Hash, foo.bar
|
60
69
|
assert_equal foo.bar.sort, foo_again.bar.sort
|
70
|
+
assert_equal true, foo_again.baz.first
|
61
71
|
end
|
62
72
|
|
63
73
|
def test_array
|
64
|
-
foo = Foo.new(:bar => [ 1, 2, 3 ])
|
74
|
+
foo = Foo.new(:bar => [ 1, 2, 3 ], :baz => [ true ])
|
65
75
|
assert foo.save
|
66
76
|
foo_again = Foo.find(foo.id)
|
67
77
|
assert_equal foo.bar, foo_again.bar
|
78
|
+
assert_equal true, foo_again.baz.first
|
68
79
|
end
|
69
80
|
|
70
81
|
def test_object
|
71
|
-
foo = Foo.new(:bar => Bar.new(23))
|
82
|
+
foo = Foo.new(:bar => Bar.new(23), :baz => [ true ])
|
72
83
|
assert foo.save
|
73
84
|
foo_again = Foo.find(foo.id)
|
74
85
|
assert_kind_of Bar, foo.bar
|
75
86
|
assert_equal foo.bar, foo_again.bar
|
87
|
+
assert_equal true, foo_again.baz.first
|
76
88
|
end
|
77
89
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Florian Frank
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-08-
|
17
|
+
date: 2010-08-24 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -30,6 +30,19 @@ dependencies:
|
|
30
30
|
version: "1.4"
|
31
31
|
type: :runtime
|
32
32
|
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: activerecord
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
segments:
|
41
|
+
- 2
|
42
|
+
- 3
|
43
|
+
version: "2.3"
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: *id002
|
33
46
|
description: Serialize an ActiveRecord::Base attribute via JSON in Ruby on Rails
|
34
47
|
email: flori@ping.de
|
35
48
|
executables: []
|