opal-connect 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b52883ea798fba1bc5ef8e7e3b77083fe082634e
4
- data.tar.gz: 055c956583030f66affcd36a84e96f0cb498d0d6
3
+ metadata.gz: 3a72b12d975226cbaf9f606a275f479652f7f5fd
4
+ data.tar.gz: bc02721d66f97dc7dd5393e18fca455774e47c32
5
5
  SHA512:
6
- metadata.gz: 872c395c0bdfbc176ee10d55374318f94ef063151af77143135f6d600c88e79651bebfb15d0c3d5f41406e1bd40a0ebf40834fdb1df052791f04f3959e3a477e
7
- data.tar.gz: 88a527535fc13204d3d95588fd6f56692f405ee1b27a42d786da5e4d7cd565eb1f3ccce3ff7e36555081bed5db53bd92b06a4da0b4cc9edfe307288c0b221915
6
+ metadata.gz: 8da7fefa7c29860141b0cddcdb89a2f9b8e8e601277eecafa39df1b77d02c3f44743001a5751f0f16bb1cb02d8d684229eb5fb5cd6ff1929cba9f670f6b61e87
7
+ data.tar.gz: 384e1482e818813dac36b0b0940761fc0ffce26099407d57677458b40b0b210e77cf6fb90d6915621c590f707b31bce7a03fd2d09b11657ab53e7cecb42488dc
@@ -9,19 +9,19 @@ module Opal
9
9
  end
10
10
 
11
11
  def self.configure(connect, options = false)
12
- if options
13
- unless RUBY_ENGINE == 'opal'
14
- Connect::CLIENT_OPTIONS << 'current_user'
15
- connect.options[:plugin_requires] << options[:class_path]
16
- require options[:class_path]
17
- end
12
+ return unless options
18
13
 
19
- connect.options[:current_user] = options
14
+ unless RUBY_ENGINE == 'opal'
15
+ Connect::CLIENT_OPTIONS << 'current_user'
16
+ connect.options[:plugin_requires] << options[:class_path]
17
+ require options[:class_path]
20
18
  end
19
+
20
+ connect.options[:current_user] = options
21
21
  end
22
22
 
23
23
  ConnectJavascript = -> do
24
- "$current_user = Base64.decode64('#{Base64.encode64 current_user.to_h.to_json}')"
24
+ "$current_user = JSON.parse Base64.decode64('#{Base64.encode64 current_user.to_h.to_json}')"
25
25
  end
26
26
 
27
27
  module InstanceMethods
@@ -40,17 +40,17 @@ module Opal
40
40
  if !block_given?
41
41
  HTML::DSL.html(&scope).to_html
42
42
  else
43
- HTML::DSL.scope!(scope).html(&block).to_html
43
+ HTML::DSL.scope!(scope).html!(&block).to_html
44
44
  end
45
45
  end
46
46
 
47
47
  def dom(selector = false)
48
48
  if RUBY_ENGINE == 'opal'
49
49
  selector ||= 'html'
50
- Instance.new selector, cache
50
+ Instance.new selector, cache, self
51
51
  else
52
52
  selector ||= false
53
- @dom ||= Instance.new selector, cache
53
+ @dom ||= Instance.new selector, cache, self
54
54
  end
55
55
  end
56
56
  end
@@ -63,22 +63,21 @@ module Opal
63
63
  def dom(selector = false)
64
64
  if RUBY_ENGINE == 'opal'
65
65
  selector ||= 'html'
66
- Instance.new selector, cache
66
+ Instance.new selector, cache, self
67
67
  else
68
68
  selector ||= cache[:html]
69
- @dom ||= Instance.new selector, cache
69
+ @dom ||= Instance.new selector, cache, self
70
70
  end
71
71
  end
72
72
  end
73
73
 
74
74
  class Instance
75
- RETURN_VALUE_FIELDS = %w'attr prop'
75
+ attr_reader :selector, :cache, :dom, :scope
76
76
 
77
- attr_reader :selector, :cache, :dom
78
-
79
- def initialize(selector, cache)
77
+ def initialize(selector, cache, scope)
80
78
  @selector = selector
81
79
  @cache = cache
80
+ @scope = scope
82
81
 
83
82
  if selector.is_a?(String)
84
83
  if RUBY_ENGINE == 'opal'
