dominate 0.6.1 → 0.6.2

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: e395d2086436cdb2ce8e73ff7e8b4ff3bdb33975
4
- data.tar.gz: 97061dbbb9e56558721bc9cd3266aec83b15c14b
3
+ metadata.gz: 72d9211bc986aa60662aca31c47f9d21079012ca
4
+ data.tar.gz: b4134caf0f64afff8dd8586e92f8120f3748694f
5
5
  SHA512:
6
- metadata.gz: 5d5f41678d8ff7d2fc9ccf22a72eb62d728696443f9d1bae8cc1c9866e2b80279e5bd757a04e410cfcf4ce78ea43fcde1c2ab2d56a1b636556649b19abe2293b
7
- data.tar.gz: af47481108af632da963b17ea4fe7ba30bc62f11a9055e655e9029a5f8af6391ee43db5b80743675b18249cff7198fe8fe804d54fb4da437bc9e953639d72413
6
+ metadata.gz: dbd70fd7b2e6d7ef293c3c3af0563427a7a49753eccb7b25a331a69906a5ca1d49ecb41a471ac180ff1ab70469fa1b4b89641d04c0f4cd456eea1adac3e68e4b
7
+ data.tar.gz: bf9a7234075c749d8938b67e8eba81d39b57ca312307ee82e707547e9f5da74b48d085e8625317058a38550b08e1572cdb16d4e91b3babf81e53c08b0195ebf5
data/lib/dominate/dom.rb CHANGED
@@ -18,10 +18,12 @@ module Dominate
18
18
  def load_html
19
19
  load_layout if config.layout
20
20
 
21
- updated_html = doc.inner_html.gsub(PARTIAL_REGEX_WITHIN) do |m|
21
+ inner_html = doc.inner_html
22
+
23
+ updated_html = inner_html.gsub(PARTIAL_REGEX_WITHIN) do |m|
22
24
  match = m.strip.match(PARTIAL_REGEX)
23
25
  partial = match[1]
24
- HTML.load "#{view_path}/#{partial}", config, instance
26
+ HTML.load_file "#{view_path}/#{partial}", config, instance
25
27
  end
26
28
 
27
29
  set_doc updated_html if updated_html
@@ -30,7 +32,7 @@ module Dominate
30
32
  if match = e.to_html.strip.match(PARTIAL_REGEX)
31
33
  partial = match[1]
32
34
  e.swap Nokogiri::HTML.fragment(
33
- HTML.load "#{view_path}/#{partial}", config, instance
35
+ HTML.load_file "#{view_path}/#{partial}", config, instance
34
36
  )
35
37
  end
36
38
  end
@@ -38,7 +40,7 @@ module Dominate
38
40
 
39
41
  def load_layout
40
42
  path = "#{config.view_path}/#{config.layout}"
41
- html = HTML.load path, config, instance
43
+ html = HTML.load_file path, config, instance
42
44
  inner_html = doc.inner_html
43
45
  set_doc html.gsub YIELD_REGEX, inner_html
44
46
  end
@@ -70,18 +72,14 @@ module Dominate
70
72
  Scope.new(instance, config, doc).apply_instance
71
73
  end
72
74
 
75
+ def reset_html
76
+ @html = false
77
+ end
78
+
73
79
  private
74
80
 
75
81
  def set_doc html
76
- if html.match(/<html.*>/)
77
- @doc = Nokogiri::HTML::Document.parse html
78
- else
79
- @doc = Nokogiri::HTML.fragment html
80
- end
81
- end
82
-
83
- def reset_html
84
- @html = false
82
+ @doc = Nokogiri::HTML html
85
83
  end
86
84
 
87
85
  def view_path
data/lib/dominate/html.rb CHANGED
@@ -7,35 +7,47 @@ module Dominate
7
7
  def file file, instance = false, config = {}
8
8
  c = (Dominate.config.to_h.merge config).to_deep_ostruct
9
9
  path = "#{c.view_path}/#{file}"
10
- html = load path, c, instance
11
- dom = Dom.new html, instance, config
12
-
13
- if File.file? path + '.dom'
14
- dom = Instance.new(instance, c).instance_eval File.read(path + '.dom')
15
- end
16
-
10
+ dom = load_file path, c, instance
11
+
12
+ # todo> try https://github.com/ohler55/ox instead
13
+ # dom = Dom.new html, instance, config
14
+ #
15
+ # if File.file? path + '.dom'
16
+ # dom = Instance.new(instance, c).instance_eval File.read(path + '.dom')
17
+ # end
18
+ #
17
19
  dom
18
20
  end
19
21
 
