dominate 0.1.0 → 0.1.1

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: 532cf23fe764bdddaef370b9e8142c6da1f6ad45
4
- data.tar.gz: 0003456300745421cd8b029c7acecc53a4b85bd7
3
+ metadata.gz: 6dfebc8c4d9a8b4820871cc1ead5f65be47e475b
4
+ data.tar.gz: 015fc0939f8bd03b4a895a71fdeb4e1d0ae36d40
5
5
  SHA512:
6
- metadata.gz: 54455c11e4ecf1f018a38f175d22b958f435c9b1452fb16930d108e30f878c8be3263df20f228708902a63574e236e62b8e6076e71886be2423359eb57664c70
7
- data.tar.gz: 23a05a39d0af125bd17023bd31d620310ef59d4d295041218904d9cb0cbf8aa3b326df99a3a9ec2141bb19809bde5024476b5cc5c88eacd48a73db89b72bdb97
6
+ metadata.gz: f080d42f48dd437d9a9ed1f5884c6f829fb7fe85f5702d911872f83fe18523f5ca3b0c1973ea6c2b502a64f5e77ceab4db53050057c91a98563f65869a051cf5
7
+ data.tar.gz: 30e238e4e39cea411892e2707032e61914e74928e78c8f049f0f481c8b224af8b55b7f5fb270323940d864ae718ae8c0fb4768cc73ecd2223d1660b99479f501
@@ -0,0 +1,58 @@
1
+ module Dominate
2
+ class Dom
3
+ attr_accessor :raw_html, :instance, :doc
4
+
5
+ PARTIAL_REGEX = /<!--\s*@partial\s*([a-zA-Z0-9\-_]*)\s*-->/
6
+
7
+ def initialize raw_html, instance = false
8
+ @raw_html = raw_html
9
+ @instance = instance
10
+
11
+ if raw_html.match(/<html.*>/)
12
+ @doc = Nokogiri::HTML::Document.parse raw_html
13
+ else
14
+ @doc = Nokogiri::HTML.fragment raw_html
15
+ end
16
+
17
+ load_partials if Dominate.config.view_path
18
+ end
19
+
20
+ def load_partials
21
+ doc.traverse do |e|
22
+ if match = e.to_html.strip.match(PARTIAL_REGEX)
23
+ partial = match[1]
24
+ e.swap Nokogiri::HTML.fragment(
25
+ File.read "#{view_path}/#{partial}.html"
26
+ )
27
+ end
28
+ end
29
+ end
30
+
31
+ def scope name
32
+ reset_html
33
+ Scope.new instance, doc.search("[data-scope='#{name}']")
34
+ end
35
+
36
+ def html
37
+ @html ||= begin
38
+ apply_instance if instance
39
+ "#{doc}"
40
+ end
41
+ end
42
+
43
+ def apply_instance
44
+ reset_html
45
+ Scope.new(instance, doc).apply_instance
46
+ end
47
+
48
+ private
49
+
50
+ def reset_html
51
+ @html = false
52
+ end
53
+
54
+ def view_path
55
+ Dominate.config.view_path
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,106 @@
1
+ module Dominate
2
+ class Scope < Struct.new :instance, :root_doc
3
+
4
+ def apply data, &block
5
+ root_doc.each do |doc|
6
+ if data.is_a? Array
7
+ doc = apply_list doc, data, &block
8
+ else
9
+ doc = apply_data doc, data, &block
10
+ end
11
+ end
12
+
13
+ root_doc
14
+ end
15
+
16
+ def apply_instance
17
+ root_doc.traverse do |x|
18
+ if defined?(x.attributes) && x.attributes.keys.include?('data-instance')
19
+ method = x.attr 'data-instance'
20
+
21
+ begin
22
+ x.inner_html = instance.instance_eval method
23
+ rescue
24
+ x.inner_html = ''
25
+ end
26
+ end
27
+ end
28
+
29
+ root_doc
30
+ end
31
+
32
+ private
33
+
34
+ def apply_data doc, data, &block
35
+ data = data.to_deep_ostruct
36
+
37
+ doc.traverse do |x|
38
+ if x.attributes.keys.include? 'data-prop'
39
+ prop_val = x.attr('data-prop').to_s
40
+
41
+ x.inner_html = value_for(
42
+ data.instance_eval(prop_val), data, doc
43
+ )
44
+ end
45
+ end
46
+ end
47
+
48
+ def apply_list doc, data_list, &block
49
+ # child placement
50
+ placement = 'after'
51
+ # clean the html, removing spaces and returns
52
+ doc.inner_html = doc.inner_html.strip
53
+ # grab the first element before we remove the rest
54
+ first_elem = doc.children.first
55
+ # remove all the children
56
+ doc.children.each_with_index do |node, index|
57
+ if "#{node}"['data-scope']
58
+ placement = (index == 0 ? 'after' : 'before')
59
+ else
60
+ node.remove
61
+ end
62
+ end
63
+
64
+ # loop through the data list and create and element for each
65
+ data_list.each do |data|
66
+ # dup the element
67
+ elem = first_elem.dup
68
+
69
+ data = data.to_deep_ostruct
70
+
71
+ # lets look for data-prop elements
72
+ elem.traverse do |x|
73
+ if x.attributes.keys.include? 'data-prop'
74
+ prop_val = x.attr('data-prop').to_s
75
+
76
+ value = value_for data.instance_eval(prop_val), data, elem
77
+ x.inner_html = value
78
+ end
79
+ end
80
+
81
+ block.call elem, data if block
82
+ # add the element back to the doc
83
+ doc.children.public_send(placement, elem)
84
+ end
85
+
86
+ doc
87
+ end
88
+
89
+ private
90
+
91
+ def value_for value, data, elem
92
+ if value.is_a? Proc
93
+ if value.parameters.length == 0
94
+ instance.instance_exec(&value).to_s
95
+ elsif value.parameters.length == 1
96
+ instance.instance_exec(data, &value).to_s
97
+ elsif value.parameters.length == 2
98
+ instance.instance_exec(data, elem, &value).to_s
99
+ end
100
+ else
101
+ value.to_s
102
+ end
103
+ end
104
+
105
+ end
106
+ end
@@ -1,3 +1,3 @@
1
1
  module Dominate
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/dominate.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "nokogiri"
2
2
  require "dominate/version"
