dominate 0.0.2 → 0.0.3

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: 0f420bad8c25bc8fb0ea788e7eb0f13ecda46047
4
- data.tar.gz: 37cd280f32a2536aa9dbc05908e8bcec3a931766
3
+ metadata.gz: 61a49494a53476a0d74388878a9ee0f901d75b25
4
+ data.tar.gz: 3a28099fb6c50171da8dbf16cee5f762e353aaae
5
5
  SHA512:
6
- metadata.gz: 5b0fcfc52fad1756420467d97511002cda7cbde2eb0db66d004200c51bdc9812313caf5640c8ca93398cb732b73a721f74161ce70c84d5f1c0ff36cbe4951bf5
7
- data.tar.gz: 29865ebfedb3b9baf2dfdf8a7af900468f50dce1d1a0c23c9b515c892f819b9607ca505802af084f066d0705e43a29a79b9115852e49ab0675336510144e132e
6
+ metadata.gz: ee8d6e3796133e2f233a57ab8e496e4a841d3c4fbd2f78c3ad71dae6758051bc07348a337c73db37aee13d22fee8190d8b627d65b8ea2cdf22dcd27e090b2bf5
7
+ data.tar.gz: c9dc24d1abfa7cba0e0f6e78b2a24f1b8dd0f3bc6a9bd16549eaee234b55704751d09d6c2c61a64c58b35ecc5b2bf45b79b8180dff2c72752cc600022b2dea23
@@ -1,3 +1,3 @@
1
1
  module Dominate
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/dominate.rb CHANGED
@@ -18,38 +18,50 @@ module Dominate
18
18
  end
19
19
 
20
20
  def scope name
21
- Scope.new doc.search "[data-scope='#{name}']", instance
21
+ reset_html
22
+ Scope.new instance, doc.search("[data-scope='#{name}']")
22
23
  end
23
24
 
24
25
  def html
25
- apply_instance if instance
26
-
27
- @html ||= "#{doc}"
26
+ @html ||= begin
27
+ apply_instance if instance
28
+ "#{doc}"
29
+ end
28
30
  end
29
31
 
30
32
  def apply_instance
31
- Scope.new(doc, instance).apply_instance
33
+ reset_html
34
+ Scope.new(instance, doc).apply_instance
35
+ end
36
+
37
+ private
38
+
39
+ def reset_html
40
+ @html = false
32
41
  end
33
42
  end
34
43
 
35
- class Scope < Struct.new :root_doc, :instance
44
+ class Scope < Struct.new :instance, :root_doc
36
45
 
37
- def apply data
46
+ def apply data, &block
38
47
  @data = data
39
48
 
40
49
  root_doc.each do |doc|
41
50
  if data.is_a? Array
42
- doc = apply_list doc, data
51
+ doc = apply_list doc, data, &block
43
52
  else
44
- doc = apply_data doc, data
53
+ doc = apply_data doc, data, &block
45
54
  end
46
55
  end
56
+
57
+ root_doc
47
58
  end
48
59
 
49
60
  def apply_instance
50
61
  root_doc.traverse do |x|
51
62
  if defined?(x.attributes) && x.attributes.keys.include?('data-instance')
52
- method = x.attr('data-instance')
63
+ method = x.attr 'data-instance'
64
+
53
65
  begin
54
66
  x.inner_html = instance.instance_eval method
55
67
  rescue
@@ -63,14 +75,17 @@ module Dominate
63
75
 
64
76
  private
65
77
 
66
- def apply_data doc, data
78
+ def apply_data doc, data, &block
67
79
  doc.traverse do |x|
68
80
  if x.attributes.keys.include? 'data-prop'
81
+ x.inner_html = value_for(
82
+ data[x.attr('data-prop').to_s.to_sym], data, doc
83
+ )
69
84
  end
70
85
  end
71
86
  end
72
87
 
73
- def apply_list doc, data_list
88
+ def apply_list doc, data_list, &block
74
89
  # child placement
75
90
  placement = 'after'
76
91
  # clean the html, removing spaces and returns
@@ -81,8 +96,6 @@ module Dominate
81
96
  doc.children.each_with_index do |node, index|
82
97
  if "#{node}"['data-scope']
83
98
  placement = (index == 0 ? 'after' : 'before')
84
- # TODO: Scope.new doc.search "[data-scope='#{name}']"
85
- # create a new scope
86
99
  else
87
100
  node.remove
88
101
  end
@@ -96,15 +109,34 @@ module Dominate
96
109
  # lets look for data-prop elements
97
110
  elem.traverse do |x|
98
111
  if x.attributes.keys.include? 'data-prop'
99
- x.inner_html = data[x.attr('data-prop').to_s.to_sym]
112
+ value = value_for data[x.attr('data-prop').to_s.to_sym], data, elem
113
+ x.inner_html = value
100
114
  end
101
115
  end
102
116
 
117
+ block.call elem, data if block
103
118
  # add the element back to the doc