20
- def load path, config, instance
21
- html = false
22
+ def load_file path, config, instance
23
+ html = _cache.fetch(path) {
24
+ template = false
25
+
26
+ VIEW_TYPES.each do |type|
27
+ f = "#{path}.#{type}"
22
28
 
23
- VIEW_TYPES.each do |type|
24
- f = "#{path}.#{type}"
29
+ if File.file? f
30
+ template = Tilt.new f, 1, outvar: '@_output'
31
+ break
32
+ end
33
+ end
25
34
 
26
- if File.file? f
27
- template = Tilt.new f
28
- html = template.render Instance.new(instance, config)
29
- break
35
+ unless template
36
+ raise Dominate::NoFileFound,
37
+ "Could't find file: #{path} with any of these extensions: #{VIEW_TYPES.join(', ')}."
30
38
  end
31
- end
32
39
 
33
- unless html
34
- raise Dominate::NoFileFound,
35
- "Could't find file: #{path} with any of these extensions: #{VIEW_TYPES.join(', ')}."
36
- end
40
+ template
41
+ }.render instance, config.to_h
37
42
 
38
43
  html
39
44
  end
45
+
46
+ # @private Used internally by #render to cache the
47
+ # Tilt templates.
48
+ def _cache
49
+ Thread.current[:_cache] ||= Tilt::Cache.new
50
+ end
51
+ private :_cache
40
52
  end
41
53
  end
@@ -1,24 +1,43 @@
1
- module Dominate
2
- class Instance
3
- def initialize instance, config
4
- @__config__ = config
5
- @__instance__ = instance
1
+ # module Dominate
2
+ # class Instance < SimpleDelegator
3
+ # def initialize instance, config
4
+ # @__config__ = config
5
+ # @__instance__ = instance
6
+ #
7
+ # instance.instance_variables.each do |name|
8
+ # instance_variable_set name, instance.instance_variable_get(name)
9
+ # end
10
+ #
11
+ # instance
12
+ # end
13
+ #
14
+ # def method_missing method, *args, &block
15
+ # if @__config__.respond_to? method
16
+ # @__config__.send method, *args, &block
17
+ # elsif @__instance__.respond_to? method
18
+ # @__instance__.send method, *args, &block
19
+ # else
20
+ # @__instance__.send 'method_missing', method, *args, &block
21
+ # end
22
+ # end
23
+ # end
24
+ # end
25
+
26
+ require 'delegate'
6
27
 
28
+ module Dominate
29
+ class Instance < SimpleDelegator
30
+ def initialize instance, locals = {}
7
31
  instance.instance_variables.each do |name|
8
32
  instance_variable_set name, instance.instance_variable_get(name)
9
33
  end
10
34
 
11
- instance
12
- end
13
-
14
- def method_missing method, *args, &block
15
- if @__config__.respond_to? method
16
- @__config__.send method, *args, &block
17
- elsif @__instance__.respond_to? method, *args, &block
18
- @__instance__.send method, *args, &block
19
- else
20
- @__instance__.send 'method_missing', method, *args, &block
35
+ locals.to_h.each do |key, value|
36
+ (class << self; self; end).send(:attr_accessor, key.to_sym)
37
+ instance_variable_set("@#{key}", value)
21
38
  end
39
+
40
+ super instance
22
41
  end
23
42
  end
24
43
  end
@@ -1,3 +1,3 @@
1
1
  module Dominate
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.2"
3
3
  end
@@ -32,6 +32,77 @@ module Dominate
32
32
  end
33
33
  end
34
34
 
