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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4adddd0bf591973649a8641ec85bfa3dc2f27d4a
4
- data.tar.gz: aabc7be0a6a8813c8fbbecd32cd9b96882fca631
3
+ metadata.gz: 532cf23fe764bdddaef370b9e8142c6da1f6ad45
4
+ data.tar.gz: 0003456300745421cd8b029c7acecc53a4b85bd7
5
5
  SHA512:
6
- metadata.gz: 45880828162943194af2eede0dcf0e48f31a997d2ae1334533e89e1f139a412d8a9f7d72cc6b979a8fa1a50d55d4d37095904527b17097bb230496a535a1a8b4
7
- data.tar.gz: 7e8e33ff4626b560d4f4404093cbb344411a6a66c65d65cf6ee57bd5a7f78dbc5770c5f69e338e6bc801586ae254aa007ea4a38d71c63c560ed1c4718ef68625
6
+ metadata.gz: 54455c11e4ecf1f018a38f175d22b958f435c9b1452fb16930d108e30f878c8be3263df20f228708902a63574e236e62b8e6076e71886be2423359eb57664c70
7
+ data.tar.gz: 23a05a39d0af125bd17023bd31d620310ef59d4d295041218904d9cb0cbf8aa3b326df99a3a9ec2141bb19809bde5024476b5cc5c88eacd48a73db89b72bdb97
@@ -1,3 +1,3 @@
1
1
  module Dominate
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/dominate.rb CHANGED
@@ -3,19 +3,54 @@ require "dominate/version"
3
3
  require "dominate/inflectors"
4
4
 
5
5
  module Dominate
6
- class << self
7
- def HTML html, data = {}
8
- Dom.new html, data
9
- end
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
- @doc = Nokogiri::HTML raw_html
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
@@ -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 'render html' do |a|
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 'binding data' do |a|
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 'binding context' do |a|
43
+ test 'context' do |a|
39
44
  assert a.dom.html['John'] == nil
40
45
  assert a.dom.html['CJ']
41
46
  end
@@ -0,0 +1,3 @@
1
+ <div data-scope="footer">
2
+ <span data-prop="company.name"></span>
3
+ </div>
@@ -0,0 +1,3 @@
1
+ <div>
2
+ <h1>header</h1>
3
+ </div>
@@ -1,24 +1,28 @@
1
- <div>
2
- <h1 data-scope='admin_only'>Admin</h1>
3
- <span>current_user:
4
- <span data-instance="current_user.first_name">John</span>
5
- <span data-instance="current_user.last_name">Doe</span>
6
- </span>
7
- </div>
8
- <ul data-scope="list">
9
- <li>
10
- <a href="#" data-prop="todo">test</a>
11
- </li>
12
- <li>
13
- <a href="#" data-prop="todo">testing</a>
14
- </li>
15
- <li>
16
- <ul data-scope="list-nested">
17
- <li data-prop="todo"></li>
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
- <div data-scope="footer">
23
- <span data-prop="company.name"></span>
24
- </div>
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
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