dominate 0.7.6 → 0.8.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: cc880a64862f4eb9bffacfb66763b25fa1ba6452
4
- data.tar.gz: 675cd58433124b28376788d3ca01dc83daab1360
3
+ metadata.gz: bb04c61b1d509aa27a58a56ae57c0952dc272ad9
4
+ data.tar.gz: 9bacdf195170b8f86d27e4fb1eff7c2f28149c9e
5
5
  SHA512:
6
- metadata.gz: d2bfc67b7b492515b8e3b27340d49b983bfd9861106f0d29fef1f9d6d1f36c379de8389e764ada262f0f0b73b5fc179d771fecda96a348e35598abd3af693bf3
7
- data.tar.gz: 5e2cde594faf34e217dede5b13a1811392facea3ef9d063275536acd52e0d67fd464cc62b5182fe2e74f922b9f940fed9781b578847cdafc51a57e5add0ff767
6
+ metadata.gz: 6a9973834d2a6b1e0d3344da950e8349657000b43b1b10115fc24e8a3d425a8168ae8fcf6b2b9489720e36f8752038edaa260186d91bb83a82251d3ee6fcfa11
7
+ data.tar.gz: ba07354f0a6b3d38d522b3053a15706155160c31ddf71d2c2f63133ef958374677c423f65a4e381fa6554136ece59d90e605a4ef7c2562c32f94d6e1307bd6eb
data/.gems ADDED
@@ -0,0 +1,6 @@
1
+ tilt -v 2.0.1
2
+ eventable -v 0.1.4
3
+ slim -v 2.0.3
4
+ cuba -v 3.1.1
5
+ nokogiri -v 1.6.2.1
6
+ nokogiri-styles -v 0.1.2
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .gs
19
+ .env
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p481
data/Makefile ADDED
@@ -0,0 +1,28 @@
1
+ .PHONY: install test server
2
+
3
+ define install_bs
4
+ which bs || (wget https://raw.githubusercontent.com/educabilia/bs/master/bin/bs && chmod +x bs && sudo mv bs /usr/local/bin)
5
+
6
+ @if [ -s .gs ]; then \
7
+ true; \
8
+ else \
9
+ mkdir .gs; \
10
+ touch .env; \
11
+ echo 'GEM_HOME=$$(pwd)/.gs' >> .env; \
12
+ echo 'GEM_PATH=$$(pwd)/.gs' >> .env; \
13
+ echo 'PATH=$$(pwd)/.gs/bin:$$PATH' >> .env; \
14
+ echo 'RACK_ENV=development' >> .env; \
15
+ fi;
16
+
17
+ bs gem install dep
18
+ bs gem install cutest-cj
19
+ bs gem install pry
20
+ bs gem install awesome_print
21
+ endef
22
+
23
+ install:
24
+ $(call install_bs)
25
+ bs dep install
26
+
27
+ test:
28
+ bs env $$(cat .env.test) cutest test/**/*_test.rb
@@ -0,0 +1,124 @@
1
+ require 'rack/mime'
2
+
3
+ module Dominate
4
+ module Assets
5
+ class Middleware
6
+ STATIC_TYPES = %w(js css)
7
+
8
+ attr_reader :app, :env, :res
9
+
10
+ def initialize(app)
11
+ @app = app
12
+ end
13
+
14
+ def call env
15
+ dup.call! env
16
+ end
17
+
18
+ def call! env
19
+ @env = env
20
+
21
+ if assets_path
22
+ render_assets
23
+ else
24
+ res
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def res
31
+ @res ||= begin
32
+ if not assets_path
33
+ app.call(req.env)
34
+ else
35
+ Cuba::Response.new
36
+ end
37
+ end
38
+ end
39
+
40
+ def req
41
+ @req ||= Rack::Request.new env
42
+ end
43
+
44
+ def assets_path
45
+ path[Regexp.new("^#{Dominate.config.asset_url}($|.*)")]
46
+ end
47
+
48
+ def path
49
+ env['PATH_INFO']
50
+ end
51
+
52
+ def type
53
+ if matched = path.match(/(?<=#{Dominate.config.asset_url}\/).*(?=\/)/)
54
+ matched[0]
55
+ else
56
+ ''
57
+ end
58
+ end
59
+
60
+ def name
61
+ cleaned = path.gsub(/\.#{ext}$/, '')
62
+ cleaned = cleaned.gsub(/^#{Dominate.config.asset_url}\//, '')
63
+ cleaned = cleaned.gsub(/^#{type}\//, '')
64
+ cleaned
65
+ end
66
+
67
+ def ext
68
+ if matched = path.match(/(?<=\.).*$/)
69
+ matched[0]
70
+ else
71
+ false
72
+ end
73
+ end
74
+
75
+ def render_assets
76
+ if name == 'all'
77
+ res.write render_all_files
78
+ else
79
+ res.write render_single_file
80
+ end
81
+
82
+ case type
83
+ when 'css', 'stylesheet', 'stylesheets'
84
+ content_type = 'text/css'
85
+ when 'js', 'javascript', 'javascripts'
86
+ content_type = 'text/javascript'
87
+ else
88
+ content_type = Rack::Mime.mime_type ext
89
+ end
90
+
91
+ res.headers["Content-Type"] = content_type
92
+
93
+ res.finish
94
+ end
95
+
96
+ def render_all_files
97
+ content = ''
98
+ files = Dominate.config.assets[ext]
99
+ path = "#{Dominate.config.asset_path}/#{type}"
100
+
101
+ files.each do |file|
102
+ content += load_file "#{path}/#{file}"
103
+ end
104
+
105
+ content
106
+ end
107
+
108
+ def render_single_file
109
+ path = "#{Dominate.config.asset_path}/#{type}"
110
+ load_file "#{path}/#{name}.#{ext}"
111
+ end
112
+
113
+ def load_file path
114
+ ext = path[/\.[^.]*$/][1..-1]
115
+
116
+ if STATIC_TYPES.include? ext
117
+ File.read path
118
+ else
119
+ Dominate::HTML.load_file path
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,44 @@
1
+ module Dominate
2
+ module Assets
3
+ module Render
4
+ PARTIAL_REGEX = Regexp.new '([a-zA-Z_]+)$'
5
+
6
+ def self.setup app
7
+ app.settings[:render] ||= {}
8
+ app.use Middleware
9
+
10
+ load_engines
11
+ end
12
+
13
+ def render file, options = {}
14
+ path = "#{settings[:render][:views] || Dominate.config.view_path}"
15
+ layout_path = settings[:layout_path] || Dominate.config.layout_path
16
+ layout = "#{layout_path}/#{settings[:render][:layout] || Dominate.config.layout}"
17
+ content = Dominate::HTML.load_file "#{path}/#{file}", options, self
18
+ options[:content] = content
19
+ Dominate::HTML.load_file layout, options, self
20
+ end
21
+
22
+ def partial file, options = {}
23
+ file.gsub! PARTIAL_REGEX, '_\1'
24
+ path = "#{settings[:render][:views] || Dominate.config.view_path}"
25
+ Dominate::HTML.load_file "#{path}/#{file}", options, self
26
+ end
27
+
28
+ private
29
+
30
+ def self.load_engines
31
+ if defined? Slim
32
+ Slim::Engine.set_default_options \
33
+ disable_escape: true,
34
+ use_html_safe: true,
35
+ disable_capture: false
36
+
37
+ if RACK_ENV == 'development'
38
+ Slim::Engine.set_default_options pretty: true
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,6 @@
1
+ module Dominate
2
+ module Assets
3
+ autoload :Middleware, 'dominate/assets/middleware'
4
+ autoload :Render, 'dominate/assets/render'
5
+ end
6
+ end
data/lib/dominate/dom.rb CHANGED
@@ -26,23 +26,28 @@ module Dominate
26
26
  HTML.load_file "#{view_path}/#{partial}", config, instance
27
27
  end
28
28
 
29
- set_doc updated_html if updated_html
30
-
31
- doc.traverse do |e|
32
- if match = e.to_html.strip.match(PARTIAL_REGEX)
33
- partial = match[1]
34
- e.swap Nokogiri::HTML.fragment(
35
- HTML.load_file "#{view_path}/#{partial}", config, instance
36
- )
37
- end
29
+ updated_html = updated_html.gsub(PARTIAL_REGEX) do |m|
30
+ partial = $~.captures.first
31
+ HTML.load_file "#{view_path}/#{partial}", config, instance
38
32
  end
33
+
34
+ set_doc updated_html
35
+
36
+ # doc.traverse do |e|
37
+ # if match = e.to_html.strip.match(PARTIAL_REGEX)
38
+ # partial = match[1]
39
+ # e.swap Nokogiri::HTML.fragment(
40
+ # HTML.load_file "#{view_path}/#{partial}", config, instance
41
+ # )
42
+ # end
43
+ # end
39
44
  end
40
45
 
41
46
  def load_layout
42
47
  path = "#{config.view_path}/#{config.layout}"
43
48
  html = HTML.load_file path, config, instance
44
49
  inner_html = doc.inner_html
45
- set_doc html.gsub YIELD_REGEX, inner_html
50
+ doc.inner_html = html.gsub YIELD_REGEX, inner_html
46
51
  end
47
52
 
48
53
  def scope name, &block
data/lib/dominate/html.rb CHANGED
@@ -7,28 +7,39 @@ 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
- 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
- #
19
- dom
10
+ html = load_file path, c, instance
11
+
12
+ if c.parse_dom
13
+ unless dom_cache = _dom_cache[path]
14
+ dom_cache = (_dom_cache[path] = Dom.new(html, instance, config))
15
+ end
16
+
17
+ dom = dom_cache.dup
18
+
19
+ if File.file? path + '.dom'
20
+ dom = Instance.new(instance, c).instance_eval File.read(path + '.dom')
21
+ end
22
+
23
+ dom
24
+ else
25
+ html
26
+ end
20
27
  end
21
28
 
22
- def load_file path, c, instance
29
+ def load_file path, c = {}, instance = self
23
30
  html = _cache.fetch(path) {
24
31
  template = false
25
32
 
26
- VIEW_TYPES.each do |type|
27
- f = "#{path}.#{type}"
33
+ if path[/\..*$/] && File.file?(path)
34
+ template = Tilt.new path, 1, outvar: '@_output'
35
+ else
36
+ VIEW_TYPES.each do |type|
37
+ f = "#{path}.#{type}"
28
38
 
29
- if File.file? f
30
- template = ::Tilt.new f, 1, outvar: '@_output'
31
- break
39
+ if File.file? f
40
+ template = Tilt.new f, 1, outvar: '@_output'
41
+ break
42
+ end
32
43
  end
33
44
  end
34
45
 
@@ -43,11 +54,16 @@ module Dominate
43
54
  html
44
55
  end
45
56
 
57
+ private
58
+
46
59
  # @private Used internally by #render to cache the
47
60
  # ::Tilt templates.
48
61
  def _cache
49
62
  Thread.current[:_cache] ||= ::Tilt::Cache.new
50
63
  end
51
- private :_cache
64
+
65
+ def _dom_cache
66
+ Thread.current[:_dom_cache] ||= {}
67
+ end
52
68
  end
53
69
  end
@@ -1,3 +1,3 @@
1
1
  module Dominate
2
- VERSION = "0.7.6"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -44,12 +44,13 @@ module Dominate
44
44
  resp = widget.send state
45
45
  end
46
46
 
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
51
- # resp.html
52
- if resp.is_a? String
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
51
+ # resp.html
52
+ html
53
+ elsif resp.is_a? String
53
54
  html = "<div id='#{widget.id_for(state)}'>#{resp}</div>"
54
55
  html
55
56
  else
@@ -58,12 +58,13 @@ module Dominate
58
58
  resp = send(state)
59
59
  end
60
60
 
61
- # if resp.is_a? Dominate::Dom
62
- # html = "<div id='#{id_for(state)}'>#{resp.html}</div>"
63
- # resp.doc.inner_html = html
64
- # resp.reset_html
65
- # resp.html
66
- if resp.is_a? String
61
+ if resp.is_a? Dominate::Dom
62
+ html = "<div id='#{id_for(state)}'>#{resp.html}</div>"
63
+ # resp.doc.inner_html = html
64
+ # resp.reset_html
65
+ # resp.html
66
+ html
67
+ elsif resp.is_a? String
67
68
  html = "<div id='#{id_for(state)}'>#{resp}</div>"
68
69
  html
69
70
  else
@@ -143,12 +144,13 @@ module Dominate
143
144
  resp = send(e)
144
145
  end
145
146
 
146
- # if resp.is_a? Dominate::Dom
147
- # html = "<div id='#{id_for(e)}'>#{resp.html}</div>"
148
- # resp.doc.inner_html = html
149
- # resp.reset_html
150
- # res.write resp.html
151
- if resp.is_a? String
147
+ if resp.is_a? Dominate::Dom
148
+ html = "<div id='#{id_for(e)}'>#{resp.html}</div>"
149
+ # resp.doc.inner_html = html
150
+ # resp.reset_html
151
+ # res.write resp.html
152
+ res.write html
153
+ elsif resp.is_a? String
152
154
  html = "<div id='#{id_for(e)}'>#{resp}</div>"
153
155
  res.write html
154
156
  else
data/lib/dominate.rb CHANGED
@@ -12,6 +12,7 @@ module Dominate
12
12
  autoload :Scope, "dominate/scope"
13
13
  autoload :Dom, "dominate/dom"
14
14
  autoload :Widget, "dominate/widget"
15
+ autoload :Assets, "dominate/assets"
15
16
 
16
17
  class NoFileFound < StandardError; end
17
18
 
@@ -28,11 +29,19 @@ module Dominate
28
29
  # Resets the configuration to the default (empty hash)
29
30
  def reset_config!
30
31
  @config = OpenStruct.new({
31
- view_path: './views',
32
+ parse_dom: false,
32
33
  layout: 'app',
34
+ layout_path: './views/layouts',
35
+ view_path: './views',
33
36
  widget_path: './widgets',
34
37
  widget_url: '/widgets',
35
- widgets: {}
38
+ widgets: {},
39
+ assets: OpenStruct.new({
40
+ js: {},
41
+ css: {}
42
+ }),
43
+ asset_url: '/assets',
44
+ asset_path: './assets'
36
45
  })
37
46
  end
38
47
 
data/test/helper.rb CHANGED
@@ -1,2 +1,5 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
1
4
  require 'dominate'
2
5
  require 'slim'
@@ -5,6 +5,7 @@ setup do
5
5
  Dominate.setup do |c|
6
6
  c.view_path = './test/dummy'
7
7
  c.layout = "app"
8
+ c.parse_dom = true
8
9
  end
9
10
 
10
11
  inline_html = File.read './test/dummy/index.html'
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.7.6
4
+ version: 0.8.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-07-08 00:00:00.000000000 Z
11
+ date: 2014-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -171,13 +171,18 @@ executables: []
171
171
  extensions: []
172
172
  extra_rdoc_files: []
173
173
  files:
174
+ - .gems
174
175
  - .gitignore
175
- - Gemfile
176
+ - .ruby-version
176
177
  - LICENSE.txt
178
+ - Makefile
177
179
  - README.md
178
180
  - Rakefile
179
181
  - dominate.gemspec
180
182
  - lib/dominate.rb
183
+ - lib/dominate/assets.rb
184
+ - lib/dominate/assets/middleware.rb
185
+ - lib/dominate/assets/render.rb
181
186
  - lib/dominate/dom.rb
182
187
  - lib/dominate/html.rb
183
188
  - lib/dominate/inflectors.rb
@@ -239,4 +244,3 @@ test_files:
239
244
  - test/helper.rb
240
245
  - test/template_test.rb
241
246
  - test/widget_test.rb
242
- has_rdoc:
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in dominate.gemspec
4
- gemspec