json_serialize 1.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/.document +5 -0
- data/.gitignore +25 -0
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +51 -0
- data/LICENSE +20 -0
- data/README.textile +33 -0
- data/Rakefile +36 -0
- data/VERSION +1 -0
- data/json_serialize.gemspec +58 -0
- data/lib/json_serialize.rb +28 -0
- data/spec/json_serialize_spec.rb +44 -0
- data/spec/spec_helper.rb +10 -0
- metadata +92 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
## MAC OS
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## PROJECT::GENERAL
|
17
|
+
coverage
|
18
|
+
rdoc
|
19
|
+
pkg
|
20
|
+
.bundle
|
21
|
+
.rvmrc
|
22
|
+
|
23
|
+
## PROJECT::DOCUMENTATION
|
24
|
+
.yardoc
|
25
|
+
doc
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
-cfs
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
# DEPENDENCIES
|
4
|
+
gem 'activerecord', require: 'active_record'
|
5
|
+
gem 'activesupport', require: 'active_support/core_ext/object/try'
|
6
|
+
|
7
|
+
# DEVELOPMENT
|
8
|
+
gem 'jeweler'
|
9
|
+
gem 'yard'
|
10
|
+
gem 'RedCloth', require: 'redcloth'
|
11
|
+
|
12
|
+
# TEST
|
13
|
+
gem 'rspec'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
RedCloth (4.2.3)
|
5
|
+
activemodel (3.0.1)
|
6
|
+
activesupport (= 3.0.1)
|
7
|
+
builder (~> 2.1.2)
|
8
|
+
i18n (~> 0.4.1)
|
9
|
+
activerecord (3.0.1)
|
10
|
+
activemodel (= 3.0.1)
|
11
|
+
activesupport (= 3.0.1)
|
12
|
+
arel (~> 1.0.0)
|
13
|
+
tzinfo (~> 0.3.23)
|
14
|
+
activesupport (3.0.1)
|
15
|
+
arel (1.0.1)
|
16
|
+
activesupport (~> 3.0.0)
|
17
|
+
builder (2.1.2)
|
18
|
+
diff-lcs (1.1.2)
|
19
|
+
gemcutter (0.6.1)
|
20
|
+
git (1.2.5)
|
21
|
+
i18n (0.4.2)
|
22
|
+
jeweler (1.4.0)
|
23
|
+
gemcutter (>= 0.1.0)
|
24
|
+
git (>= 1.2.5)
|
25
|
+
rubyforge (>= 2.0.0)
|
26
|
+
json_pure (1.4.6)
|
27
|
+
rspec (2.0.1)
|
28
|
+
rspec-core (~> 2.0.1)
|
29
|
+
rspec-expectations (~> 2.0.1)
|
30
|
+
rspec-mocks (~> 2.0.1)
|
31
|
+
rspec-core (2.0.1)
|
32
|
+
rspec-expectations (2.0.1)
|
33
|
+
diff-lcs (>= 1.1.2)
|
34
|
+
rspec-mocks (2.0.1)
|
35
|
+
rspec-core (~> 2.0.1)
|
36
|
+
rspec-expectations (~> 2.0.1)
|
37
|
+
rubyforge (2.0.4)
|
38
|
+
json_pure (>= 1.1.7)
|
39
|
+
tzinfo (0.3.23)
|
40
|
+
yard (0.6.1)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
RedCloth
|
47
|
+
activerecord
|
48
|
+
activesupport
|
49
|
+
jeweler
|
50
|
+
rspec
|
51
|
+
yard
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Tim Morgan
|
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.textile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
h1. json_serialize -- JSON serialization in ActiveRecord
|
2
|
+
|
3
|
+
| *Author* | Tim Morgan |
|
4
|
+
| *Version* | 1.0 (Oct 30, 2010) |
|
5
|
+
| *License* | Released under the MIT license. |
|
6
|
+
|
7
|
+
h2. About
|
8
|
+
|
9
|
+
@json_serialize@ gives you the ability to JSON-encode data into ActiveRecord
|
10
|
+
model fields. JSON is a more compact but less robust serialization than YAML.
|
11
|
+
Only hashes, arrays, and primitives can be reliably encoded to database fields;
|
12
|
+
other types may not decode properly or at all.
|
13
|
+
|
14
|
+
h2. Installation and Usage
|
15
|
+
|
16
|
+
Firstly, add the gem to your Rails project's @Gemfile@:
|
17
|
+
|
18
|
+
<pre><code>
|
19
|
+
gem 'json_serialize'
|
20
|
+
</code></pre>
|
21
|
+
|
22
|
+
Then, extend your model with the @JsonSerialize@ module, and call the
|
23
|
+
@json_serialize@ method to indicate which fields should be serialized:
|
24
|
+
|
25
|
+
<pre><code>
|
26
|
+
class MyModel < ActiveRecord::Base
|
27
|
+
extend JsonSerialize
|
28
|
+
json_serialize :favorites, :preferences
|
29
|
+
end
|
30
|
+
</code></pre>
|
31
|
+
|
32
|
+
More information can be found at the {JsonSerialize#json_serialize} method
|
33
|
+
documentation.
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rake'
|
2
|
+
begin
|
3
|
+
require 'bundler'
|
4
|
+
rescue LoadError
|
5
|
+
puts "Bundler is not installed; install with `gem install bundler`."
|
6
|
+
exit 1
|
7
|
+
end
|
8
|
+
|
9
|
+
Bundler.require :default
|
10
|
+
|
11
|
+
Jeweler::Tasks.new do |gem|
|
12
|
+
gem.name = "json_serialize"
|
13
|
+
gem.summary = %Q{Adds JSON serialization to ActiveRecord models}
|
14
|
+
gem.description = %Q{Adds to ActiveRecord the ability to JSON-serialize certain fields.}
|
15
|
+
gem.email = "git@timothymorgan.info"
|
16
|
+
gem.homepage = "http://github.com/riscfuture/json_serialize"
|
17
|
+
gem.authors = [ "Tim Morgan" ]
|
18
|
+
gem.required_ruby_version = '>= 1.9'
|
19
|
+
gem.add_dependency "activesupport", ">= 2.0"
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
|
23
|
+
require 'rspec/core/rake_task'
|
24
|
+
RSpec::Core::RakeTask.new
|
25
|
+
|
26
|
+
YARD::Rake::YardocTask.new('doc') do |doc|
|
27
|
+
doc.options << "-m" << "textile"
|
28
|
+
doc.options << "--protected"
|
29
|
+
doc.options << "-r" << "README.textile"
|
30
|
+
doc.options << "-o" << "doc"
|
31
|
+
doc.options << "--title" << "json_serialize Documentation".inspect
|
32
|
+
|
33
|
+
doc.files = [ 'lib/**/*', 'README.textile' ]
|
34
|
+
end
|
35
|
+
|
36
|
+
task(default: :spec)
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{json_serialize}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Tim Morgan"]
|
12
|
+
s.date = %q{2010-10-30}
|
13
|
+
s.description = %q{Adds to ActiveRecord the ability to JSON-serialize certain fields.}
|
14
|
+
s.email = %q{git@timothymorgan.info}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.textile"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
".rspec",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE",
|
26
|
+
"README.textile",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"json_serialize.gemspec",
|
30
|
+
"lib/json_serialize.rb",
|
31
|
+
"spec/json_serialize_spec.rb",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/riscfuture/json_serialize}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.required_ruby_version = Gem::Requirement.new(">= 1.9")
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{Adds JSON serialization to ActiveRecord models}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/json_serialize_spec.rb",
|
42
|
+
"spec/spec_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 2.0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<activesupport>, [">= 2.0"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<activesupport>, [">= 2.0"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# Adds the {#json_serialize} method to @ActiveRecord@ objects.
|
2
|
+
#
|
3
|
+
# @example Basic usage
|
4
|
+
# class MyModel < ActiveRecord::Base
|
5
|
+
# extend JsonSerialize
|
6
|
+
# json_serialize :some_field
|
7
|
+
# end
|
8
|
+
|
9
|
+
module JsonSerialize
|
10
|
+
|
11
|
+
# @overload json_serialize(field, ...)
|
12
|
+
# Marks one or more fields as JSON-serialized. These fields are stored in
|
13
|
+
# the database as JSON and encoded/decoded automatically upon read/write.
|
14
|
+
# @param [Symbol] field The database field to JSON-serialize.
|
15
|
+
|
16
|
+
def json_serialize(*fields)
|
17
|
+
fields.each do |field|
|
18
|
+
define_method field do
|
19
|
+
value = read_attribute(field)
|
20
|
+
value.nil? ? nil : ActiveSupport::JSON.decode(value)
|
21
|
+
end
|
22
|
+
|
23
|
+
define_method :"#{field}=" do |value|
|
24
|
+
write_attribute field, (value.nil? ? nil : ActiveSupport::JSON.encode(value))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
module SpecSupport
|
4
|
+
class JsonSerializeTester
|
5
|
+
extend JsonSerialize
|
6
|
+
json_serialize :field
|
7
|
+
def read_attribute(field) @value end
|
8
|
+
def write_attribute(field, value) @value = value end
|
9
|
+
def get() @value end
|
10
|
+
def set(value) @value = value end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe JsonSerialize do
|
15
|
+
before :each do
|
16
|
+
@object = SpecSupport::JsonSerializeTester.new
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#json_serialize" do
|
20
|
+
context "getter" do
|
21
|
+
it "should JSON-decode the value" do
|
22
|
+
@object.set(ActiveSupport::JSON.encode({ foo: 'bar' }))
|
23
|
+
@object.field.should eql({ 'foo' => 'bar' })
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return nil if the value is nil" do
|
27
|
+
@object.set nil
|
28
|
+
@object.field.should be_nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "setter" do
|
33
|
+
it "should JSON-encode the value" do
|
34
|
+
@object.field = { foo: 'bar' }
|
35
|
+
@object.get.should eql({ foo: 'bar' }.to_json)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should leave nil as nil" do
|
39
|
+
@object.field = nil
|
40
|
+
@object.get.should be_nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_serialize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Tim Morgan
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-30 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activesupport
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 0
|
30
|
+
version: "2.0"
|
31
|
+
type: :runtime
|
32
|
+
prerelease: false
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Adds to ActiveRecord the ability to JSON-serialize certain fields.
|
35
|
+
email: git@timothymorgan.info
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.textile
|
43
|
+
files:
|
44
|
+
- .document
|
45
|
+
- .gitignore
|
46
|
+
- .rspec
|
47
|
+
- Gemfile
|
48
|
+
- Gemfile.lock
|
49
|
+
- LICENSE
|
50
|
+
- README.textile
|
51
|
+
- Rakefile
|
52
|
+
- VERSION
|
53
|
+
- json_serialize.gemspec
|
54
|
+
- lib/json_serialize.rb
|
55
|
+
- spec/json_serialize_spec.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/riscfuture/json_serialize
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 1
|
73
|
+
- 9
|
74
|
+
version: "1.9"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.3.7
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Adds JSON serialization to ActiveRecord models
|
90
|
+
test_files:
|
91
|
+
- spec/json_serialize_spec.rb
|
92
|
+
- spec/spec_helper.rb
|