active_record_serialize_json 0.0.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/CHANGES ADDED
File without changes
data/README ADDED
@@ -0,0 +1,25 @@
1
+ == Description
2
+
3
+ == Download
4
+
5
+ The latest version of the <b>active_record_serialize_json</b> source archive can be
6
+ found at
7
+
8
+ * http://www.ping.de/~flori
9
+
10
+ == Installation
11
+
12
+ You can use rubygems to fetch the gem and install it for you:
13
+
14
+ # gem install active_record_serialize_json
15
+
16
+ You can also put this line into your Rails environment.rb file
17
+
18
+ config.gem 'active_record_serialize_json'
19
+
20
+ and install the gem via
21
+
22
+ $ rake gems:install
23
+
24
+ == Usage
25
+
data/Rakefile ADDED
@@ -0,0 +1,86 @@
1
+ begin
2
+ require 'rake/gempackagetask'
3
+ rescue LoadError
4
+ end
5
+ require 'rake/clean'
6
+ require 'rbconfig'
7
+ include Config
8
+
9
+ PKG_NAME = 'active_record_serialize_json'
10
+ PKG_VERSION = File.read('VERSION').chomp
11
+ PKG_FILES = FileList['**/*'].exclude(/^(doc|CVS|pkg|coverage)/)
12
+ PKG_SUMMARY = 'Serialize an ActiveRecord::Base attribute via JSON'
13
+ PKG_RDOC_OPTIONS = [ '--main', 'README', '--title', PKG_SUMMARY ]
14
+ CLEAN.include 'coverage', 'doc'
15
+
16
+ desc "Testing library"
17
+ task :test do
18
+ ruby %{-Ilib test/serialize_json_test.rb}
19
+ end
20
+
21
+ desc "Testing library (with coverage)"
22
+ task :coverage do
23
+ sh %{rcov -Ilib test/serialize_json_test.rb}
24
+ end
25
+
26
+ desc "Installing library"
27
+ task :install do
28
+ ruby 'install.rb'
29
+ end
30
+
31
+ desc "Creating documentation"
32
+ task :doc do
33
+ sh 'rdoc', *(PKG_RDOC_OPTIONS + Dir['lib/**/*.rb'] + [ 'README' ])
34
+ end
35
+
36
+ if defined? Gem
37
+ spec = Gem::Specification.new do |s|
38
+ s.name = PKG_NAME
39
+ s.version = PKG_VERSION
40
+ s.summary = PKG_SUMMARY
41
+ s.description = PKG_SUMMARY + ' in Ruby on Rails'
42
+
43
+ s.files = PKG_FILES.to_a.sort
44
+
45
+ s.require_path = 'lib'
46
+
47
+ s.has_rdoc = true
48
+ s.rdoc_options = PKG_RDOC_OPTIONS
49
+ s.extra_rdoc_files << 'README'
50
+ #s.test_files << 'test/serialize_json_test.rb'
51
+
52
+ s.author = "Florian Frank"
53
+ s.email = "flori@ping.de"
54
+ s.homepage = "http://flori.github.com/#{PKG_NAME}"
55
+ s.rubyforge_project = "#{PKG_NAME}"
56
+ s.add_dependency 'json', '~>1.4'
57
+ end
58
+
59
+ Rake::GemPackageTask.new(spec) do |pkg|
60
+ pkg.need_tar = true
61
+ pkg.package_files += PKG_FILES
62
+ end
63
+ end
64
+
65
+ desc m = "Writing version information for #{PKG_VERSION}"
66
+ task :version do
67
+ puts m
68
+ File.open(File.join('lib', 'active_record', 'serialize_json', 'version.rb'), 'w') do |v|
69
+ v.puts <<EOT
70
+ module ActiveRecord
71
+ module SerializeJSON
72
+ # ActiveRecord::SerializeJSON version
73
+ VERSION = '#{PKG_VERSION}'
74
+ VERSION_ARRAY = VERSION.split(/\\./).map { |x| x.to_i } # :nodoc:
75
+ VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
76
+ VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
77
+ VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
78
+ end
79
+ end
80
+ EOT
81
+ end
82
+ end
83
+
84
+ task :default => [ :version, :test ]
85
+
86
+ task :release => [ :clean, :version, :package ]
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,10 @@
1
+ module ActiveRecord
2
+ module SerializeJSON
3
+ # ActiveRecord::SerializeJSON version
4
+ VERSION = '0.0.0'
5
+ VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
6
+ VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
7
+ VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
8
+ VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
9
+ end
10
+ end
@@ -0,0 +1,60 @@
1
+ require 'json'
2
+
3
+ module ActiveRecord
4
+ class SerializeJSON
5
+ def initialize(attribute, opts = {})
6
+ @attribute = attribute.to_s.to_sym
7
+ @serialize = opts[:serialize] || {}
8
+ @deserialize = opts[:deserialize] || {}
9
+ end
10
+
11
+ attr_reader :attribute
12
+
13
+ def before_save(record)
14
+ json = serialize record
15
+ record.__send__(:"#{@attribute}=", json)
16
+ end
17
+
18
+ def after_save(record)
19
+ data = deserialize record
20
+ record.__send__(:"#{@attribute}=", data)
21
+ end
22
+
23
+ def serialize(record)
24
+ self.class.serialize(record.__send__(@attribute), @serialize)
25
+ end
26
+
27
+ def deserialize(record)
28
+ self.class.deserialize(record.__send__(@attribute), @deserialize)
29
+ end
30
+
31
+ def self.serialize(value, opts = {})
32
+ opts ||= {}
33
+ JSON.generate(value, opts)
34
+ end
35
+
36
+ def self.deserialize(value, opts = {})
37
+ opts ||= {}
38
+ JSON.parse(value, opts)
39
+ rescue => e
40
+ Rails.logger.warn e
41
+ value
42
+ end
43
+ end
44
+
45
+ class Base
46
+ def self.serialize_json(attribute, opts = {})
47
+ sj = SerializeJSON.new(attribute, opts)
48
+
49
+ before_save sj
50
+ after_save sj
51
+
52
+ class_eval do
53
+ define_method(:after_find) do
54
+ super if defined? super
55
+ __send__(:"#{sj.attribute}=", sj.deserialize(self))
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1 @@
1
+ require 'active_record/serialize_json'
@@ -0,0 +1,77 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+
4
+ require 'active_record'
5
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
6
+ require 'active_record/serialize_json'
7
+
8
+ ActiveRecord::Base.establish_connection(
9
+ :adapter => "mysql",
10
+ :database => ENV['DATABASE'] || "test",
11
+ :username => ENV['USER'],
12
+ :password => ENV['PASSWORD']
13
+ )
14
+
15
+ class SerializeJsonTest < Test::Unit::TestCase
16
+ class Bar
17
+ def initialize(bar)
18
+ @bar = bar
19
+ end
20
+
21
+ attr_reader :bar
22
+
23
+ def self.json_create(data)
24
+ new(data['bar'])
25
+ end
26
+
27
+ def to_json(*a)
28
+ {
29
+ :bar => bar,
30
+ JSON.create_id => self.class.name,
31
+ }.to_json(*a)
32
+ end
33
+
34
+ def ==(other)
35
+ bar == other.bar
36
+ end
37
+ end
38
+
39
+ class Foo < ActiveRecord::Base
40
+ serialize_json :bar
41
+ end
42
+
43
+ def setup
44
+ ActiveRecord::Schema.define(:version => 1) do
45
+ create_table(:foos, :force => true) { |t| t.string :bar }
46
+ end
47
+ end
48
+
49
+ def teardown
50
+ ActiveRecord::Base.connection.tables.each do |table|
51
+ ActiveRecord::Base.connection.drop_table(table)
52
+ end
53
+ end
54
+
55
+ def test_hash
56
+ foo = Foo.new(:bar => { 'bar' => 'baz' })
57
+ assert foo.save
58
+ foo_again = Foo.find(foo.id)
59
+ assert_kind_of Hash, foo.bar
60
+ assert_equal foo.bar.sort, foo_again.bar.sort
61
+ end
62
+
63
+ def test_array
64
+ foo = Foo.new(:bar => [ 1, 2, 3 ])
65
+ assert foo.save
66
+ foo_again = Foo.find(foo.id)
67
+ assert_equal foo.bar, foo_again.bar
68
+ end
69
+
70
+ def test_object
71
+ foo = Foo.new(:bar => Bar.new(23))
72
+ assert foo.save
73
+ foo_again = Foo.find(foo.id)
74
+ assert_kind_of Bar, foo.bar
75
+ assert_equal foo.bar, foo_again.bar
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_serialize_json
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ version: 0.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Florian Frank
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-19 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: json
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 4
30
+ version: "1.4"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: Serialize an ActiveRecord::Base attribute via JSON in Ruby on Rails
34
+ email: flori@ping.de
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - README
41
+ files:
42
+ - CHANGES
43
+ - README
44
+ - Rakefile
45
+ - VERSION
46
+ - lib/active_record/serialize_json.rb
47
+ - lib/active_record/serialize_json/version.rb
48
+ - lib/active_record_serialize_json.rb
49
+ - test/serialize_json_test.rb
50
+ has_rdoc: true
51
+ homepage: http://flori.github.com/active_record_serialize_json
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --main
57
+ - README
58
+ - --title
59
+ - Serialize an ActiveRecord::Base attribute via JSON
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project: active_record_serialize_json
79
+ rubygems_version: 1.3.6
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Serialize an ActiveRecord::Base attribute via JSON
83
+ test_files: []
84
+