json_serializable 0.0.2 → 0.1.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/.gitignore +0 -1
- data/.rspec +1 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +26 -0
- data/Rakefile +10 -0
- data/lib/json_serializable.rb +18 -6
- data/lib/json_serializable/version.rb +1 -1
- data/spec/json_serializable_spec.rb +69 -0
- metadata +15 -5
data/.gitignore
CHANGED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
json_serializable (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
rake (0.9.2.2)
|
11
|
+
rspec (2.6.0)
|
12
|
+
rspec-core (~> 2.6.0)
|
13
|
+
rspec-expectations (~> 2.6.0)
|
14
|
+
rspec-mocks (~> 2.6.0)
|
15
|
+
rspec-core (2.6.4)
|
16
|
+
rspec-expectations (2.6.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.6.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
json_serializable!
|
25
|
+
rake
|
26
|
+
rspec
|
data/Rakefile
CHANGED
data/lib/json_serializable.rb
CHANGED
@@ -1,6 +1,13 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
#
|
1
|
+
# This Module extends the as_json method in your model to easily customize the JSON output
|
2
|
+
#
|
3
|
+
# Example:
|
4
|
+
# include JsonSerializable
|
5
|
+
# json_serializable [:name, :description], :private => lambda {|model| model.secret}
|
6
|
+
# this will produce the following Hash
|
7
|
+
# {:name => model.name, :description => model.description, :private => model.secret}
|
8
|
+
#
|
9
|
+
# This module works best when you want to whitelist your attributes for JSON and want to
|
10
|
+
# use different names/values than your attributes name in the models
|
4
11
|
|
5
12
|
require "json_serializable/version"
|
6
13
|
|
@@ -15,11 +22,15 @@ module JsonSerializable
|
|
15
22
|
|
16
23
|
def as_json(options = {})
|
17
24
|
options ||= {}
|
18
|
-
custom_options = self.class.json_field_names.
|
25
|
+
custom_options = self.class.json_field_names.size > 0 ? options.merge(only: self.class.json_field_names) : options
|
19
26
|
custom_hash = super(custom_options)
|
20
27
|
|
21
28
|
self.class.json_mappings.each_pair do |key, value|
|
22
|
-
|
29
|
+
if value.is_a?(Symbol)
|
30
|
+
custom_hash[value] = self.send(key)
|
31
|
+
else
|
32
|
+
custom_hash[key] = value[self]
|
33
|
+
end
|
23
34
|
end
|
24
35
|
|
25
36
|
custom_hash
|
@@ -28,6 +39,7 @@ module JsonSerializable
|
|
28
39
|
|
29
40
|
module ClassMethods
|
30
41
|
|
42
|
+
|
31
43
|
def json_serializable field_names, mappings = {}
|
32
44
|
@@json_field_names = field_names
|
33
45
|
@@json_mappings = mappings
|
@@ -38,7 +50,7 @@ module JsonSerializable
|
|
38
50
|
end
|
39
51
|
|
40
52
|
def json_mappings
|
41
|
-
@@json_mappings ||=
|
53
|
+
@@json_mappings ||= {}
|
42
54
|
end
|
43
55
|
|
44
56
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'json_serializable'
|
2
|
+
|
3
|
+
class Base
|
4
|
+
def as_json(options = {})
|
5
|
+
options
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Model < Base
|
10
|
+
include JsonSerializable
|
11
|
+
|
12
|
+
attr_accessor :name, :description, :secret
|
13
|
+
|
14
|
+
def greet
|
15
|
+
"Hello there"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe JsonSerializable do
|
20
|
+
|
21
|
+
before(:each) do
|
22
|
+
@model = Model.new
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should use base.as_json by default' do
|
26
|
+
@model.as_json(hello: "world").should == {hello: "world"}
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'custom serialization' do
|
30
|
+
|
31
|
+
before(:each) do
|
32
|
+
@model.name = "Sohan"
|
33
|
+
@model.description = "Simple guy"
|
34
|
+
@model.secret = "8789aasd7"
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should whitelist the fields' do
|
38
|
+
Model.class_eval do
|
39
|
+
json_serializable [:name, :description]
|
40
|
+
end
|
41
|
+
|
42
|
+
@model.as_json.should == {only: [:name, :description]}
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should whitelist with lambdas' do
|
46
|
+
Model.class_eval do
|
47
|
+
json_serializable [:name], :private => lambda {|model| model.secret}
|
48
|
+
end
|
49
|
+
|
50
|
+
@model.as_json.should == {only: [:name], :private => @model.secret}
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should allow methods with custom key' do
|
54
|
+
Model.class_eval do
|
55
|
+
json_serializable [:name], :greet => :welcome
|
56
|
+
end
|
57
|
+
|
58
|
+
@model.as_json.should == {only: [:name], :welcome => "Hello there"}
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should allow fields with custom key' do
|
62
|
+
Model.class_eval do
|
63
|
+
json_serializable [:name], :description => :summary
|
64
|
+
end
|
65
|
+
|
66
|
+
@model.as_json.should == {only: [:name], :summary => "Simple guy"}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_serializable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-03-07 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2156365720 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2156365720
|
25
25
|
description: Use custom names in JSON keys and your custom logic in JSON values
|
26
26
|
email:
|
27
27
|
- sohan39@gmail.com
|
@@ -30,11 +30,14 @@ extensions: []
|
|
30
30
|
extra_rdoc_files: []
|
31
31
|
files:
|
32
32
|
- .gitignore
|
33
|
+
- .rspec
|
33
34
|
- Gemfile
|
35
|
+
- Gemfile.lock
|
34
36
|
- Rakefile
|
35
37
|
- json_serializable.gemspec
|
36
38
|
- lib/json_serializable.rb
|
37
39
|
- lib/json_serializable/version.rb
|
40
|
+
- spec/json_serializable_spec.rb
|
38
41
|
homepage: ''
|
39
42
|
licenses: []
|
40
43
|
post_install_message:
|
@@ -47,16 +50,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
50
|
- - ! '>='
|
48
51
|
- !ruby/object:Gem::Version
|
49
52
|
version: '0'
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
hash: 329497193356031779
|
50
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
57
|
none: false
|
52
58
|
requirements:
|
53
59
|
- - ! '>='
|
54
60
|
- !ruby/object:Gem::Version
|
55
61
|
version: '0'
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
hash: 329497193356031779
|
56
65
|
requirements: []
|
57
66
|
rubyforge_project: json_serializable
|
58
|
-
rubygems_version: 1.8.
|
67
|
+
rubygems_version: 1.8.17
|
59
68
|
signing_key:
|
60
69
|
specification_version: 3
|
61
70
|
summary: Provides a clean approach to custom JSON serialize your AR models
|
62
|
-
test_files:
|
71
|
+
test_files:
|
72
|
+
- spec/json_serializable_spec.rb
|