dominate 0.0.4 → 0.1.0
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 +4 -4
- data/lib/dominate/version.rb +1 -1
- data/lib/dominate.rb +44 -5
- data/test/dominate_test.rb +8 -3
- data/test/dummy/footer.html +3 -0
- data/test/dummy/header.html +3 -0
- data/test/dummy/index.html +26 -22
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 532cf23fe764bdddaef370b9e8142c6da1f6ad45
|
4
|
+
data.tar.gz: 0003456300745421cd8b029c7acecc53a4b85bd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54455c11e4ecf1f018a38f175d22b958f435c9b1452fb16930d108e30f878c8be3263df20f228708902a63574e236e62b8e6076e71886be2423359eb57664c70
|
7
|
+
data.tar.gz: 23a05a39d0af125bd17023bd31d620310ef59d4d295041218904d9cb0cbf8aa3b326df99a3a9ec2141bb19809bde5024476b5cc5c88eacd48a73db89b72bdb97
|
data/lib/dominate/version.rb
CHANGED
data/lib/dominate.rb
CHANGED
@@ -3,19 +3,54 @@ require "dominate/version"
|
|
3
3
|
require "dominate/inflectors"
|
4
4
|
|
5
5
|
module Dominate
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
extend self
|
7
|
+
|
8
|
+
attr_accessor :config, :reset_config, :load_all
|
9
|
+
|
10
|
+
def HTML html, data = {}
|
11
|
+
Dom.new html, data
|
12
|
+
end
|
13
|
+
|
14
|
+
def setup
|
15
|
+
yield config
|
16
|
+
end
|
17
|
+
|
18
|
+
def config
|
19
|
+
@config || reset_config!
|
20
|
+
end
|
21
|
+
|
22
|
+
# Resets the configuration to the default (empty hash)
|
23
|
+
def reset_config!
|
24
|
+
@config = OpenStruct.new
|
10
25
|
end
|
11
26
|
|
12
27
|
class Dom
|
13
28
|
attr_accessor :raw_html, :instance, :doc
|
14
29
|
|
30
|
+
PARTIAL_REGEX = /<!--\s*@partial\s*([a-zA-Z0-9\-_]*)\s*-->/
|
31
|
+
|
15
32
|
def initialize raw_html, instance = false
|
16
33
|
@raw_html = raw_html
|
17
34
|
@instance = instance
|
18
|
-
|
35
|
+
|
36
|
+
if raw_html.match(/<html.*>/)
|
37
|
+
@doc = Nokogiri::HTML::Document.parse raw_html
|
38
|
+
else
|
39
|
+
@doc = Nokogiri::HTML.fragment raw_html
|
40
|
+
end
|
41
|
+
|
42
|
+
load_partials if Dominate.config.view_path
|
43
|
+
end
|
44
|
+
|
45
|
+
def load_partials
|
46
|
+
doc.traverse do |e|
|
47
|
+
if match = e.to_html.strip.match(PARTIAL_REGEX)
|
48
|
+
partial = match[1]
|
49
|
+
e.swap Nokogiri::HTML.fragment(
|
50
|
+
File.read "#{view_path}/#{partial}.html"
|
51
|
+
)
|
52
|
+
end
|
53
|
+
end
|
19
54
|
end
|
20
55
|
|
21
56
|
def scope name
|
@@ -40,6 +75,10 @@ module Dominate
|
|
40
75
|
def reset_html
|
41
76
|
@html = false
|
42
77
|
end
|
78
|
+
|
79
|
+
def view_path
|
80
|
+
Dominate.config.view_path
|
81
|
+
end
|
43
82
|
end
|
44
83
|
|
45
84
|
class Scope < Struct.new :instance, :root_doc
|
data/test/dominate_test.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
require_relative 'helper'
|
2
2
|
require 'dominate'
|
3
3
|
setup do
|
4
|
+
Dominate.reset_config!
|
5
|
+
Dominate.setup do |c|
|
6
|
+
c.view_path = './test/dummy'
|
7
|
+
end
|
8
|
+
|
4
9
|
inline_html = File.read './test/dummy/index.html'
|
5
10
|
|
6
11
|
instance = OpenStruct.new({
|
@@ -17,12 +22,12 @@ setup do
|
|
17
22
|
end
|
18
23
|
|
19
24
|
scope 'dominate' do
|
20
|
-
test '
|
25
|
+
test 'html' do |a|
|
21
26
|
assert a.dom.html['test']
|
22
27
|
assert a.dom.html.scan(/<a.*>/).length == 2
|
23
28
|
end
|
24
29
|
|
25
|
-
test '
|
30
|
+
test 'data' do |a|
|
26
31
|
a.dom.scope(:list).apply([
|
27
32
|
{ todo: 'get milk' },
|
28
33
|
{ todo: 'get cookies' },
|
@@ -35,7 +40,7 @@ scope 'dominate' do
|
|
35
40
|
assert a.dom.html['get cookies']
|
36
41
|
end
|
37
42
|
|
38
|
-
test '
|
43
|
+
test 'context' do |a|
|
39
44
|
assert a.dom.html['John'] == nil
|
40
45
|
assert a.dom.html['CJ']
|
41
46
|
end
|
data/test/dummy/index.html
CHANGED
@@ -1,24 +1,28 @@
|
|
1
|
-
<
|
2
|
-
<
|
3
|
-
|
4
|
-
|
5
|
-
<
|
6
|
-
|
7
|
-
|
8
|
-
<
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
1
|
+
<html>
|
2
|
+
<body>
|
3
|
+
<!-- @partial header -->
|
4
|
+
|
5
|
+
<div>
|
6
|
+
<h1 data-scope='admin_only'>Admin</h1>
|
7
|
+
<span>current_user:
|
8
|
+
<span data-instance="current_user.first_name">John</span>
|
9
|
+
<span data-instance="current_user.last_name">Doe</span>
|
10
|
+
</span>
|
11
|
+
</div>
|
12
|
+
<ul data-scope="list">
|
13
|
+
<li>
|
14
|
+
<a href="#" data-prop="todo">test</a>
|
15
|
+
</li>
|
16
|
+
<li>
|
17
|
+
<a href="#" data-prop="todo">testing</a>
|
18
|
+
</li>
|
19
|
+
<li>
|
20
|
+
<ul data-scope="list-nested">
|
21
|
+
<li data-prop="todo"></li>
|
22
|
+
</ul>
|
23
|
+
</li>
|
18
24
|
</ul>
|
19
|
-
</li>
|
20
|
-
</ul>
|
21
25
|
|
22
|
-
|
23
|
-
|
24
|
-
</
|
26
|
+
<!-- @partial footer -->
|
27
|
+
</body>
|
28
|
+
</html>
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dominate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- cj
|
@@ -111,6 +111,8 @@ files:
|
|
111
111
|
- lib/dominate/inflectors.rb
|
112
112
|
- lib/dominate/version.rb
|
113
113
|
- test/dominate_test.rb
|
114
|
+
- test/dummy/footer.html
|
115
|
+
- test/dummy/header.html
|
114
116
|
- test/dummy/index.html
|
115
117
|
- test/helper.rb
|
116
118
|
homepage: ''
|
@@ -139,5 +141,7 @@ specification_version: 4
|
|
139
141
|
summary: Templating Engine.
|
140
142
|
test_files:
|
141
143
|
- test/dominate_test.rb
|
144
|
+
- test/dummy/footer.html
|
145
|
+
- test/dummy/header.html
|
142
146
|
- test/dummy/index.html
|
143
147
|
- test/helper.rb
|