@@ -98,12 +97,14 @@ module Opal
98
97
  end
99
98
 
100
99
  def set html
101
- @dom = Instance.new(html, cache)
100
+ @dom = Instance.new(html, cache, scope)
101
+ @dom.save!
102
+ @dom
102
103
  end
103
104
  alias set! set
104
105
 
105
106
  def load html
106
- Instance.new(html, cache)
107
+ Instance.new(html, cache, scope)
107
108
  end
108
109
  alias load! load
109
110
 
@@ -129,7 +130,20 @@ module Opal
129
130
  end
130
131
  end
131
132
 
132
- if RUBY_ENGINE != 'opal'
133
+ if RUBY_ENGINE == 'opal'
134
+ def on(name, selector = false, &handler)
135
+ if scope.respond_to?(:connect_events_started)
136
+ wrapper = -> (e) do
137
+ scope.connect_events_started(e, name, selector)
138
+ scope.instance_exec(e, &handler)
139
+ end
140
+
141
+ node.on(name, selector, &wrapper)
142
+ else
143
+ node.on(name, selector, &handler)
144
+ end
145
+ end
146
+ else
133
147
  def to_s
134
148
  if dom.respond_to?(:first)
135
149
  dom.first.to_xml
@@ -179,7 +193,7 @@ module Opal
179
193
 
180
194
  def tmpl(name)
181
195
  if cached_tmpl = cache[:"#{name}"]
182
- Instance.new(cached_tmpl, cache)
196
+ Instance.new(cached_tmpl, cache, scope)
183
197
  else
184
198
  puts "There is no template `#{name}`"
185
199
  end
@@ -187,7 +201,7 @@ module Opal
187
201
 
188
202
  def append(content = false, &block)
189
203
  # content becomes scope in this case
190
- content = HTML::DSL.scope!(content).html(&block).to_html if block_given?
204
+ content = HTML::DSL.scope!(content).html!(&block).to_html if block_given?
191
205
 
192
206
  if RUBY_ENGINE == 'opal'
193
207
  node.append(content)
@@ -210,7 +224,7 @@ module Opal
210
224
 
211
225
  def prepend(content = false, &block)
212
226
  # content becomes scope in this case
213
- content = HTML::DSL.scope!(content).html(&block).to_html if block_given?
227
+ content = HTML::DSL.scope!(content).html!(&block).to_html if block_given?
214
228
 
215
229
  if RUBY_ENGINE == 'opal'
216
230
  node.prepend(content)
@@ -233,7 +247,7 @@ module Opal
233
247
 
234
248
  def html(content = false, &block)
235
249
  # content becomes scope in this case
236
- content = HTML::DSL.scope!(content).html(&block).to_html if block_given?
250
+ content = HTML::DSL.scope!(content).html!(&block).to_html if block_given?
237
251
 
238
252
  if RUBY_ENGINE == 'opal'
239
253
  node.html(content)
@@ -265,11 +279,11 @@ module Opal
265
279
  end
266
280
  end
267
281
 
268
- Instance.new(new_node, cache)
282
+ Instance.new(new_node, cache, scope)
269
283
  end
270
284
 
271
285
  def each
272
- node.each { |n| yield Instance.new(n, cache) }
286
+ node.each { |n| yield Instance.new(n, cache, scope) }
273
287
  end
274
288
 
275
289
  def node
@@ -297,7 +311,11 @@ module Opal
297
311
  super
298
312
  end
299
313
 
300
- RETURN_VALUE_FIELDS.include?(method.to_s) ? n : Instance.new(n, cache)
314
+ if RUBY_ENGINE == 'opal'
315
+ n.is_a?(Element) ? Instance.new(n, cache, scope) : n
316
+ else
317
+ n.class.name['Oga::'] ? Instance.new(n, cache, scope) : n
318
+ end
301
319
  end
302
320
  end
303
321
  end
@@ -4,6 +4,13 @@ module Opal
4
4
  module Events
5
5
  $connect_events = ConnectCache.new if RUBY_ENGINE == 'opal'
6
6
 
7
+ module InstanceMethods
8
+ def connect_event_instance_variables(event, _name, _selector)
9
+ # gives you access to this, like jquery
10
+ @this = dom event.current_target
11
+ end
12
+ end
13
+
7
14
  module ClassMethods
8
15
  if RUBY_ENGINE == 'opal'
