spread_beaver 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 44dcd6c05da8f54db561229e1656865172be4d9e
4
+ data.tar.gz: bbd1b31028cbe167fa9cae3441dd62eab5162d2c
5
+ SHA512:
6
+ metadata.gz: 103d3a26a8d6aab84d105c3e594e2f7152283d0a0a122de0a51d5f9326b01dcc2621f7e2bc389d98b20526a2009e3bbcc5f453d387ba62c57cf58f6888a8d4b8
7
+ data.tar.gz: 986c790139c16dc2546846318943778678cda0cad5fe670b3d646e6cceacb9a2d35d5ea06ead47ac285dacabadb1a179f1d3e61bd11a61c490dc236a5e5ded47
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 shinya takahashi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # SpreadBeaver
2
+ Server side Javascript runner in Ruby on Rails.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'spread_beaver'
10
+ ```
11
+
12
+ ## Getting Started
13
+
14
+ ### Edit config/application.rb
15
+ ```
16
+ # These are the defaults if you dont specify any yourself
17
+ config.spread_beaver.pool_size = 10 #ExecJS instance size
18
+ config.spread_beaver.timeout = 20 #ExecJS timeout
19
+ config.spread_beaver.bundle = "server-side-bundle.js" #Javascript file placed at under app/assets/javascripts
20
+ ```
21
+
22
+ ### Create config/initializers/spread_beaver.rb
23
+ ```
24
+ module SpreadBeaver
25
+ class Runner
26
+ # Execute javascript in javascript context before every SpreadBeaver::Runner.exec
27
+ def initialize_script
28
+ "clearAllStores();"
29
+ end
30
+
31
+ # Execute queued javascript in javascript context before every SpreadBeaver::Runner.exec
32
+ def exec_queued_module module_name, props
33
+ "#{module_name}.init(#{props_to_s(props)});"
34
+ end
35
+
36
+ # Execute javascript by SpreadBeaver::Runner.exec
37
+ def exec_module module_name, props
38
+ "#{module_name}.render(#{props_to_s(props)});"
39
+ end
40
+ end
41
+ end
42
+ ```
43
+
44
+ ## API
45
+
46
+ - **SpreadBeaver::Runner.queue(module_name, props)**:
47
+ Push hash that has passed values to request store.
48
+ Execute `exec_queued_module` with stored hash data.
49
+
50
+ - **SpreadBeaver::Runner.exec(module_name, props)**:
51
+ Execute `exec_module` with connection pooling.
52
+
53
+ ## Example
54
+ Rails, Browserify, React, Flux, Server side rendering example.
55
+ Go to https://github.com/oreshinya/spread_beaver/tree/master/example !
@@ -0,0 +1,9 @@
1
+ require "active_support/core_ext/class/attribute_accessors"
2
+ require "active_support/core_ext/string/output_safety"
3
+ require "connection_pool"
4
+ require "execjs"
5
+ require "request_store"
6
+
7
+ require "spread_beaver/version"
8
+ require "spread_beaver/runner"
9
+ require "spread_beaver/railtie"
@@ -0,0 +1,27 @@
1
+ require "rails"
2
+
3
+ module SpreadBeaver
4
+ class Railtie < ::Rails::Railtie
5
+ config.spread_beaver = ActiveSupport::OrderedOptions.new
6
+ config.spread_beaver.pool_size = 10
7
+ config.spread_beaver.timeout = 20
8
+ config.spread_beaver.bundle = "server-side-bundle.js"
9
+
10
+ initializer "spread_beaver.add_watchable_files", group: :all do |app|
11
+ watch_file = "#{Rails.root}/app/assets/javascripts/#{app.config.spread_beaver.bundle}"
12
+ app.config.watchable_files.push watch_file
13
+ end
14
+
15
+ config.after_initialize do |app|
16
+ cnf = config.spread_beaver
17
+ setup = -> {
18
+ SpreadBeaver::Runner.setup!(
19
+ app.assets[cnf.bundle].to_s,
20
+ {size: cnf[:pool_size], timeout: cnf[:timeout]}
21
+ )
22
+ }
23
+ setup.call
24
+ ActionDispatch::Reloader.to_prepare(&setup)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,71 @@
1
+ module SpreadBeaver
2
+ class Runner
3
+ QUEUE_KEY = :_spread_beaver_queue
4
+
5
+ cattr_accessor :pool
6
+
7
+ def self.setup! bundle, opts={size: 10, timeout: 20}
8
+ @@bundle = bundle
9
+ @@pool.shutdown{} if @@pool
10
+ @@pool = ConnectionPool.new(opts) { self.new }
11
+ end
12
+
13
+ def self.exec module_name, props
14
+ @@pool.with do |r|
15
+ r.exec module_name, props
16
+ end
17
+ end
18
+
19
+ def self.queue module_name, props
20
+ if RequestStore.store[QUEUE_KEY].nil?
21
+ RequestStore.store[QUEUE_KEY] = []
22
+ end
23
+ RequestStore.store[QUEUE_KEY].push module_name: module_name, props: props
24
+ end
25
+
26
+ def exec module_name, props
27
+ js_code = "(function() {"
28
+ js_code += initialize_script if !initialize_script.nil?
29
+ if !RequestStore.store[self.class::QUEUE_KEY].nil?
30
+ RequestStore.store[self.class::QUEUE_KEY].each do |store|
31
+ str = exec_queued_module store[:module_name], store[:props]
32
+ js_code += str if !str.nil?
33
+ end
34
+ end
35
+ js_code += "return #{exec_module module_name, props}"
36
+ js_code += "})()"
37
+ context.eval(js_code).html_safe
38
+ end
39
+
40
+ private
41
+
42
+ def context
43
+ @context ||= ExecJS.compile <<-JS
44
+ var global = {};
45
+ #{@@bundle}
46
+ JS
47
+ end
48
+
49
+ def initialize_script
50
+ #Override this.
51
+ nil
52
+ end
53
+
54
+ def exec_queued_module module_name, props
55
+ #Override this.
56
+ nil
57
+ end
58
+
59
+ def exec_module module_name, props
60
+ raise "Override this."
61
+ end
62
+
63
+ def props_to_s props
64
+ if props.is_a? String
65
+ props
66
+ else
67
+ props.to_json
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module SpreadBeaver
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spread_beaver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - shinya takahashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-04 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: '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: connection_pool
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: execjs
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: request_store
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
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: rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '4.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '4.0'
111
+ description: Server side Javascript runner in Ruby on Rails.
112
+ email:
113
+ - s.takahashi313@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - LICENSE.txt
119
+ - README.md
120
+ - lib/spread_beaver.rb
121
+ - lib/spread_beaver/railtie.rb
122
+ - lib/spread_beaver/runner.rb
123
+ - lib/spread_beaver/version.rb
124
+ homepage: https://github.com/oreshinya/spread_beaver
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.4.5
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Server side Javascript runner in Ruby on Rails.
148
+ test_files: []