3
3
  require "dominate/inflectors"
4
+ require "dominate/scope"
5
+ require "dominate/dom"
4
6
 
5
7
  module Dominate
6
8
  extend self
@@ -23,166 +25,4 @@ module Dominate
23
25
  def reset_config!
24
26
  @config = OpenStruct.new
25
27
  end
26
-
27
- class Dom
28
- attr_accessor :raw_html, :instance, :doc
29
-
30
- PARTIAL_REGEX = /<!--\s*@partial\s*([a-zA-Z0-9\-_]*)\s*-->/
31
-
32
- def initialize raw_html, instance = false
33
- @raw_html = raw_html
34
- @instance = instance
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
54
- end
55
-
56
- def scope name
57
- reset_html
58
- Scope.new instance, doc.search("[data-scope='#{name}']")
59
- end
60
-
61
- def html
62
- @html ||= begin
63
- apply_instance if instance
64
- "#{doc}"
65
- end
66
- end
67
-
68
- def apply_instance
69
- reset_html
70
- Scope.new(instance, doc).apply_instance
71
- end
72
-
73
- private
74
-
75
- def reset_html
76
- @html = false
77
- end
78
-
79
- def view_path
80
- Dominate.config.view_path
81
- end
82
- end
83
-
84
- class Scope < Struct.new :instance, :root_doc
85
-
86
- def apply data, &block
87
- root_doc.each do |doc|
88
- if data.is_a? Array
89
- doc = apply_list doc, data, &block
90
- else
91
- doc = apply_data doc, data, &block
92
- end
93
- end
94
-
95
- root_doc
96
- end
97
-
98
- def apply_instance
99
- root_doc.traverse do |x|
100
- if defined?(x.attributes) && x.attributes.keys.include?('data-instance')
101
- method = x.attr 'data-instance'
102
-
103
- begin
104
- x.inner_html = instance.instance_eval method
105
- rescue
106
- x.inner_html = ''
107
- end
108
- end
109
- end
110
-
111
- root_doc
112
- end
113
-
114
- private
115
-
116
- def apply_data doc, data, &block
117
- data = data.to_deep_ostruct
118
-
119
- doc.traverse do |x|
120
- if x.attributes.keys.include? 'data-prop'
121
- prop_val = x.attr('data-prop').to_s
122
-
123
- x.inner_html = value_for(
124
- data.instance_eval(prop_val), data, doc
125
- )
126
- end
127
- end
128
- end
129
-
130
- def apply_list doc, data_list, &block
131
- # child placement
132
- placement = 'after'
133
- # clean the html, removing spaces and returns
134
- doc.inner_html = doc.inner_html.strip
135
- # grab the first element before we remove the rest
136
- first_elem = doc.children.first
137
- # remove all the children
138
- doc.children.each_with_index do |node, index|
139
- if "#{node}"['data-scope']
140
- placement = (index == 0 ? 'after' : 'before')
141
- else
142
- node.remove
143
- end
144
- end
145
-
146
- # loop through the data list and create and element for each
147
- data_list.each do |data|
148
- # dup the element
149
- elem = first_elem.dup
150
-
151
- data = data.to_deep_ostruct
152
-
153
- # lets look for data-prop elements
154
- elem.traverse do |x|
155
- if x.attributes.keys.include? 'data-prop'
156
- prop_val = x.attr('data-prop').to_s
157
-
158
- value = value_for data.instance_eval(prop_val), data, elem
159
- x.inner_html = value
160
- end
161
- end
162
-
163
- block.call elem, data if block
164
- # add the element back to the doc
165
- doc.children.public_send(placement, elem)
166
- end
167
-
168
- doc
169
- end
170
-
171
- private
172
-
173
- def value_for value, data, elem
174
- if value.is_a? Proc
175
- if value.parameters.length == 0
176
- instance.instance_exec(&value).to_s
177
- elsif value.parameters.length == 1
178
- instance.instance_exec(data, &value).to_s
179
- elsif value.parameters.length == 2
180
- instance.instance_exec(data, elem, &value).to_s
181
- end
182
- else
183
- value.to_s
184
- end
185
- end
186
-
187
- end
188
28
  end
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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
@@ -108,7 +108,9 @@ files:
108
108
  - Rakefile
109
109
  - dominate.gemspec
110
110
  - lib/dominate.rb
111
+ - lib/dominate/dom.rb
111
112
  - lib/dominate/inflectors.rb
113
+ - lib/dominate/scope.rb
112
114
  - lib/dominate/version.rb
113
115
  - test/dominate_test.rb
114
116
  - test/dummy/footer.html