9
16
  def connect_events
@@ -61,16 +68,19 @@ module Opal
61
68
 
62
69
  events.map! do |event|
63
70
  klass, name, selector, handler = event
64
- c = klass.name == 'Opal::Connect' ? klass : klass.new
65
71
 
66
72
  wrapper = ->(e) do
67
- # gives you access to this, like jquery
68
- c.instance_variable_set(:@this, dom(e.current_target))
73
+ # we want to create an anonymous class so we can use instance
74
+ # methods if events are coming from the Connect module.
75
+ c = klass.name == 'Opal::Connect' \
76
+ ? Class.new { include Opal::Connect }.new \
77
+ : klass.new
78
+ c.connect_event_instance_variables(e, name, selector)
69
79
  c.instance_exec(e, &handler)
70
80
  end
71
81
 
72
- if name.to_s != 'document'
73
- el.on(name, selector, &wrapper)
82
+ if name != :document
83
+ el.dom.on(name, selector, &wrapper)
74
84
  else
75
85
  Document.on(selector, &wrapper)
76
86
  end
@@ -0,0 +1,39 @@
1
+ unless RUBY_ENGINE == 'opal'
2
+ Opal.use_gem 'scrivener-opal'
3
+ end
4
+
5
+ require 'scrivener'
6
+
7
+ module Opal
8
+ module Connect
9
+ class Form < ::Scrivener; end
10
+
11
+ module ConnectPlugins
12
+ module Form
13
+ FORM_EVENTS = %i`submit keydown keyup change`
14
+
15
+ def self.load_dependencies(connect, *args)
16
+ connect.plugin :events
17
+ end
18
+
19
+ module InstanceMethods
20
+ def connect_event_instance_variables(_event, name, _selector)
21
+ super
22
+
23
+ # we only want to grab form params if we are submitting a form
24
+ return unless FORM_EVENTS.include?(name)
25
+
26
+ @params = {}
27
+
28
+ # grab all of the form params!
29
+ Native(@this.serialize_array).each do |item|
30
+ @params[item[:name]] = item[:value]
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ register_plugin :form, Form
37
+ end
38
+ end
39
+ end
@@ -14,8 +14,8 @@ module Opal
14
14
  td textarea tfoot th thead time title tr track tt u ul var video wbr}
15
15
 
16
16
  module InstanceMethods
17
- def html(&block)
18
- HTML::DSL.scope!(self).html(&block).to_html
17
+ def html!(&block)
18
+ HTML::DSL.scope!(self).html!(&block).to_html
19
19
  end
20
20
  end
21
21
 
@@ -66,7 +66,7 @@ module Opal
66
66
  class << self
67
67
  attr_accessor :scope
68
68
 
69
- def html &block
69
+ def html! &block
70
70
  DSL.scope!(scope).new(nil, nil, &block)
71
71
  end
72
72
 
@@ -17,9 +17,23 @@ module Opal
17
17
  Connect.server_methods[self.name] ||= []
18
18
  end
19
19
 
20
- def __server__(method = false, *args, &block)
20
+ def server(method = false, *args, &block)
21
+ if RUBY_ENGINE != 'opal'
22
+ method ||= Module.new(&block)
23
+
24
+ yield if block_given?
25
+
26
+ method.public_instance_methods(false).each do |meth|
27
+ connect_server_methods << meth unless connect_server_methods.include? meth
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ module InstanceMethods
34
+ def server(method, *args)
21
35
  if RUBY_ENGINE == 'opal'
22
- klass_name = self.name
36
+ klass_name = self.class.name
23
37
 
24
38
  if Connect.server_methods[klass_name].include? method
25
39
  promise = Promise.new
@@ -44,23 +58,9 @@ module Opal
44
58
  raise "#{method} is not a server method"
45
59
  end
46
60
  else
47
- method ||= Module.new(&block)
48
-
49
- yield if block_given?
50
-
51
- method.public_instance_methods(false).each do |meth|
52
- connect_server_methods << meth unless connect_server_methods.include? meth
53
- end
61
+ send(method, *args)
54
62
  end
55
63
  end
56
- alias server __server__
57
- end
58
-
59
- module InstanceMethods
60
- def __server__(method, *args)
61
- self.class.server(method, *args)
62
- end
63
- alias server __server__
64
64
  end
65
65
  end
