dominate 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab7ec2b9a4cf3b16fd7bdc84bd399aa0be703c3e
4
- data.tar.gz: 7ea39278bdad0a307021074a332813592ff0cd81
3
+ metadata.gz: 0f420bad8c25bc8fb0ea788e7eb0f13ecda46047
4
+ data.tar.gz: 37cd280f32a2536aa9dbc05908e8bcec3a931766
5
5
  SHA512:
6
- metadata.gz: 1d789ac6186aeabf7387fa1d0502ef025b9a178f33cfb59313e9b5b573d7be787e06ef3b5b016835b73f0e358ace22102056ac87f597241d776eb7fb1d961502
7
- data.tar.gz: abba5aff573f213432ac3d08a016e5b17c29cb7660e0f5fa6ec624aac269bd51b34e8ed4c06d4aba29f6e2e7c8a9900939b6e6e7c4d482420ca6c9f02c44a578
6
+ metadata.gz: 5b0fcfc52fad1756420467d97511002cda7cbde2eb0db66d004200c51bdc9812313caf5640c8ca93398cb732b73a721f74161ce70c84d5f1c0ff36cbe4951bf5
7
+ data.tar.gz: 29865ebfedb3b9baf2dfdf8a7af900468f50dce1d1a0c23c9b515c892f819b9607ca505802af084f066d0705e43a29a79b9115852e49ab0675336510144e132e
data/dominate.gemspec CHANGED
@@ -18,6 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "nokogiri"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.3"
22
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "cutest-cj"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "awesome_print"
23
28
  end
@@ -1,3 +1,3 @@
1
1
  module Dominate
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/dominate.rb CHANGED
@@ -1,5 +1,110 @@
1
+ require "nokogiri"
1
2
  require "dominate/version"
2
3
 
3
4
  module Dominate
4
- # Your code goes here...
5
+ class << self
6
+ def HTML html, data = {}
7
+ Dom.new html, data
8
+ end
9
+ end
10
+
11
+ class Dom
12
+ attr_accessor :raw_html, :instance, :doc
13
+
14
+ def initialize raw_html, instance = false
15
+ @raw_html = raw_html
16
+ @instance = instance
17
+ @doc = Nokogiri::HTML raw_html
18
+ end
19
+
20
+ def scope name
21
+ Scope.new doc.search "[data-scope='#{name}']", instance
22
+ end
23
+
24
+ def html
25
+ apply_instance if instance
26
+
27
+ @html ||= "#{doc}"
28
+ end
29
+
30
+ def apply_instance
31
+ Scope.new(doc, instance).apply_instance
32
+ end
33
+ end
34
+
35
+ class Scope < Struct.new :root_doc, :instance
36
+
37
+ def apply data
38
+ @data = data
39
+
40
+ root_doc.each do |doc|
41
+ if data.is_a? Array
42
+ doc = apply_list doc, data
43
+ else
44
+ doc = apply_data doc, data
45
+ end
46
+ end
47
+ end
48
+
49
+ def apply_instance
50
+ root_doc.traverse do |x|
51
+ if defined?(x.attributes) && x.attributes.keys.include?('data-instance')
52
+ method = x.attr('data-instance')
53
+ begin
54
+ x.inner_html = instance.instance_eval method
55
+ rescue
56
+ x.inner_html = ''
57
+ end
58
+ end
59
+ end
60
+
61
+ root_doc
62
+ end
63
+
64
+ private
65
+
66
+ def apply_data doc, data
67
+ doc.traverse do |x|
68
+ if x.attributes.keys.include? 'data-prop'
69
+ end
70
+ end
71
+ end
72
+
73
+ def apply_list doc, data_list
74
+ # child placement
75
+ placement = 'after'
76
+ # clean the html, removing spaces and returns
77
+ doc.inner_html = doc.inner_html.strip
78
+ # grab the first element before we remove the rest
79
+ first_elem = doc.children.first
80
+ # remove all the children
81
+ doc.children.each_with_index do |node, index|
82
+ if "#{node}"['data-scope']
83
+ placement = (index == 0 ? 'after' : 'before')
84
+ # TODO: Scope.new doc.search "[data-scope='#{name}']"
85
+ # create a new scope
86
+ else
87
+ node.remove
88
+ end
89
+ end
90
+
91
+ # loop through the data list and create and element for each
92
+ data_list.each do |data|
93
+ # dup the element
94
+ elem = first_elem.dup
95
+
96
+ # lets look for data-prop elements
97
+ elem.traverse do |x|
98
+ if x.attributes.keys.include? 'data-prop'
99
+ x.inner_html = data[x.attr('data-prop').to_s.to_sym]
100
+ end
101
+ end
102
+
103
+ # add the element back to the doc
104
+ doc.children.public_send(placement, elem)
105
+ end
106
+
107
+ doc
108
+ end
109
+ end
5
110
  end
@@ -0,0 +1,61 @@
1
+ require_relative 'helper'
2
+ require 'dominate'
3
+
4
+ setup do
5
+ OpenStruct.new({
6
+ current_user: OpenStruct.new({
7
+ first_name: 'CJ',
8
+ 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
+ end
35
+
36
+ scope 'dominate' do
37
+ 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
41
+ end
42
+
43
+ test 'binding data' do |a|
44
+ dom = Dominate::HTML a.inline_html
45
+ dom.scope(:list).apply([
46
+ { todo: 'get milk' },
47
+ { todo: 'get cookies' },
48
+ { todo: 'work out' },
49
+ ])
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
+ end
55
+
56
+ test 'binding context' do |a|
57
+ dom = Dominate::HTML a.inline_html, a
58
+ assert dom.html['John'] == nil
59
+ assert dom.html['CJ']
60
+ end
61
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'pry'
2
+ require 'awesome_print'
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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,48 @@ dependencies:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: cutest-cj
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: awesome_print
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
41
97
  description: Allows you to manipulate the dom without touching it.
42
98
  email:
43
99
  - cjlazell@gmail.com
@@ -53,6 +109,8 @@ files:
53
109
  - dominate.gemspec
54
110
  - lib/dominate.rb
55
111
  - lib/dominate/version.rb
112
+ - test/dominate_test.rb
113
+ - test/helper.rb
56
114
  homepage: ''
57
115
  licenses:
58
116
  - MIT
@@ -77,4 +135,6 @@ rubygems_version: 2.2.2
77
135
  signing_key:
78
136
  specification_version: 4
79
137
  summary: Templating Engine.
80
- test_files: []
138
+ test_files:
139
+ - test/dominate_test.rb
140
+ - test/helper.rb