dominate 0.6.4 → 0.7.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
2
  SHA1:
3
- metadata.gz: 94bfaabd26dfb0e6fc431562da55303563361f5b
4
- data.tar.gz: 75872c48e58ac4dc358db70b52a77330f2c03a71
3
+ metadata.gz: 6ca8662354db26898388756f0313d7c2c2966ebd
4
+ data.tar.gz: e5f26d9caf945d90c0e80e4e809cc4439ef3d62a
5
5
  SHA512:
6
- metadata.gz: 49dca0e0ff8c89ebedbe518631e1d60e595783e5b1bcf44ac5e064c989b90ef57310ac6962d58d28c83496b55a35594eba60e281939a138a37d52a8b5a24b52b
7
- data.tar.gz: 8e5f6868794dfc20474ec7544e7e327b74132ddafb04511f79398ec96a95bb3699fc479904d9aa5ddb0c99d3673f0e197579207bac1954db1f9a632a3cea4482
6
+ metadata.gz: 25fd968a2f8c1d1e62b50ce4af62b7c012dbdd99a75c28f78dedbed63423a0f49ec631a8ed016701400b045f3471f301ed378582d6f69018828c0bdfb2e5d6cd
7
+ data.tar.gz: 5c651fd9fd8f536c53bd5c1c4804a0ad8a24d9f8617b7f0a13ec0f3e4c0bdb947dfdb25e79eb22bdb9ccc9b896dbb7ae61056b2cb8d90ca02db3b30a5abdded2
data/dominate.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_dependency "nokogiri"
22
22
  spec.add_dependency "tilt"
23
23
  spec.add_dependency "nokogiri-styles"
24
+ spec.add_dependency "eventable"
24
25
 
25
26
  spec.add_development_dependency "bundler", "~> 1.3"
26
27
  spec.add_development_dependency "rake"
@@ -1,3 +1,3 @@
1
1
  module Dominate
2
- VERSION = "0.6.4"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -1,16 +1,63 @@
1
- require "observer"
1
+ require "eventable"
2
+
3
+ # http://t-a-w.blogspot.com/2010/05/very-simple-parallelization-with-ruby.html
4
+ class ThreadUtility
5
+ def self.with_connection(&block)
6
+ begin
7
+ yield block
8
+ rescue Exception => e
9
+ raise e
10
+ ensure
11
+ # Check the connection back in to the connection pool
12
+ if defined? ActiveRecord
13
+ ActiveRecord::Base.connection.close if ActiveRecord::Base.connection
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ module Eventable
20
+ # When the event happens the class where it happens runs this
21
+ def fire_event(event, *return_value, &block)
22
+ check_mutex
23
+ threads = []
24
+
25
+ @eventable_mutex.synchronize {
26
+
27
+ return false unless @callbacks && @callbacks[event] && !@callbacks[event].empty?
28
+
29
+ @callbacks[event].each do |listener_id, callbacks|
30
+ begin
31
+ listener = ObjectSpace._id2ref(listener_id)
32
+ callbacks.each do |callback|
33
+ threads << Thread.new do
34
+ ThreadUtility.with_connection do
35
+ listener.send callback, *return_value, &block
36
+ end
37
+ end
38
+ end
39
+ rescue RangeError => re
40
+ # Don't bubble up a missing recycled object, I don't care if it's not there, I just won't call it
41
+ raise re unless re.message.match(/is recycled object/)
42
+ end
43
+ end
44
+ }
45
+
46
+ threads.map(&:join)
47
+
48
+ true
49
+ end
50
+ end
2
51
 
3
52
  module Dominate
4
53
  class Widget
5
- class Event < Struct.new(:res, :req)
6
- include Observable
54
+ class Event
55
+ include Eventable
7
56
 
8
- def trigger widget_name, widget_event, data = {}
9
- # THIS IS WHAT WILL MAKE SURE EVENTS ARE TRIGGERED
10
- changed
11
- ##################################################
57
+ event :trigger
12
58
 
13
- notify_observers widget_name, widget_event, data.to_deep_ostruct
59
+ def trigger widget_name, widget_event, data = {}
60
+ fire_event :trigger, widget_name, widget_event, data.to_deep_ostruct
14
61
  end
15
62
  end
16
63
  end
@@ -22,7 +22,7 @@ module Dominate
22
22
  end
23
23
 
24
24
  def render_widget *args
25
- Widget.load_all(self, req, res)
25
+ @dominate_widgets ||= Widget.load_all(self, Event.new, req, res)
26
26
 
27
27
  if args.first.kind_of? Hash
28
28
  opts = args.first
@@ -61,7 +61,7 @@ module Dominate
61
61
  end