66
66
 
@@ -0,0 +1,41 @@
1
+ module Opal
2
+ module Connect
3
+ class RakeTask
4
+ include Rake::DSL if defined? Rake::DSL
5
+
6
+ DEFAULT_OPTIONS = { port: 8080, host: '0.0.0.0' }
7
+
8
+ def initialize(name = 'webpack', opts = {})
9
+ options = DEFAULT_OPTIONS.merge opts
10
+
11
+ namespace name do
12
+ Opal::Connect.setup
13
+ Opal::Config.dynamic_require_severity = 'ignore'
14
+ Opal.append_path Dir.pwd
15
+
16
+ opal_file_path = "#{Dir.pwd}/.connect/opal.js"
17
+
18
+ unless File.exist? opal_file_path
19
+ builder = Opal::Builder.new
20
+ build_str = '`require("expose?$!expose?jQuery!jquery")`; require "opal"; require "opal-jquery"; require "opal/connect"; require "opal-parser";'
21
+ builder.build_str(build_str, '(inline)', { dynamic_require_severity: :ignore })
22
+ File.write opal_file_path, builder.to_s
23
+ end
24
+
25
+ desc "Start webpack"
26
+ task :run do
27
+ exec({"OPAL_LOAD_PATH" => Opal.paths.join(":")}, "webpack-dev-server --progress -d --host #{options[:host]} --port #{options[:port]} --compress --devtool eval --progress --colors --historyApiFallback true --hot --watch")
28
+ end
29
+
30
+ desc "Build webpack"
31
+ task :build do
32
+ exec({
33
+ "OPAL_LOAD_PATH" => Opal.paths.join(":"),
34
+ "RACK_ENV" => 'production'
35
+ }, 'webpack --progress')
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  module Connect
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
data/lib/opal/connect.rb CHANGED
@@ -8,14 +8,56 @@ if RUBY_ENGINE == 'opal'
8
8
  $console.log(*args)
9
9
  end
10
10
  end
11
+ else
12
+ Opal.append_path File.expand_path('../..', __FILE__).untaint
11
13
  end
12
14
 
13
- # Opal corelib is already loaded from CDN
14
15
  module Opal
15
16
  module Connect
16
17
  CLIENT_OPTIONS = %w'url plugins' unless RUBY_ENGINE == 'opal'
17
18
 
18
19
  class << self
20
+ attr_accessor :pids
21
+
22
+ def run(scope, server, opts = {})
23
+ if ENV['CUTEST']
24
+ scope.run server
25
+ else
26
+ @pids = []
27
+ options = { env: { RACK_ENV: ENV['RACK_ENV']} }.merge opts
28
+
29
+ envs = options[:env].to_a.map { |k, v| "#{k}=#{v}" }.join ' '
30
+ pids << {
31
+ name: 'webpack',
32
+ pid: Process.spawn("#{envs} bundle exec rake webpack:run")
33
+ }
34
+
35
+ if (cutest = Connect.options[:cutest]) && pids.select { |pid| pid[:name] == 'cutest' }.empty?
36
+ envs = cutest[:env].to_a.map { |k, v| "#{k}=#{v}" }.join ' '
37
+ pids << {
38
+ name: 'cutest',
39
+ pid: Process.spawn("#{envs} #{cutest[:run]}")
40
+ }
41
+ end
42
+
43
+ scope.send(:at_exit) { quit_pids }
44
+
45
+ scope.run server
46
+ end
47
+ rescue
48
+ quit_pids
49
+ end
50
+
51
+ def quit_pids
52
+ begin
53
+ while pids.length > 0
54
+ Process.kill "QUIT", (pids.shift)[:pid]
55
+ end
56
+ rescue
57
+ # process already dead
58
+ end
59
+ end
60
+
19
61
  def options
