to_j 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3736fab4fbb92872ce6b30d5cb1b382b1b08d69a
4
- data.tar.gz: 18b7d07c59748f4db9ea250249926d4c99f015fa
2
+ SHA256:
3
+ metadata.gz: df7f248de377076c5a2c2bd0d7289c3f4fbe4af999de7a3fb4cfe8d0d12f668b
4
+ data.tar.gz: 501a33961635100bdfbb1c2a45861725d4aad12d295d12fa0c1a1cf23b988f0e
5
5
  SHA512:
6
- metadata.gz: 0c4f03865d620a4b581f566c835367c67b7d359ccf2332b5bf4bc6554594ae2f9c088713d67bf2e7f595e4a8b7d2050517bb36c80f0170b90b835d09ca428bed
7
- data.tar.gz: d6b53b1bb4bc0fbbc41507a8f7b6a101775cb6ac24da79570c9704005078c25af27e315be684d81e88f0c92ed75f1b2508f03b51393dcf24c946227a2f91a001
6
+ metadata.gz: a5a860f9f73437cf5ac2928a47d5b9354f74a874068e0f48f70a35ee7d40061db9ba5792422bf324b5d69695af0af5949a4ea6c72f470a222d807b9e3b25a421
7
+ data.tar.gz: 2b11adf3cac85a15b866e032e46c915efccfd95ad0d89c0a7e45325b7a364bf19e869e3ca2ed8b6748d1bbef1fa886f5af3344451eb67407e2a02fa35526eb80
@@ -1,4 +1,4 @@
1
- Copyright 2018 pioz
1
+ Copyright 2019 Enrico
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,13 +1,17 @@
1
1
  # ToJ
2
2
 
3
- ToJ is a helpful gem that allow you to build json from your active model
4
- object or collection. ToJ allow you to define a Serializer with many methods
5
- called `views`. A view is a method that describe how to generate the json. So
6
- if you want generate a json with only the author data you can use
3
+ [![Build Status](https://travis-ci.org/pioz/to_j.svg?branch=master)](https://travis-ci.org/pioz/to_j)
4
+ [![Coverage Status](https://coveralls.io/repos/github/pioz/to_j/badge.svg?branch=master)](https://coveralls.io/github/pioz/to_j?branch=master)
5
+
6
+ ToJ is a helpful gem that allows you to build json from your active model
7
+ object or collection. ToJ allows you to define a Serializer with many methods
8
+ called `views`. A view is a method that describe how to generate json. So
9
+ if you want to generate a json with only the author data you can use
7
10
  `author.to_j`, but if you want add also the books you can use
8
11
  `author.to_j(view: :with_books)`.
9
12
 
10
13
  ## Installation
14
+
11
15
  Add this line to your application's Gemfile:
12
16
 
13
17
  ```ruby
@@ -15,11 +19,13 @@ gem 'to_j'
15
19
  ```
16
20
 
17
21
  And then execute:
22
+
18
23
  ```bash
19
24
  $ bundle
20
25
  ```
21
26
 
22
27
  Or install it yourself as:
28
+
23
29
  ```bash
24
30
  $ gem install to_j
25
31
  ```
@@ -52,7 +58,7 @@ module AuthorSerializer
52
58
 
53
59
  def default(author, options = {})
54
60
  self.extract!(author, :id, :name)
55
- if options[:user].try(:admin?)
61
+ if options[:user]&.admin?
56
62
  self.extract!(author, :email)
57
63
  end
58
64
  end
@@ -60,7 +66,7 @@ module AuthorSerializer
60
66
  def with_books(author, options = {})
61
67
  default(author, options)
62
68
  self.books do
63
- self.array!(author.books.map{|book| book.to_j(options)})
69
+ self.array!(author.books.map { |book| book.to_j(options) })
64
70
  end
65
71
  end
66
72
 
@@ -93,7 +99,7 @@ Author.limit(100).to_j
93
99
  Author.limit(100).to_j(view: :with_books)
94
100
  # [{"id"=>1028680, "name"=>"Kristopher", "books"=>[{"id"=>192756823, "title"=>"If not now, when?", "nice_title"=>"If Not Now, When?"}, {...}, ...]
95
101
  Book.first.to_j(view: :only_title)
96
- # {"title"=>"Jesting pilate"}
102
+ # {"title"=>"Jesting pilate"}
97
103
  ```
98
104
 
99
105
  or in your controllers:
@@ -119,9 +125,9 @@ end
119
125
  ```
120
126
 
121
127
  If a serializer or `default` method is not defined all table columns will be
122
- added in the json (default Rails behaviour).
128
+ added in the json (default Rails behavior).
123
129
 
124
- ### Under the Hood there is Jbuilder
130
+ ### Under the hood there is Jbuilder
125
131
 
126
132
  The `self` object in serializer's methods are a Jbuilder object so you can use
127
133
  the [Jbuilder](https://github.com/rails/jbuilder/) DSL to create and
@@ -130,9 +136,10 @@ personalized your json views.
130
136
  ## Performance
131
137
 
132
138
  In the `test` directory I've write a simple benchmark to check the performance of this gem with other common serializers:
133
- * [as_json](http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json)
134
- * [active_model_serializers](https://github.com/rails-api/active_model_serializers/)
135
- * [Jbuilder](https://github.com/rails/jbuilder/)
139
+
140
+ - [as_json](http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json)
141
+ - [active_model_serializers](https://github.com/rails-api/active_model_serializers/)
142
+ - [Jbuilder](https://github.com/rails/jbuilder/)
136
143
 
137
144
  Here the results to build a json of `10_000` authors with a total of `100_000` books:
138
145
 
@@ -150,4 +157,5 @@ jbuilder 9.940000 0.380000 10.320000 ( 10.305331)
150
157
  ```
151
158
 
152
159
  ## License
160
+
153
161
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,4 +1,4 @@
1
- require "to_j/railtie"
1
+ require 'to_j/railtie'
2
2
 
3
3
  module ToJ
4
4
  extend ActiveSupport::Concern
@@ -11,15 +11,19 @@ module ToJ
11
11
  @json = Jbuilder.new
12
12
  yield(self) if block_given?
13
13
  end
14
- def method_missing(method, *args, &block)
15
- @json.__send__(method, *args, &block)
14
+
15
+ # rubocop: disable Style/MethodMissingSuper
16
+ # rubocop: disable Style/MissingRespondToMissing
17
+ def method_missing(method_name, *args, &block)
18
+ @json.__send__(method_name, *args, &block)
16
19
  end
20
+ # rubocop: enable all
17
21
  end
18
22
  Object.const_set("#{klass_name}JbuilderProxy", proxy)
19
23
 
20
24
  def self.to_j(options = {})
21
25
  Jbuilder.new do |json|
22
- json.array!(current_scope.map { |obj| obj.to_j(options) })
26
+ json.array!(current_scope.map { |obj| obj.to_j(options) }) # test
23
27
  end.attributes!
24
28
  end
25
29
 
@@ -31,10 +35,9 @@ module ToJ
31
35
  elsif view != :default
32
36
  raise "Serializer view :#{view} does not exist!"
33
37
  else
34
- json.(self, *self.class.column_names)
38
+ json.call(self, *self.class.column_names)
35
39
  end
36
40
  return json.attributes!
37
41
  end
38
-
39
42
  end
40
43
  end
@@ -1,3 +1,4 @@
1
+ # :nocov:
1
2
  module ToJ
2
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'.freeze
3
4
  end
metadata CHANGED
@@ -1,31 +1,59 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_j
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - pioz
7
+ - Enrico
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-08 00:00:00.000000000 Z
11
+ date: 2019-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 5.2.0
19
+ version: '5.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 5.2.0
26
+ version: '5.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: sqlite3
28
+ name: active_model_serializers
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
29
57
  requirement: !ruby/object:Gem::Requirement
30
58
  requirements:
31
59
  - - ">="
@@ -66,6 +94,20 @@ dependencies:
66
94
  - - ">="
67
95
  - !ruby/object:Gem::Version
68
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: jbuilder
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
69
111
  - !ruby/object:Gem::Dependency
70
112
  name: oj
71
113
  requirement: !ruby/object:Gem::Requirement
@@ -81,7 +123,7 @@ dependencies:
81
123
  - !ruby/object:Gem::Version
82
124
  version: '0'
83
125
  - !ruby/object:Gem::Dependency
84
- name: jbuilder
126
+ name: rubocop
85
127
  requirement: !ruby/object:Gem::Requirement
86
128
  requirements:
87
129
  - - ">="
@@ -95,7 +137,35 @@ dependencies:
95
137
  - !ruby/object:Gem::Version
96
138
  version: '0'
97
139
  - !ruby/object:Gem::Dependency
98
- name: active_model_serializers
140
+ name: rubocop-performance
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-rails
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: sqlite3
99
169
  requirement: !ruby/object:Gem::Requirement
100
170
  requirements:
101
171
  - - ">="
@@ -108,7 +178,7 @@ dependencies:
108
178
  - - ">="
109
179
  - !ruby/object:Gem::Version
110
180
  version: '0'
111
- description: Fast JSON serializer for Rails based on Jbuilder and concept of view
181
+ description: Fast JSON serializer for Rails based on Jbuilder and concept of views.
112
182
  email:
113
183
  - epilotto@gmx.com
114
184
  executables: []
@@ -118,7 +188,6 @@ files:
118
188
  - MIT-LICENSE
119
189
  - README.md
120
190
  - Rakefile
121
- - lib/tasks/to_j_tasks.rake
122
191
  - lib/to_j.rb
123
192
  - lib/to_j/railtie.rb
124
193
  - lib/to_j/version.rb
@@ -141,9 +210,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
210
  - !ruby/object:Gem::Version
142
211
  version: '0'
143
212
  requirements: []
144
- rubyforge_project:
145
- rubygems_version: 2.6.13
213
+ rubygems_version: 3.0.2
146
214
  signing_key:
147
215
  specification_version: 4
148
- summary: Fast JSON serializer for Rails based on Jbuilder and concept of view
216
+ summary: Fast JSON serializer for Rails based on Jbuilder and concept of views.
149
217
  test_files: []
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :to_j do
3
- # # Task goes here
4
- # end