62
62
 
63
63
  def url_for_event event, options = {}
64
- widget_name = options.delete(:widget_name) || req.env[:widget_name]
64
+ widget_name = options.delete(:widget_name)
65
65
  "http#{req.env['SERVER_PORT'] == '443' ? 's' : ''}://#{req.env['HTTP_HOST']}#{Dominate.config.widget_url}?widget_event=#{event}&widget_name=#{widget_name}" + (options.any?? '&' + URI.encode_www_form(options) : '')
66
66
  end
67
67
  end
@@ -20,8 +20,6 @@ module Dominate
20
20
  self.instance_variables.each do |n|
21
21
  app.instance_variable_set n, self.instance_variable_get(n)
22
22
  end
23
-
24
- event.add_observer self, :trigger_event
25
23
  end
26
24
 
27
25
  def method_missing method, *args, &block
@@ -108,19 +106,16 @@ module Dominate
108
106
 
109
107
  def trigger widget_event, data = {}
110
108
  data = data.to_h
111
- widget_name = data.delete(:for)
109
+ widget_name = data.delete(:for) || req.params['widget_name']
112
110
 
113
- req.env[:loaded_widgets].each do |n, w|
114
- w.trigger_event (widget_name || req.params['widget_name']), widget_event,
115
- data.to_deep_ostruct
116
- end
111
+ event.trigger widget_name, widget_event, data.to_h
117
112
  end
118
113
 
119
114
  def trigger_event widget_name, widget_event, data = {}
120
115
  if class_events = self.class.events
121
116
  class_events.each do |class_event, opts|
122
117
  if class_event.to_s == widget_event.to_s && (
123
- widget_name.to_s == name or
118
+ widget_name.to_s == name.to_s or
124
119
  opts[:for].to_s == widget_name.to_s
125
120
  )
126
121
  if not opts[:with]
@@ -142,7 +137,7 @@ module Dominate
142
137
  # resp.reset_html
143
138
  # res.write resp.html
144
139
  if resp.is_a? String
145
- html = "<div id='#{id_for(e)}'>#{html}</div>"
140
+ html = "<div id='#{id_for(e)}'>#{resp}</div>"
146
141
  res.write html
147
142
  else
148
143
  resp
@@ -155,6 +150,11 @@ module Dominate
155
150
  end
156
151
  end
157
152
 
153
+ def url_for_event event, options = {}
154
+ widget_name = options.delete(:widget_name) || name
155
+ "http#{req.env['SERVER_PORT'] == '443' ? 's' : ''}://#{req.env['HTTP_HOST']}#{Dominate.config.widget_url}?widget_event=#{event}&widget_name=#{widget_name}" + (options.any?? '&' + URI.encode_www_form(options) : '')
156
+ end
157
+
158
158
  def render *args
159
159
  if args.first.kind_of? Hash
160
160
  locals = args.first
@@ -197,9 +197,7 @@ module Dominate
197
197
  class << self
198
198
  attr_accessor :events
199
199
 
200
- def load_all app, req, res
201
- event = Event.new res, req
202
-
200
+ def load_all app, event, req, res
203
201
  if widget_event = req.params["widget_event"]
204
202
  widget_name = req.params["widget_name"]
205
203
  end
@@ -207,10 +205,13 @@ module Dominate
207
205
  unless req.env[:loaded_widgets]
208
206
  req.env[:loaded_widgets] ||= {}
209
207
 
210
- Dominate.config.widgets.each do |name, widget|
211
- req.env[:loaded_widgets][name] = Object.const_get(widget).new(
208
+ Dominate.config.widgets.each do |name, widget_class_name|
209
+ widget = Object.const_get(widget_class_name).new(
212
210
  app, res, req, name, event
213
211
  )
212
+ event.register_for_event(event: :trigger, listener: widget, callback: :trigger_event)
213
+
214
+ req.env[:loaded_widgets][name] = widget
214
215
  end
215
216
  end
216
217
 
data/test/widget_test.rb CHANGED
@@ -18,7 +18,9 @@ setup do
18
18
  Cuba.define do
19
19
 
20
20
  on "widgets" do
21
- widget_name, widget_event, event = Dominate::Widget.load_all self, req, res
21
+ @dominate_event ||= Dominate::Widget::Event.new
22
+
23
+ widget_name, widget_event, event = Dominate::Widget.load_all self, @dominate_event, req, res
22
24
 
23
25
  event.trigger widget_name, widget_event, req.params
24
26
  # res.write "$('head > meta[name=csrf-token]').attr('content', '#{csrf_token}');"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dominate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-12 00:00:00.000000000 Z
11
+ date: 2014-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: eventable
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'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement