dominate 0.0.3 → 0.0.4

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: 61a49494a53476a0d74388878a9ee0f901d75b25
4
- data.tar.gz: 3a28099fb6c50171da8dbf16cee5f762e353aaae
3
+ metadata.gz: 4adddd0bf591973649a8641ec85bfa3dc2f27d4a
4
+ data.tar.gz: aabc7be0a6a8813c8fbbecd32cd9b96882fca631
5
5
  SHA512:
6
- metadata.gz: ee8d6e3796133e2f233a57ab8e496e4a841d3c4fbd2f78c3ad71dae6758051bc07348a337c73db37aee13d22fee8190d8b627d65b8ea2cdf22dcd27e090b2bf5
7
- data.tar.gz: c9dc24d1abfa7cba0e0f6e78b2a24f1b8dd0f3bc6a9bd16549eaee234b55704751d09d6c2c61a64c58b35ecc5b2bf45b79b8180dff2c72752cc600022b2dea23
6
+ metadata.gz: 45880828162943194af2eede0dcf0e48f31a997d2ae1334533e89e1f139a412d8a9f7d72cc6b979a8fa1a50d55d4d37095904527b17097bb230496a535a1a8b4
7
+ data.tar.gz: 7e8e33ff4626b560d4f4404093cbb344411a6a66c65d65cf6ee57bd5a7f78dbc5770c5f69e338e6bc801586ae254aa007ea4a38d71c63c560ed1c4718ef68625
data/lib/dominate.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "nokogiri"
2
2
  require "dominate/version"
3
+ require "dominate/inflectors"
3
4
 
4
5
  module Dominate
5
6
  class << self
@@ -44,8 +45,6 @@ module Dominate
44
45
  class Scope < Struct.new :instance, :root_doc
45
46
 
46
47
  def apply data, &block
47
- @data = data
48
-
49
48
  root_doc.each do |doc|
50
49
  if data.is_a? Array
51
50
  doc = apply_list doc, data, &block
@@ -76,10 +75,14 @@ module Dominate
76
75
  private
77
76
 
78
77
  def apply_data doc, data, &block
78
+ data = data.to_deep_ostruct
79
+
79
80
  doc.traverse do |x|
80
81
  if x.attributes.keys.include? 'data-prop'
82
+ prop_val = x.attr('data-prop').to_s
83
+
81
84
  x.inner_html = value_for(
82
- data[x.attr('data-prop').to_s.to_sym], data, doc
85
+ data.instance_eval(prop_val), data, doc
83
86
  )
84
87
  end
85
88
  end
@@ -106,10 +109,14 @@ module Dominate
106
109
  # dup the element
107
110
  elem = first_elem.dup
108
111
 
112
+ data = data.to_deep_ostruct
113
+
109
114
  # lets look for data-prop elements
110
115
  elem.traverse do |x|
111
116
  if x.attributes.keys.include? 'data-prop'
112
- value = value_for data[x.attr('data-prop').to_s.to_sym], data, elem
117
+ prop_val = x.attr('data-prop').to_s
118
+
119
+ value = value_for data.instance_eval(prop_val), data, elem
113
120
  x.inner_html = value
114
121
  end
115
122
  end
@@ -0,0 +1,59 @@
1
+ require 'ostruct'
2
+
3
+ class String
4
+ def underscore
5
+ self.gsub(/::/, '/').
6
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
7
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
8
+ tr(" ", "_").
9
+ tr("-", "_").
10
+ downcase
11
+ end
12
+ end
13
+
14
+ class Object
15
+ def blank?
16
+ respond_to?(:empty?) ? empty? : !self
17
+ end
18
+
19
+ def present?
20
+ !blank?
21
+ end
22
+ end
23
+
24
+ class DeepOpenStruct < OpenStruct
25
+ def initialize hash = nil
26
+
27
+ @table = {}
28
+ @hash_table = {}
29
+
30
+ hash = [hash] unless hash.is_a? Hash
31
+
32
+ if hash
33
+ hash.each do |k,v|
34
+
35
+ if v.is_a? Array
36
+ @table[k.to_sym] ||= []
37
+
38
+ v.each { | entry |
39
+ @table[k.to_sym] << entry
40
+ }
41
+ else
42
+ @table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
43
+ @hash_table[k.to_sym] = v
44
+ new_ostruct_member(k)
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def to_h
51
+ @hash_table
52
+ end
53
+ end
54
+
55
+ class Hash
56
+ def to_deep_ostruct
57
+ DeepOpenStruct.new self
58
+ end
59
+ end
@@ -1,3 +1,3 @@
1
1
  module Dominate
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,31 +1,8 @@
1
1
  require_relative 'helper'
2
2
  require 'dominate'
3
-
4
3
  setup do
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
4
+ inline_html = File.read './test/dummy/index.html'
5
+
29
6
  instance = OpenStruct.new({
30
7
  current_user: OpenStruct.new({
31
8
  first_name: 'CJ',
@@ -84,4 +61,15 @@ scope 'dominate' do
84
61
  a.dom.scope(:list).apply(data)
85
62
  assert a.dom.html['do normal person stuff']
86
63
  end
64
+
65
+ test 'partial' do |a|
66
+ data = {
67
+ company: {
68
+ name: 'Test Company'
69
+ }
70
+ }
71
+
72
+ a.dom.scope(:footer).apply data
73
+ assert a.dom.html['Test Company']
74
+ end
87
75
  end
@@ -0,0 +1,24 @@
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>
18
+ </ul>
19
+ </li>
20
+ </ul>
21
+
22
+ <div data-scope="footer">
23
+ <span data-prop="company.name"></span>
24
+ </div>
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
@@ -108,8 +108,10 @@ files:
108
108
  - Rakefile
109
109
  - dominate.gemspec
110
110
  - lib/dominate.rb
111
+ - lib/dominate/inflectors.rb
111
112
  - lib/dominate/version.rb
112
113
  - test/dominate_test.rb
114
+ - test/dummy/index.html
113
115
  - test/helper.rb
114
116
  homepage: ''
115
117
  licenses:
@@ -137,4 +139,5 @@ specification_version: 4
137
139
  summary: Templating Engine.
138
140
  test_files:
139
141
  - test/dominate_test.rb
142
+ - test/dummy/index.html
140
143
  - test/helper.rb