juso 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +10 -1
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +134 -1
- data/README.md +60 -7
- data/Rakefile +11 -5
- data/benchmark/single_resource.rb +2 -4
- data/juso.gemspec +2 -0
- data/lib/juso/version.rb +1 -1
- data/lib/juso.rb +38 -24
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 912622622f8b9a6866a505f8ba27a1cc21a3073a59ac364942ce6d997baeb875
|
4
|
+
data.tar.gz: 125f56e1649fad476b076457272349a221ec69f38600bc17f62bf0255902a777
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc04ee74481f9fa92988d4e4457eb40929e670a91c95b07171af2358c5f85a77dbd2af2ba846a0bc1c90da13aca85363fba5060b75255e91373fcf964ee5893c
|
7
|
+
data.tar.gz: 7d51bbec1f134665d4f058c666d64616762e8b86b2981fd94eb03af40a638e71d952e42bdaf1a8216342a4ee644da6f57f7ccba8730b0f4dc6f6672e1ae6e0b9
|
data/.github/workflows/main.yml
CHANGED
@@ -8,8 +8,10 @@ jobs:
|
|
8
8
|
timeout-minutes: 10
|
9
9
|
|
10
10
|
strategy:
|
11
|
+
fail-fast: false
|
11
12
|
matrix:
|
12
|
-
ruby-version: [3.1.0-preview1, 3.0.2, 2.7.4, 2.6.8]
|
13
|
+
# ruby-version: [3.1.0-preview1, 3.0.2, 2.7.4, 2.6.8]
|
14
|
+
ruby-version: [3.0.2, 2.7.4, 2.6.8]
|
13
15
|
|
14
16
|
steps:
|
15
17
|
- uses: actions/checkout@v2
|
@@ -22,3 +24,10 @@ jobs:
|
|
22
24
|
|
23
25
|
- name: Run test
|
24
26
|
run: bundle exec rake test
|
27
|
+
|
28
|
+
- name: Run test (rails)
|
29
|
+
env:
|
30
|
+
RAILS_TEST: true
|
31
|
+
run: |
|
32
|
+
bundle exec rails db:setup
|
33
|
+
bundle exec rake test
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,16 +1,131 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
juso (
|
4
|
+
juso (1.0.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
actioncable (6.1.4.1)
|
10
|
+
actionpack (= 6.1.4.1)
|
11
|
+
activesupport (= 6.1.4.1)
|
12
|
+
nio4r (~> 2.0)
|
13
|
+
websocket-driver (>= 0.6.1)
|
14
|
+
actionmailbox (6.1.4.1)
|
15
|
+
actionpack (= 6.1.4.1)
|
16
|
+
activejob (= 6.1.4.1)
|
17
|
+
activerecord (= 6.1.4.1)
|
18
|
+
activestorage (= 6.1.4.1)
|
19
|
+
activesupport (= 6.1.4.1)
|
20
|
+
mail (>= 2.7.1)
|
21
|
+
actionmailer (6.1.4.1)
|
22
|
+
actionpack (= 6.1.4.1)
|
23
|
+
actionview (= 6.1.4.1)
|
24
|
+
activejob (= 6.1.4.1)
|
25
|
+
activesupport (= 6.1.4.1)
|
26
|
+
mail (~> 2.5, >= 2.5.4)
|
27
|
+
rails-dom-testing (~> 2.0)
|
28
|
+
actionpack (6.1.4.1)
|
29
|
+
actionview (= 6.1.4.1)
|
30
|
+
activesupport (= 6.1.4.1)
|
31
|
+
rack (~> 2.0, >= 2.0.9)
|
32
|
+
rack-test (>= 0.6.3)
|
33
|
+
rails-dom-testing (~> 2.0)
|
34
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
35
|
+
actiontext (6.1.4.1)
|
36
|
+
actionpack (= 6.1.4.1)
|
37
|
+
activerecord (= 6.1.4.1)
|
38
|
+
activestorage (= 6.1.4.1)
|
39
|
+
activesupport (= 6.1.4.1)
|
40
|
+
nokogiri (>= 1.8.5)
|
41
|
+
actionview (6.1.4.1)
|
42
|
+
activesupport (= 6.1.4.1)
|
43
|
+
builder (~> 3.1)
|
44
|
+
erubi (~> 1.4)
|
45
|
+
rails-dom-testing (~> 2.0)
|
46
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
47
|
+
activejob (6.1.4.1)
|
48
|
+
activesupport (= 6.1.4.1)
|
49
|
+
globalid (>= 0.3.6)
|
50
|
+
activemodel (6.1.4.1)
|
51
|
+
activesupport (= 6.1.4.1)
|
52
|
+
activerecord (6.1.4.1)
|
53
|
+
activemodel (= 6.1.4.1)
|
54
|
+
activesupport (= 6.1.4.1)
|
55
|
+
activestorage (6.1.4.1)
|
56
|
+
actionpack (= 6.1.4.1)
|
57
|
+
activejob (= 6.1.4.1)
|
58
|
+
activerecord (= 6.1.4.1)
|
59
|
+
activesupport (= 6.1.4.1)
|
60
|
+
marcel (~> 1.0.0)
|
61
|
+
mini_mime (>= 1.1.0)
|
62
|
+
activesupport (6.1.4.1)
|
63
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
64
|
+
i18n (>= 1.6, < 2)
|
65
|
+
minitest (>= 5.1)
|
66
|
+
tzinfo (~> 2.0)
|
67
|
+
zeitwerk (~> 2.3)
|
9
68
|
ast (2.4.2)
|
69
|
+
builder (3.2.4)
|
70
|
+
concurrent-ruby (1.1.9)
|
71
|
+
crass (1.0.6)
|
72
|
+
erubi (1.10.0)
|
73
|
+
globalid (1.0.0)
|
74
|
+
activesupport (>= 5.0)
|
75
|
+
i18n (1.8.11)
|
76
|
+
concurrent-ruby (~> 1.0)
|
77
|
+
json_expressions (0.9.0)
|
78
|
+
loofah (2.12.0)
|
79
|
+
crass (~> 1.0.2)
|
80
|
+
nokogiri (>= 1.5.9)
|
81
|
+
mail (2.7.1)
|
82
|
+
mini_mime (>= 0.1.1)
|
83
|
+
marcel (1.0.2)
|
84
|
+
method_source (1.0.0)
|
85
|
+
mini_mime (1.1.2)
|
86
|
+
mini_portile2 (2.6.1)
|
10
87
|
minitest (5.14.4)
|
88
|
+
nio4r (2.5.8)
|
89
|
+
nokogiri (1.12.5)
|
90
|
+
mini_portile2 (~> 2.6.1)
|
91
|
+
racc (~> 1.4)
|
92
|
+
nokogiri (1.12.5-arm64-darwin)
|
93
|
+
racc (~> 1.4)
|
94
|
+
nokogiri (1.12.5-x86_64-linux)
|
95
|
+
racc (~> 1.4)
|
11
96
|
parallel (1.21.0)
|
12
97
|
parser (3.0.2.0)
|
13
98
|
ast (~> 2.4.1)
|
99
|
+
racc (1.6.0)
|
100
|
+
rack (2.2.3)
|
101
|
+
rack-test (1.1.0)
|
102
|
+
rack (>= 1.0, < 3)
|
103
|
+
rails (6.1.4.1)
|
104
|
+
actioncable (= 6.1.4.1)
|
105
|
+
actionmailbox (= 6.1.4.1)
|
106
|
+
actionmailer (= 6.1.4.1)
|
107
|
+
actionpack (= 6.1.4.1)
|
108
|
+
actiontext (= 6.1.4.1)
|
109
|
+
actionview (= 6.1.4.1)
|
110
|
+
activejob (= 6.1.4.1)
|
111
|
+
activemodel (= 6.1.4.1)
|
112
|
+
activerecord (= 6.1.4.1)
|
113
|
+
activestorage (= 6.1.4.1)
|
114
|
+
activesupport (= 6.1.4.1)
|
115
|
+
bundler (>= 1.15.0)
|
116
|
+
railties (= 6.1.4.1)
|
117
|
+
sprockets-rails (>= 2.0.0)
|
118
|
+
rails-dom-testing (2.0.3)
|
119
|
+
activesupport (>= 4.2.0)
|
120
|
+
nokogiri (>= 1.6)
|
121
|
+
rails-html-sanitizer (1.4.2)
|
122
|
+
loofah (~> 2.3)
|
123
|
+
railties (6.1.4.1)
|
124
|
+
actionpack (= 6.1.4.1)
|
125
|
+
activesupport (= 6.1.4.1)
|
126
|
+
method_source
|
127
|
+
rake (>= 0.13)
|
128
|
+
thor (~> 1.0)
|
14
129
|
rainbow (3.0.0)
|
15
130
|
rake (13.0.6)
|
16
131
|
regexp_parser (2.1.1)
|
@@ -27,7 +142,22 @@ GEM
|
|
27
142
|
rubocop-ast (1.13.0)
|
28
143
|
parser (>= 3.0.1.1)
|
29
144
|
ruby-progressbar (1.11.0)
|
145
|
+
sprockets (4.0.2)
|
146
|
+
concurrent-ruby (~> 1.0)
|
147
|
+
rack (> 1, < 3)
|
148
|
+
sprockets-rails (3.4.1)
|
149
|
+
actionpack (>= 5.2)
|
150
|
+
activesupport (>= 5.2)
|
151
|
+
sprockets (>= 3.0.0)
|
152
|
+
sqlite3 (1.4.2)
|
153
|
+
thor (1.1.0)
|
154
|
+
tzinfo (2.0.4)
|
155
|
+
concurrent-ruby (~> 1.0)
|
30
156
|
unicode-display_width (2.1.0)
|
157
|
+
websocket-driver (0.7.5)
|
158
|
+
websocket-extensions (>= 0.1.0)
|
159
|
+
websocket-extensions (0.1.5)
|
160
|
+
zeitwerk (2.5.1)
|
31
161
|
|
32
162
|
PLATFORMS
|
33
163
|
arm64-darwin-20
|
@@ -35,10 +165,13 @@ PLATFORMS
|
|
35
165
|
x86_64-linux
|
36
166
|
|
37
167
|
DEPENDENCIES
|
168
|
+
json_expressions
|
38
169
|
juso!
|
39
170
|
minitest (~> 5.0)
|
171
|
+
rails (~> 6.1.4)
|
40
172
|
rake (~> 13.0)
|
41
173
|
rubocop (~> 1.7)
|
174
|
+
sqlite3
|
42
175
|
|
43
176
|
BUNDLED WITH
|
44
177
|
2.2.22
|
data/README.md
CHANGED
@@ -7,6 +7,16 @@
|
|
7
7
|
Juso is simple, fast and explicit JSON Serializer.
|
8
8
|
Juso means 13 (thirteen) in Japanese.
|
9
9
|
|
10
|
+
## Motivation
|
11
|
+
|
12
|
+
#### Japanese
|
13
|
+
|
14
|
+
Juso は juso というメソッドを定義することで JSON 化が可能になります。Ruby の Hash や Array を用いて定義すれば良いので、覚えることが非常に少ないことが特徴です。また、暗黙的な挙動で非公開にすべき属性が公開されることを防ぎます。
|
15
|
+
|
16
|
+
Ruby on Rails においては Model のクラスにそのまま定義することができるため、初期の導入としてはシンプルでわかりやすいです。[^1]
|
17
|
+
|
18
|
+
[^1]: as_json メソッドで頑張ることもできますが、より宣言的で分かりやすいはずです
|
19
|
+
|
10
20
|
## Installation
|
11
21
|
|
12
22
|
Add this line to your application's Gemfile:
|
@@ -26,7 +36,7 @@ Or install it yourself as:
|
|
26
36
|
## Usage
|
27
37
|
|
28
38
|
1. Include `Juso::Serializable` to your class.
|
29
|
-
2. Define
|
39
|
+
2. Define juso(context) method.
|
30
40
|
3. Use Juso.generate(object) method to generate json.
|
31
41
|
|
32
42
|
```ruby
|
@@ -35,7 +45,7 @@ class User < ApplicationRecord
|
|
35
45
|
|
36
46
|
# ...
|
37
47
|
|
38
|
-
def
|
48
|
+
def juso(context)
|
39
49
|
{
|
40
50
|
id: id,
|
41
51
|
nickname: nickname,
|
@@ -50,7 +60,7 @@ class Team < ApplicationRecord
|
|
50
60
|
|
51
61
|
# ...
|
52
62
|
|
53
|
-
def
|
63
|
+
def juso(context)
|
54
64
|
{
|
55
65
|
id: id,
|
56
66
|
name: name,
|
@@ -70,17 +80,60 @@ Juso.generate(team)
|
|
70
80
|
|
71
81
|
#### Japanese
|
72
82
|
|
73
|
-
|
83
|
+
juso メソッドは以下のインスタンスしか返してはいけません
|
74
84
|
|
75
85
|
- Numeric Class
|
76
86
|
- String Class
|
77
|
-
-
|
87
|
+
- Nil Class
|
88
|
+
- True Class
|
89
|
+
- False Class
|
78
90
|
- Hash Class
|
79
91
|
- Array Class
|
80
|
-
- Juso::Serializable をinclude したクラス
|
92
|
+
- Juso::Serializable を include したクラス
|
81
93
|
- Date / DateTime / ActiveSupport::TimeWithZone
|
82
94
|
|
83
|
-
再帰的にjusoの処理が適用されるため、Arrayの要素やHashのvalueも同様のルールが適用されます
|
95
|
+
再帰的に juso の処理が適用されるため、Array の要素や Hash の value も同様のルールが適用されます
|
96
|
+
|
97
|
+
### Juso::Context
|
98
|
+
|
99
|
+
#### Japanese
|
100
|
+
|
101
|
+
juso メソッドには Context オブジェクトが渡されます。これによって、Juso.generate から各 juso メソッドにシリアライズのオプションを伝播させることができます
|
102
|
+
|
103
|
+
### Juso.wrap
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
class UserSerializer
|
107
|
+
include Juso::Serializable
|
108
|
+
|
109
|
+
# ...
|
110
|
+
|
111
|
+
def juso(context)
|
112
|
+
{
|
113
|
+
id: @user.id,
|
114
|
+
posts: Juso.wrap(@user.posts, PostSerializer), # use PostSerializer#juso method. Each post passes into PostSerializer object.
|
115
|
+
team: @user.team, # use Team#juso method
|
116
|
+
}
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
class PostSerializer
|
121
|
+
include Juso::Serializable
|
122
|
+
|
123
|
+
def initialize(post)
|
124
|
+
@post = post
|
125
|
+
end
|
126
|
+
|
127
|
+
def juso(context)
|
128
|
+
# do something with @post...
|
129
|
+
end
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
#### Japanese
|
134
|
+
|
135
|
+
Juso.wrap(object, serializable_class) ユーティリティを使うことで、特定のクラスに処理を委譲できます。
|
136
|
+
コード例だと、 @user.posts は Post の ActiveRecord::Relation を返しますが、通常であれば Post インスタンスの juso メソッドが使われるのに対して、各 Post インスタンスを PostSerializer でラップします。PostSerializer#juso メソッドが呼ばれることになります。
|
84
137
|
|
85
138
|
## Development
|
86
139
|
|
data/Rakefile
CHANGED
@@ -6,11 +6,17 @@ require "rake/testtask"
|
|
6
6
|
Rake::TestTask.new(:test) do |t|
|
7
7
|
t.libs << "test"
|
8
8
|
t.libs << "lib"
|
9
|
-
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
-
end
|
11
9
|
|
12
|
-
|
10
|
+
files = FileList["test/juso_test.rb"]
|
11
|
+
|
12
|
+
if ENV['RAILS_TEST']
|
13
|
+
files << 'test/rails_test.rb'
|
14
|
+
end
|
15
|
+
|
16
|
+
t.test_files = files
|
17
|
+
end
|
13
18
|
|
14
|
-
|
19
|
+
# require "rubocop/rake_task"
|
20
|
+
# RuboCop::RakeTask.new
|
15
21
|
|
16
|
-
task default: %i[test
|
22
|
+
task default: %i[test]
|
@@ -90,13 +90,11 @@ end
|
|
90
90
|
# --- Juso serializers ---
|
91
91
|
|
92
92
|
require "juso"
|
93
|
-
require 'active_record' # hack for CollectionProxy
|
94
|
-
Juso.reset_collection_classes
|
95
93
|
|
96
94
|
class Comment
|
97
95
|
include ::Juso::Serializable
|
98
96
|
|
99
|
-
def
|
97
|
+
def juso(context)
|
100
98
|
{id: id, body: body}
|
101
99
|
end
|
102
100
|
end
|
@@ -104,7 +102,7 @@ end
|
|
104
102
|
class Post
|
105
103
|
include ::Juso::Serializable
|
106
104
|
|
107
|
-
def
|
105
|
+
def juso(context)
|
108
106
|
{id: id, body: body, commenter_names: commenter_names, comments: comments}
|
109
107
|
end
|
110
108
|
end
|
data/juso.gemspec
CHANGED
data/lib/juso/version.rb
CHANGED
data/lib/juso.rb
CHANGED
@@ -9,63 +9,77 @@ module Juso
|
|
9
9
|
class Error < StandardError; end
|
10
10
|
|
11
11
|
module Serializable
|
12
|
-
def
|
12
|
+
def juso(_)
|
13
13
|
nil
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
# Juso
|
18
|
-
# xxxxx
|
17
|
+
# Juso::Context is serializer context
|
19
18
|
class Context
|
20
|
-
|
21
|
-
|
19
|
+
DEFAULT_OPTS = {}.freeze
|
20
|
+
|
21
|
+
def initialize(serializer: :default, options: DEFAULT_OPTS)
|
22
|
+
@serializer = serializer
|
23
|
+
@options = options
|
22
24
|
end
|
23
25
|
|
24
|
-
attr_reader :
|
26
|
+
attr_reader :serializer, :options
|
25
27
|
end
|
26
28
|
|
29
|
+
# Juso.generate generates json string
|
27
30
|
def self.generate(object, context: Context.new)
|
28
|
-
JSON.fast_generate(
|
31
|
+
JSON.fast_generate(_g(object, context))
|
29
32
|
end
|
30
33
|
|
31
34
|
# generate returns hash (as json)
|
32
|
-
def self.
|
35
|
+
def self._g(object, context)
|
33
36
|
case object
|
34
|
-
when nil, Numeric, String
|
37
|
+
when nil, Numeric, String, true, false
|
35
38
|
return object
|
36
39
|
when Hash
|
37
40
|
return object.each_with_object({}) do |(k, v), acc|
|
38
|
-
acc[k] =
|
41
|
+
acc[k] = _g(v, context)
|
39
42
|
end
|
40
43
|
when Serializable
|
41
|
-
# respond_to?(:
|
42
|
-
return
|
44
|
+
# respond_to?(:juso) のほうが良い可能性ある?
|
45
|
+
return _g(object.juso(context), context)
|
43
46
|
when *collection_classes
|
44
|
-
return object.to_a.map { |o|
|
47
|
+
return object.to_a.map { |o| _g(o, context) }
|
45
48
|
when *date_classes
|
46
49
|
return object.iso8601
|
47
50
|
else
|
48
|
-
# TODO: fallback to respond_to?(:
|
51
|
+
# TODO: fallback to respond_to?(:juso) and warn?
|
49
52
|
|
50
53
|
raise Error.new("cannot serialize object: #{object}. you must include Juso::Serializable")
|
51
54
|
end
|
52
55
|
end
|
53
56
|
|
57
|
+
# Juso.wrap is utility for wrapping object with Juso::Serializable class
|
58
|
+
def self.wrap(object, klass)
|
59
|
+
if collection_classes.any? { |arrayish| object.is_a?(arrayish) }
|
60
|
+
object.to_a.map { |o| klass.new(o) }
|
61
|
+
else
|
62
|
+
klass.new(object)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
54
66
|
def self.collection_classes
|
55
|
-
@collection_classes
|
67
|
+
@collection_classes ||= default_collection_classes
|
56
68
|
end
|
57
69
|
|
58
|
-
def self.
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
end
|
70
|
+
def self.default_collection_classes
|
71
|
+
if defined?(ActiveRecord)
|
72
|
+
[Array, ActiveRecord::Relation]
|
73
|
+
else
|
74
|
+
[Array]
|
75
|
+
end
|
65
76
|
end
|
66
77
|
|
67
78
|
def self.date_classes
|
68
|
-
|
79
|
+
@date_classes ||= default_date_classes
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.default_date_classes
|
69
83
|
if defined?(ActiveSupport::TimeWithZone)
|
70
84
|
[Date, DateTime, ActiveSupport::TimeWithZone]
|
71
85
|
else
|
@@ -73,5 +87,5 @@ module Juso
|
|
73
87
|
end
|
74
88
|
end
|
75
89
|
|
76
|
-
|
90
|
+
private_class_method :default_collection_classes, :default_date_classes, :_g
|
77
91
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: juso
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ykpythemind
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-11-
|
12
|
-
dependencies:
|
11
|
+
date: 2021-11-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 6.1.4
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 6.1.4
|
13
27
|
description: juso is simple json serializer for web application
|
14
28
|
email:
|
15
29
|
- yukibukiyou@gmail.com
|