35
+ def set_state state
36
+ @widget_state = state
37
+ end
38
+
39
+ def reset_state
40
+ @widget_state = false
41
+ end
42
+
43
+ def partial template, locals = {}
44
+ locals[:partial] = template
45
+ resp = render locals
46
+
47
+ if resp.is_a? Dominate::Dom
48
+ resp.html
49
+ else
50
+ resp
51
+ end
52
+ end
53
+
54
+ def render_state options = {}
55
+ state = widget_state || options.delete(:state)
56
+
57
+ if method(state).parameters.length > 0
58
+ resp = send(state, options.to_deep_ostruct)
59
+ else
60
+ resp = send(state)
61
+ end
62
+
63
+ if resp.is_a? Dominate::Dom
64
+ html = "<div id='#{id_for(state)}'>#{resp.html}</div>"
65
+ resp.doc.inner_html = html
66
+ resp.reset_html
67
+ resp.html
68
+ else
69
+ resp
70
+ end
71
+ end
72
+
73
+ def replace state, opts = {}
74
+ if !state.is_a? String
75
+ opts[:state] = state
76
+ content = render_state opts
77
+ selector = '#' + id_for(state)
78
+ else
79
+ if !opts.key?(:content) and !opts.key?(:with)
80
+ opts[:state] = caller[0][/`.*'/][1..-2]
81
+ content = render_state opts
82
+ else
83
+ content = opts[:content] || opts[:with]
84
+ end
85
+ selector = state
86
+ end
87
+
88
+ res.write '$("' + selector + '").replaceWith("' + escape(content) + '");'
89
+ # scroll to the top of the page just as if we went to the url directly
90
+ # if opts[:scroll_to_top]
91
+ # res.write 'window.scrollTo(0, 0);'
92
+ # end
93
+ end
94
+
95
+ def id_for state
96
+ w_name = name.to_s.gsub(/_/, '-')
97
+ w_state = state.to_s.gsub(/_/, '-')
98
+
99
+ "#{w_name}-#{w_state}"
100
+ end
101
+
102
+ def escape js
103
+ js.to_s.gsub(/(\\|<\/|\r\n|\\3342\\2200\\2250|[\n\r"'])/) {|match| JS_ESCAPE[match] }
104
+ end
105
+
35
106
  def trigger widget_event, data = {}
36
107
  widget_name = data.delete(:for)
37
108
 
@@ -62,6 +133,9 @@ module Dominate
62
133
  end
63
134
 
64
135
  if resp.is_a? Dominate::Dom
136
+ html = "<div id='#{id_for(e)}'>#{resp.html}</div>"
137
+ resp.doc.inner_html = html
138
+ resp.reset_html
65
139
  res.write resp.html
66
140
  else
67
141
  resp
@@ -79,7 +153,7 @@ module Dominate
79
153
  locals = args.first
80
154
  # if it's a partial we add an underscore infront of it
81
155
  state = view = locals[:state] ||
82
- "#{locals[:partial]}".gsub(PARTIAL_REGEX, '_\1')
156
+ "#{locals.delete(:partial)}".gsub(PARTIAL_REGEX, '_\1')
83
157
  else
84
158
  state = view = args.first
85
159
  locals = args.length > 1 ? args.last : {}
@@ -45,6 +45,9 @@ module Dominate
45
45
  end
46
46
 
47
47
  if resp.is_a? Dominate::Dom
48
+ html = "<div id='#{widget.id_for(state)}'>#{resp.html}</div>"
49
+ resp.reset_html
50
+ resp.doc.inner_html = html
48
51
  resp.html
49
52
  else
50
53
  resp
@@ -53,6 +56,11 @@ module Dominate
53
56
  # raise "Please add ##{state} to #{widget.class}."
54
57
  # end
55
58
  end
59
+
60
+ def url_for_event event, options = {}
61
+ widget_name = options.delete(:widget_name) || req.env[:widget_name]
62
+ "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) : '')
63
+ end
56
64
  end
57
65
  end
58
66
  end
@@ -6,7 +6,11 @@ module Dominate
6
6
  @app = app
7
7
  end
8
8
 
9
- def call(env)
9
+ def call env
10
+ dup.call! env
11
+ end
12
+
13
+ def call! env
10
14
  @env = env
11
15
 
12
16
  if widget_path
data/test/widget_test.rb CHANGED
@@ -13,9 +13,18 @@ setup do
13
13
  end
14
14
 
15
15
  Cuba.reset!
16
- Cuba.use Dominate::Widget::Middleware
16
+ # Cuba.use Dominate::Widget::Middleware
17
17
  Cuba.plugin Dominate::Widget::Helper
18
18
  Cuba.define do
19
+
20
+ on "widgets" do
21
+ widget_name, widget_event, event = Dominate::Widget.load_all self, req, res
22
+
23
+ event.trigger widget_name, widget_event, req.params
24
+ # res.write "$('head > meta[name=csrf-token]').attr('content', '#{csrf_token}');"
25
+ res.write '$(document).trigger("page:change");'
26
+ end
27
+
19
28
  on "test" do
20
29
  res.write render_widget :some_widget
21
30
  end
@@ -30,13 +39,16 @@ scope 'dominate widget' do
30
39
  'REQUEST_METHOD' => 'GET',
31
40
  'rack.input' => {}
32
41
  })
42
+ body = resp.join
33
43
 
34
- assert resp.join.scan('Hello, World!').length == 2
44
+ assert body.scan('Hello, World!').length == 2
45
+ assert body['some-widget-display']
35
46
  end
36
47
 
37
48
  test 'event' do
38
49
  _, _, resp = Cuba.call({
39
50
  'PATH_INFO' => '/widgets',
51
+ 'SCRIPT_NAME' => '/widgets',
40
52
  'REQUEST_METHOD' => 'GET',
41
53
  'rack.input' => {},
42
54
  'QUERY_STRING' => 'widget_name=some_widget&widget_event=test'
@@ -46,5 +58,7 @@ scope 'dominate widget' do
46
58
  assert body['Hello, World!']
47
59
  assert body['moo']
48
60
  assert body['cow']
61
+ assert body['some-widget-display']
62
+ assert body['some-widget-test']
49
63
  end
50
64
  end
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.1
4
+ version: 0.6.2
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-05 00:00:00.000000000 Z
11
+ date: 2014-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri