to_j 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3736fab4fbb92872ce6b30d5cb1b382b1b08d69a
4
+ data.tar.gz: 18b7d07c59748f4db9ea250249926d4c99f015fa
5
+ SHA512:
6
+ metadata.gz: 0c4f03865d620a4b581f566c835367c67b7d359ccf2332b5bf4bc6554594ae2f9c088713d67bf2e7f595e4a8b7d2050517bb36c80f0170b90b835d09ca428bed
7
+ data.tar.gz: d6b53b1bb4bc0fbbc41507a8f7b6a101775cb6ac24da79570c9704005078c25af27e315be684d81e88f0c92ed75f1b2508f03b51393dcf24c946227a2f91a001
@@ -0,0 +1,20 @@
1
+ Copyright 2018 pioz
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.
@@ -0,0 +1,153 @@
1
+ # ToJ
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
7
+ `author.to_j`, but if you want add also the books you can use
8
+ `author.to_j(view: :with_books)`.
9
+
10
+ ## Installation
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'to_j'
15
+ ```
16
+
17
+ And then execute:
18
+ ```bash
19
+ $ bundle
20
+ ```
21
+
22
+ Or install it yourself as:
23
+ ```bash
24
+ $ gem install to_j
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ In your models include the ToJ module:
30
+
31
+ ```ruby
32
+ class Author < ApplicationRecord
33
+ include ToJ
34
+ has_many :books, dependent: :destroy
35
+ end
36
+
37
+ class Book < ApplicationRecord
38
+ include ToJ
39
+ belongs_to :author
40
+
41
+ def nice_title
42
+ self.title.titleize
43
+ end
44
+ end
45
+ ```
46
+
47
+ Then in the `app/serializers` directory create a serializer for each model:
48
+
49
+ ```ruby
50
+ # app/serializers/author_serializer.rb
51
+ module AuthorSerializer
52
+
53
+ def default(author, options = {})
54
+ self.extract!(author, :id, :name)
55
+ if options[:user].try(:admin?)
56
+ self.extract!(author, :email)
57
+ end
58
+ end
59
+
60
+ def with_books(author, options = {})
61
+ default(author, options)
62
+ self.books do
63
+ self.array!(author.books.map{|book| book.to_j(options)})
64
+ end
65
+ end
66
+
67
+ end
68
+ ```
69
+
70
+ ```ruby
71
+ # app/serializers/book_serializer.rb
72
+ module BookSerializer
73
+
74
+ def default(book, options = {})
75
+ self.extract!(book, :id, :title, :nice_title)
76
+ end
77
+
78
+ def only_title(book, options = {})
79
+ self.extract!(book, :title)
80
+ end
81
+
82
+ end
83
+ ```
84
+
85
+ The default view is called if the option `:view` is not present on `to_j`
86
+ options param.
87
+
88
+ Now you can call:
89
+
90
+ ```ruby
91
+ Author.limit(100).to_j
92
+ # [{"id"=>1028680, "name"=>"Kristopher", {...}, ...]
93
+ Author.limit(100).to_j(view: :with_books)
94
+ # [{"id"=>1028680, "name"=>"Kristopher", "books"=>[{"id"=>192756823, "title"=>"If not now, when?", "nice_title"=>"If Not Now, When?"}, {...}, ...]
95
+ Book.first.to_j(view: :only_title)
96
+ # {"title"=>"Jesting pilate"}
97
+ ```
98
+
99
+ or in your controllers:
100
+
101
+ ```ruby
102
+
103
+ def index
104
+ @authors = Author.limit(100)
105
+ respond_to do |format|
106
+ format.html
107
+ format.json { render json: @authors.to_j(user: current_user) }
108
+ end
109
+ end
110
+
111
+ def show
112
+ @author = Author.find(params[:id])
113
+ respond_to do |format|
114
+ format.html
115
+ format.json { render json: @author.to_j(view: :with_books) }
116
+ end
117
+ end
118
+
119
+ ```
120
+
121
+ If a serializer or `default` method is not defined all table columns will be
122
+ added in the json (default Rails behaviour).
123
+
124
+ ### Under the Hood there is Jbuilder
125
+
126
+ The `self` object in serializer's methods are a Jbuilder object so you can use
127
+ the [Jbuilder](https://github.com/rails/jbuilder/) DSL to create and
128
+ personalized your json views.
129
+
130
+ ## Performance
131
+
132
+ 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/)
136
+
137
+ Here the results to build a json of `10_000` authors with a total of `100_000` books:
138
+
139
+ ```
140
+ Run options: --seed 333
141
+
142
+ # Running:
143
+
144
+ user system total real
145
+ to_j 9.950000 0.340000 10.290000 ( 10.367328)
146
+ as_json 15.460000 0.400000 15.860000 ( 15.942882)
147
+ ams 16.000000 0.460000 16.460000 ( 16.578348)
148
+ jbuilder 9.940000 0.380000 10.320000 ( 10.305331)
149
+
150
+ ```
151
+
152
+ ## License
153
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ToJ'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :to_j do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,40 @@
1
+ require "to_j/railtie"
2
+
3
+ module ToJ
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ klass_name = self.name
8
+ proxy = Class.new do
9
+ include "#{klass_name}Serializer".constantize rescue nil
10
+ def initialize
11
+ @json = Jbuilder.new
12
+ yield(self) if block_given?
13
+ end
14
+ def method_missing(method, *args, &block)
15
+ @json.__send__(method, *args, &block)
16
+ end
17
+ end
18
+ Object.const_set("#{klass_name}JbuilderProxy", proxy)
19
+
20
+ def self.to_j(options = {})
21
+ Jbuilder.new do |json|
22
+ json.array!(current_scope.map { |obj| obj.to_j(options) })
23
+ end.attributes!
24
+ end
25
+
26
+ def to_j(options = {})
27
+ view = options[:view] || :default
28
+ json = "#{self.class.name}JbuilderProxy".constantize.new
29
+ if json.respond_to?(view)
30
+ json.send(view, self, options.except(:view))
31
+ elsif view != :default
32
+ raise "Serializer view :#{view} does not exist!"
33
+ else
34
+ json.(self, *self.class.column_names)
35
+ end
36
+ return json.attributes!
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+ module ToJ
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module ToJ
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_j
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - pioz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-08 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: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
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: factory_bot_rails
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: faker
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: oj
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: jbuilder
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: active_model_serializers
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'
111
+ description: Fast JSON serializer for Rails based on Jbuilder and concept of view
112
+ email:
113
+ - epilotto@gmx.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - MIT-LICENSE
119
+ - README.md
120
+ - Rakefile
121
+ - lib/tasks/to_j_tasks.rake
122
+ - lib/to_j.rb
123
+ - lib/to_j/railtie.rb
124
+ - lib/to_j/version.rb
125
+ homepage: https://github.com/pioz/to_j
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.6.13
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Fast JSON serializer for Rails based on Jbuilder and concept of view
149
+ test_files: []