jv 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: 4659c720a7cf96cb6e041588e660105b7073ae39
4
+ data.tar.gz: c53b26a7660f2fe140b20a067da5326f70132c29
5
+ SHA512:
6
+ metadata.gz: 4657fb5f3959d9af6a9deb2cd7b983855e01b6de6d59395dfc0c38e941b5b368b23731e06e0d559c4d5cb14f359855ee9d4b393b09b59936cfcafab9b361c18a
7
+ data.tar.gz: 1116ddeafae5714e5e43f0fbc53ae64a321827df68079d0010fdee29d1915adae1ced67170c3d4534bcccb26f2ffe2980ab64b462f49c98ba684c694da151883
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ /.idea/
14
+ /vendor/bundle
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in jv.gemspec
6
+ gemspec
7
+
8
+ gem 'benchmark-ips'
9
+ gem 'jbuilder'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 tzmfreedom
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,166 @@
1
+ # Jv
2
+
3
+ Rails JSON View Handler with ruby hash.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'jv'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install jv
20
+
21
+ ## Usage
22
+
23
+ You write view file with extension `xxx.json.jv`
24
+ ```ruby
25
+ json = {}
26
+ json[:string_property] = @string
27
+ json[:integer_property] = 123
28
+ json[:array_property] = @integers
29
+ json[:object_property] = {
30
+ single_partial: r.(partial: 'api/record', locals: { record: { a: 123, b: 100 } }),
31
+ collection_partial: r.(partial: 'api/record', collection: @records, as: :record)
32
+ }
33
+ json
34
+ ```
35
+
36
+ This response is following
37
+ ```javascript
38
+ {
39
+ "string_property": "string",
40
+ "integer_property": 123,
41
+ "array": [
42
+ 1,
43
+ 2,
44
+ 3,
45
+ 4,
46
+ 5
47
+ ],
48
+ "object_property": {
49
+ "single_partial": {
50
+ "a": 123,
51
+ "b": 100
52
+ },
53
+ "collection_partial": [
54
+ {
55
+ "a": 123,
56
+ "b": 234
57
+ },
58
+ # ...
59
+ ]
60
+ }
61
+ }
62
+ ```
63
+
64
+ ## Benchmark
65
+
66
+ Benchmark script is there.
67
+ * [bin/benchmark.rb](https://github.com/tzmfreedom/jv/blob/master/spec/dummy_app/bin/benchmark.rb)
68
+ * [api_controller.rb](https://github.com/tzmfreedom/jv/blob/master/spec/dummy_app/app/controllers/api_controller.rb)
69
+
70
+ ### Result
71
+
72
+ Development
73
+ ```
74
+ * Rendering 10 partials via render_partial
75
+ Warming up --------------------------------------
76
+ jbuilder 8.000 i/100ms
77
+ jv 10.000 i/100ms
78
+ Calculating -------------------------------------
79
+ jbuilder 79.193 (± 7.6%) i/s - 400.000 in 5.085408s
80
+ jv 106.160 (± 6.6%) i/s - 530.000 in 5.013174s
81
+
82
+ Comparison:
83
+ jv: 106.2 i/s
84
+ jbuilder: 79.2 i/s - 1.34x slower
85
+
86
+
87
+ * Rendering 100 partials via render_partial
88
+ Warming up --------------------------------------
89
+ jbuilder 1.000 i/100ms
90
+ jv 7.000 i/100ms
91
+ Calculating -------------------------------------
92
+ jbuilder 14.927 (±13.4%) i/s - 74.000 in 5.029316s
93
+ jv 71.601 (± 7.0%) i/s - 357.000 in 5.019427s
94
+
95
+ Comparison:
96
+ jv: 71.6 i/s
97
+ jbuilder: 14.9 i/s - 4.80x slower
98
+
99
+
100
+ * Rendering 1000 partials via render_partial
101
+ Warming up --------------------------------------
102
+ jbuilder 1.000 i/100ms
103
+ jv 1.000 i/100ms
104
+ Calculating -------------------------------------
105
+ jbuilder 1.614 (± 0.0%) i/s - 9.000 in 5.587806s
106
+ jv 19.176 (±10.4%) i/s - 95.000 in 5.001148s
107
+
108
+ Comparison:
109
+ jv: 19.2 i/s
110
+ jbuilder: 1.6 i/s - 11.88x slower
111
+ ```
112
+
113
+ Production
114
+ ```
115
+ * Rendering 10 partials via render_partial
116
+ Warming up --------------------------------------
117
+ jbuilder 48.000 i/100ms
118
+ jv 88.000 i/100ms
119
+ Calculating -------------------------------------
120
+ jbuilder 496.263 (± 8.7%) i/s - 2.496k in 5.075655s
121
+ jv 888.175 (± 7.0%) i/s - 4.488k in 5.080282s
122
+
123
+ Comparison:
124
+ jv: 888.2 i/s
125
+ jbuilder: 496.3 i/s - 1.79x slower
126
+
127
+
128
+ * Rendering 100 partials via render_partial
129
+ Warming up --------------------------------------
130
+ jbuilder 6.000 i/100ms
131
+ jv 18.000 i/100ms
132
+ Calculating -------------------------------------
133
+ jbuilder 64.780 (±10.8%) i/s - 324.000 in 5.069040s
134
+ jv 192.008 (± 6.2%) i/s - 972.000 in 5.082385s
135
+
136
+ Comparison:
137
+ jv: 192.0 i/s
138
+ jbuilder: 64.8 i/s - 2.96x slower
139
+
140
+
141
+ * Rendering 1000 partials via render_partial
142
+ Warming up --------------------------------------
143
+ jbuilder 1.000 i/100ms
144
+ jv 2.000 i/100ms
145
+ Calculating -------------------------------------
146
+ jbuilder 6.645 (±15.0%) i/s - 33.000 in 5.001550s
147
+ jv 23.705 (± 8.4%) i/s - 118.000 in 5.000942s
148
+
149
+ Comparison:
150
+ jv: 23.7 i/s
151
+ jbuilder: 6.6 i/s - 3.57x slower
152
+ ```
153
+
154
+ ## Development
155
+
156
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
157
+
158
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
159
+
160
+ ## Contributing
161
+
162
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jv.
163
+
164
+ ## License
165
+
166
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jv"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "jv/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jv"
8
+ spec.version = Jv::VERSION
9
+ spec.authors = ["Makoto Tajitsu"]
10
+ spec.email = ["makoto_tajitsu@hotmail.co.jp"]
11
+
12
+ spec.summary = 'Rails JSON View Handler'
13
+ spec.description = 'Rails JSON View Handler'
14
+ spec.homepage = "https://github.com/tzmfreedom/jv"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "rails"
28
+ end
@@ -0,0 +1,2 @@
1
+ require "jv/version"
2
+ require 'jv/railtie'
@@ -0,0 +1,18 @@
1
+ require 'jv/view'
2
+ require 'jv/render'
3
+
4
+ module Jv
5
+ class Handler
6
+ cattr_accessor :default_format
7
+ self.default_format = Mime[:json]
8
+
9
+ def self.call(template)
10
+ <<~SRC
11
+ r ||= Jv::Render.new(self)
12
+ _partial ||= nil
13
+ hash = ->{ #{template.source} }.()
14
+ _partial.nil? ? hash.to_json : Jv::View.new(hash)
15
+ SRC
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ require 'jv/handler'
2
+
3
+ module Jv
4
+ class Railtie < Rails::Railtie
5
+ initializer :jv do
6
+ ActiveSupport.on_load :action_view do
7
+ ActionView::Template.register_template_handler :jv, ::Jv::Handler
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ module Jv
2
+ class Render
3
+ def initialize(context)
4
+ @context = context
5
+ end
6
+
7
+ def call(options)
8
+ render_collection = options[:collection].present?
9
+ options[:locals] ||= {}
10
+ options[:locals].merge!(r: self, _partial: true) if render_collection
11
+ json = @context.render options
12
+ if render_collection
13
+ json.prepend('[')
14
+ json[-1] = ']'
15
+ end
16
+ JSON.load(json)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Jv
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ module Jv
2
+ class View
3
+ def initialize(hash)
4
+ @hash = hash
5
+ end
6
+
7
+ def to_s
8
+ "#{@hash.to_json},"
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Makoto Tajitsu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
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
+ description: Rails JSON View Handler
70
+ email:
71
+ - makoto_tajitsu@hotmail.co.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - jv.gemspec
86
+ - lib/jv.rb
87
+ - lib/jv/handler.rb
88
+ - lib/jv/railtie.rb
89
+ - lib/jv/render.rb
90
+ - lib/jv/version.rb
91
+ - lib/jv/view.rb
92
+ homepage: https://github.com/tzmfreedom/jv
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.11
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Rails JSON View Handler
116
+ test_files: []