20
62
  @options ||= Connect::ConnectCache.new(
21
63
  hot_reload: false,
@@ -65,14 +107,18 @@ module Opal
65
107
  plugins_path = Connect.options[:plugins_path]
66
108
 
67
109
  if plugins_path && File.exist?("#{plugins_path}/#{name}.rb")
68
- file.puts "require '#{plugins_path}/#{name}'"
110
+ path = "require('#{plugins_path}/#{name}')"
111
+ path = "`#{path}`" if Connect.options[:hot_reload]
112
+ file.puts path
69
113
  else
70
- file.puts "require 'opal/connect/plugins/#{name}'"
114
+ file.puts "require('opal/connect/plugins/#{name}')"
71
115
  end
72
116
  end
73
117
 
74
118
  Connect.options[:plugin_requires].each do |require_path|
75
- file.puts "require '#{require_path}'"
119
+ path = "require('#{require_path}')"
120
+ path = "`#{path}`" if Connect.options[:hot_reload]
121
+ file.puts path
76
122
  end
77
123
  end
78
124
  end
@@ -238,12 +284,14 @@ module Opal
238
284
  javascript = Connect.options[:javascript]
239
285
 
240
286
  if javascript.length
241
- javascript.each do |block|
287
+ javascript.uniq.each do |block|
242
288
  js << klass.instance_exec(&block)
243
289
  end
244
290
  end
245
291
 
246
292
  %{
293
+ #{js.join(';')}
294
+
247
295
  Document.ready? do
248
296
  klass = #{klass.class.name}.new
249
297
 
@@ -251,20 +299,16 @@ module Opal
251
299
  klass.__send__(:#{method}, *JSON.parse(Base64.decode64('#{Base64.encode64 options.to_json}')))
252
300
  end
253
301
 
254
- #{js.join(';')}
255
-
256
302
  Opal::Connect.start_events unless $connect_events_started
257
303
  end
258
304
  }
259
305
  end
260
306
 
261
307
  def write_entry_file(klass = false, method = false, *options)
262
- Opal.use_gem 'opal-jquery'
263
-
264
308
  path = "#{Dir.pwd}/.connect/entry.js"
265
309
 
266
310
  required_files = Connect.files.uniq.map do |file|
267
- !Connect.options[:hot_reload] ? "require('#{file}')" : "`require('#{file}')`"
311
+ "`require('#{file}')`"
268
312
  end.join(';')
269
313
 
270
314
  client_options = Connect.options.hash.select do |key, _|
@@ -272,11 +316,13 @@ module Opal
272
316
  end
273
317
 
274
318
  client_options = Base64.encode64 client_options.to_json
275
- templates = Base64.encode64 Connect.templates.hash.to_json
276
319
 
277
320
  code = "Opal::Connect.options = JSON.parse(Base64.decode64('#{client_options}'));"
278
321
  code = "#{code} Opal::Connect.setup;"
279
- code = "#{code} Opal::Connect.templates = JSON.parse(Base64.decode64('#{templates}'));"
322
+ if Connect.respond_to? :templates
323
+ templates = Base64.encode64 Connect.templates.hash.to_json
324
+ code = "#{code} Opal::Connect.templates = JSON.parse(Base64.decode64('#{templates}'));"
325
+ end
280
326
  code = %{#{code} Opal::Connect.server_methods = JSON.parse(
281
327
  Base64.decode64('#{Base64.encode64 Connect.server_methods.to_json}')
282
328
  );}
@@ -0,0 +1 @@
1
+ require 'opal/connect'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-31 00:00:00.000000000 Z
11
+ date: 2016-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal
@@ -92,15 +92,18 @@ files:
92
92
  - LICENSE.txt
93
93
  - README.md
94
94
  - Rakefile
95
+ - lib/opal-connect.rb
95
96
  - lib/opal/connect.rb
96
97
  - lib/opal/connect/plugins/abilities.rb
97
98
  - lib/opal/connect/plugins/current_user.rb
98
99
  - lib/opal/connect/plugins/dom.rb
99
100
  - lib/opal/connect/plugins/events.rb
101
+ - lib/opal/connect/plugins/form.rb
100
102
  - lib/opal/connect/plugins/html.rb
101
103
  - lib/opal/connect/plugins/pjax.rb
102
104
  - lib/opal/connect/plugins/scope.rb
103
105
  - lib/opal/connect/plugins/server.rb
106
+ - lib/opal/connect/rake_task.rb
104
107
  - lib/opal/connect/version.rb
105
108
  - opal-connect.gemspec
106
109
  homepage: ''
@@ -123,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
126
  version: '0'
124
127
  requirements: []
125
128
  rubyforge_project:
126
- rubygems_version: 2.2.2
129
+ rubygems_version: 2.4.5.1
127
130
  signing_key:
128
131
  specification_version: 4
129
132
  summary: Connects Opal to Ruby.