104
119
  doc.children.public_send(placement, elem)
105
120
  end
106
121
 
107
122
  doc
108
123
  end
124
+
125
+ private
126
+
127
+ def value_for value, data, elem
128
+ if value.is_a? Proc
129
+ if value.parameters.length == 0
130
+ instance.instance_exec(&value).to_s
131
+ elsif value.parameters.length == 1
132
+ instance.instance_exec(data, &value).to_s
133
+ elsif value.parameters.length == 2
134
+ instance.instance_exec(data, elem, &value).to_s
135
+ end
136
+ else
137
+ value.to_s
138
+ end
139
+ end
140
+
109
141
  end
110
142
  end
@@ -2,60 +2,86 @@ require_relative 'helper'
2
2
  require 'dominate'
3
3
 
4
4
  setup do
5
- OpenStruct.new({
5
+ inline_html = <<-D
6
+ <div>
7
+ <div>
8
+ <h1 data-scope='admin_only'>Admin</h1>
9
+ <span>current_user:
10
+ <span data-instance="current_user.first_name">John</span>
11
+ <span data-instance="current_user.last_name">Doe</span>
12
+ </span>
13
+ </div>
14
+ <ul data-scope="list">
15
+ <li>
16
+ <a href="#" data-prop="todo">test</a>
17
+ </li>
18
+ <li>
19
+ <a href="#" data-prop="todo">testing</a>
20
+ </li>
21
+ <li>
22
+ <ul data-scope="list-nested">
23
+ <li data-prop="todo"></li>
24
+ </ul>
25
+ </li>
26
+ </ul>
27
+ </div>
28
+ D
29
+ instance = OpenStruct.new({
6
30
  current_user: OpenStruct.new({
7
31
  first_name: 'CJ',
8
32
  last_name: 'Lazell'
9
- }),
10
- inline_html: <<-D
11
- <div>
12
- <div>
13
- <span>current_user:
14
- <span data-instance="current_user.first_name">John</span>
15
- <span data-instance="current_user.last_name">Doe</span>
16
- </span>
17
- </div>
18
- <ul data-scope="list">
19
- <li>
20
- <a href="#" data-prop="todo">test</a>
21
- </li>
22
- <li>
23
- <a href="#" data-prop="todo">testing</a>
24
- </li>
25
- <li>
26
- <ul data-scope="list-nested">
27
- <li data-prop="todo"></li>
28
- </ul>
29
- </li>
30
- </ul>
31
- </div>
32
- D
33
+ })
34
+ })
35
+
36
+ OpenStruct.new({
37
+ current_user: instance.current_user,
38
+ dom: Dominate::HTML(inline_html, instance),
33
39
  })
34
40
  end
35
41
 
36
42
  scope 'dominate' do
37
43
  test 'render html' do |a|
38
- dom = Dominate::HTML a.inline_html
39
- assert dom.html['test']
40
- assert dom.html.scan(/<a.*>/).length == 2
44
+ assert a.dom.html['test']
45
+ assert a.dom.html.scan(/<a.*>/).length == 2
41
46
  end
42
47
 
43
48
  test 'binding data' do |a|
44
- dom = Dominate::HTML a.inline_html
45
- dom.scope(:list).apply([
49
+ a.dom.scope(:list).apply([
46
50
  { todo: 'get milk' },
47
51
  { todo: 'get cookies' },
48
52
  { todo: 'work out' },
49
53
  ])
50
- assert dom.html['test'] == nil
51
- assert dom.html.scan(/<a.*>/).length == 3
52
- assert dom.html['get milk']
53
- assert dom.html['get cookies']
54
+
55
+ assert a.dom.html['test'] == nil
56
+ assert a.dom.html.scan(/<a.*>/).length == 3
57
+ assert a.dom.html['get milk']
58
+ assert a.dom.html['get cookies']
54
59
  end
55
60
 
56
61
  test 'binding context' do |a|
57
- dom = Dominate::HTML a.inline_html, a
58
- assert dom.html['John'] == nil
59
- assert dom.html['CJ']
62
+ assert a.dom.html['John'] == nil
63
+ assert a.dom.html['CJ']
64
+ end
65
+
66
+ test 'blocks' do |a|
67
+ a.dom.scope(:admin_only).each do |node|
68
+ unless a.current_user.admin
69
+ node.remove
70
+ end
71
+ end
72
+ assert a.dom.html['Admin'] == nil
73
+ end
74
+
75
+ test 'procs' do |a|
76
+ data = [
77
+ {todo: -> {
78
+ current_user.admin ? 'do admin stuff' : 'do normal person stuff'}
79
+ },
80
+ {todo: ->(d) { d.length }
81
+ }
82
+ ]
83
+
84
+ a.dom.scope(:list).apply(data)
85
+ assert a.dom.html['do normal person stuff']
60
86
  end
61
87
  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.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-31 00:00:00.000000000 Z
11
+ date: 2014-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri