BOBrb 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3afa5f5e8d8cdeb1eab0ffccb533cfc0c79855a4
4
+ data.tar.gz: 807d159c23278dd86b22bf6e35f4e7048cb72b52
5
+ SHA512:
6
+ metadata.gz: 32ac4ef84bfce1b6aacc7221c065c055ead785644e9dce3334b8b9778f0f66e953331e01c5b9673186ee20d4eea15440322b70a78d4c1634c60868dad56a4dbe
7
+ data.tar.gz: 5f5925f65fa6965683a1608400e1664b487092f52e2ea9fcd63bcfb1ac9bdddd925ecd162578b4d7e32fd91a2c669a4a9ddd0b55ae99b4b92429b42a2e25e18d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,24 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1.0
8
+ - 2.1.3
9
+ - 2.1.6
10
+ - jruby
11
+ os:
12
+ - linux
13
+ - osx
14
+ matrix:
15
+ exclude:
16
+ - rvm: 1.9.2
17
+ os: osx
18
+ - rvm: 2.1.0
19
+ os: osx
20
+ - rvm: jruby
21
+ os: osx
22
+ addons:
23
+ code_climate:
24
+ repo_token: dbbc732a1a68e8d47a792955b21b0a0018854a658239511a561dff1e0c7a127f
data/BOB.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'BOB/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "BOBrb"
8
+ spec.version = BOB::VERSION
9
+ spec.authors = ["Stephan Nordnes Eriksen"]
10
+ spec.email = ["stephanruler@gmail.com"]
11
+
12
+ spec.summary = %q{BOB: Powerful XML and HTML building}
13
+ spec.description = %q{BOB is a simple and powerfull ruby pipe system for building complex XML and HTML structures.}
14
+ spec.homepage = "https://github.com/stephan-nordnes-eriksen/BOBrb"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.3"
25
+ spec.add_development_dependency "codeclimate-test-reporter", "~>0.4"
26
+
27
+ end
data/BOB.png ADDED
Binary file
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in BOB.gemspec
4
+ gemspec
5
+
6
+ gem "codeclimate-test-reporter", :group => :test, :require => nil
data/Gemfile.lock ADDED
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ BOB (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ codeclimate-test-reporter (0.4.8)
10
+ simplecov (>= 0.7.1, < 1.0.0)
11
+ diff-lcs (1.2.5)
12
+ docile (1.1.5)
13
+ json (1.8.3)
14
+ rake (10.4.2)
15
+ rspec (3.3.0)
16
+ rspec-core (~> 3.3.0)
17
+ rspec-expectations (~> 3.3.0)
18
+ rspec-mocks (~> 3.3.0)
19
+ rspec-core (3.3.0)
20
+ rspec-support (~> 3.3.0)
21
+ rspec-expectations (3.3.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.3.0)
24
+ rspec-mocks (3.3.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.3.0)
27
+ rspec-support (3.3.0)
28
+ simplecov (0.10.0)
29
+ docile (~> 1.1.0)
30
+ json (~> 1.8)
31
+ simplecov-html (~> 0.10.0)
32
+ simplecov-html (0.10.0)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ BOB!
39
+ bundler (~> 1.9)
40
+ codeclimate-test-reporter
41
+ rake (~> 10.0)
42
+ rspec (~> 3.3)
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Stephan Nordnes Eriksen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,234 @@
1
+ #BOB
2
+ BOB is a simple and powerfull ruby pipe system for building complex XML and HTML structures.
3
+
4
+ ![BOB](/BOB.png?raw=true)
5
+
6
+ [![Build Status](https://travis-ci.org/stephan-nordnes-eriksen/BOBrb.svg)](https://travis-ci.org/stephan-nordnes-eriksen/BOBrb)
7
+ [![Code Climate](https://codeclimate.com/github/stephan-nordnes-eriksen/BOBrb/badges/gpa.svg)](https://codeclimate.com/github/stephan-nordnes-eriksen/BOBrb)
8
+ [![Test Coverage](https://codeclimate.com/github/stephan-nordnes-eriksen/BOBrb/badges/coverage.svg)](https://codeclimate.com/github/stephan-nordnes-eriksen/BOBrb/coverage)
9
+
10
+ ##Install:
11
+ ```ruby
12
+
13
+ require "BOB"
14
+ ```
15
+
16
+ ###Gem (currently unreleased)
17
+ gem install BOB
18
+
19
+ ##Usage:
20
+ BOB is a pipe system for generating html structures.
21
+
22
+ ###TL;DR
23
+ ```ruby
24
+
25
+ BOB.new("div").toString() #=> "<div></div>"
26
+ BOB.new("div").s() #=> "<div></div>"
27
+ BOB.new("div").classs("some_class").s() #=> "<div class=\"some_class\"></div>" #NOTICE CLASSS, with three "s"-es. This is because ruby has defined the .class method.
28
+ BOB.new("div").id("some_id").s() #=> "<div id=\"some_id\"></div>"
29
+ BOB.new("div.some_class").s() #=> "<div class=\"some_class\"></div>"
30
+ BOB.new("div#some_id").s() #=> "<div id=\"some_id\"></div>"
31
+ BOB.new("div").style("min-height: 10px;").s() #=> "<div style=\"min-height: 10px;\"></div>"
32
+ BOB.new("h1").content("BOB is awesome! <3").s() #=> "<h1>BOB is awesome! <3</h1>"
33
+ BOB.new("div", {"data-BOB-is-cool": "Yes it is", "data-very-cool": "indeed"}).s() #=> "<div data-BOB-is-cool="Yes it is" data-very-cool="indeed"></div>"
34
+ BOB.new("div").append("span").s() #=> "<div></div><span></span>"
35
+ BOB.new("div").prepend("span").s() #=> "<span></span><div></div>"
36
+ BOB.new("div").insert("span").s() #=> "<div><span></span></div>"
37
+ BOB.new("div").append("span").id("some_id").s() #=> "<div></div><span id=\"some_id\"></span>"
38
+ BOB.new("div").append("span").up().id("some_id").s() #=> "<div id=\"some_id\"></div><span></span>"
39
+ BOB.new("ul").do([1,2,3]).insert("li").content(BOB.data).s() #=> <ul><li>1</li><li>2</li><li>3</li></ul>
40
+ data = [1,2,3]; BOB.new("ul").do(data).insert("li", {"data-property": BOB.data}).id(BOB.data).s() #=> <ul><li id="1" data-property="1"></li><li id="2" data-property="2"></li><li id="3" data-property="3"></li></ul>
41
+ BOB.new("ul").do([1,2,3]).insert("li").up().id(BOB.data).s() //INVALID #=> The BOB.data will not be set and you will get the output of: "<ul><li></li><li></li><li></li></ul>".
42
+ BOB.new("ul").do([1,2,3]).insert("li").content(lambda{return BOB.data() + 2}).s() #=> <ul><li>3</li><li>4</li><li>5</li></ul>
43
+ data_modifier = lambda{return BOB.data() + 2}; BOB.new("ul").do([1,2,3]).insert("li").content(data_modifier).s() #=> <ul><li>3</li><li>4</li><li>5</li></ul>
44
+
45
+ //Shorthand syntax:
46
+ BOB.new("div").i("img", {"src":"some.png"}).u().d([1,2,3]).i("p.number").co(BOB.d).s() #=> "<div><img src="some.png" /><p class="number">1</p><p class="number">2</p><p class="number">3</p></div>"
47
+ ```
48
+ [Go to shorthand syntax section](#shorthand)
49
+
50
+ ###Building a simple tag:
51
+ ```ruby
52
+
53
+ BOB.new("div").toString()
54
+ #=> "<div></div>"
55
+ ```
56
+ You can also use the shorthand method "s". For a full list see [the shorthand section](#shorthand)
57
+
58
+ ```ruby
59
+
60
+ BOB.new("div").s()
61
+ #=> "<div></div>"
62
+ ```
63
+
64
+ ###Adding IDs and classes
65
+ ```ruby
66
+
67
+ BOB.new("div").classs("some_class").s()
68
+ #=> "<div class=\"some_class\"></div>"
69
+ BOB.new("div").id("some_id").s()
70
+ #=> "<div id=\"some_class\"></div>"
71
+ ```
72
+
73
+ This can also be done with the shorthand selector style:
74
+ ```ruby
75
+
76
+ BOB.new("div.some_class").s()
77
+ #=> "<div class=\"some_class\"></div>"
78
+ BOB.new("div#some_id").s()
79
+ #=> "<div id=\"some_class\"></div>"
80
+ ```
81
+
82
+ ###Adding styles, content, and custom attributes
83
+ ```ruby
84
+
85
+ BOB.new("div").style("min-height: 10px;").s()
86
+ #=> "<div style=\"min-height: 10px;\"></div>"
87
+ BOB.new("h1").content("BOB is awesome! <3").s()
88
+ #=> "<h1>BOB is awesome! <3</h1>"
89
+ BOB.new("div", {"data-BOB-is-cool": "Yes it is", "data-very-cool": "indeed"}).s()
90
+ #=> "<div data-BOB-is-cool="Yes it is" data-very-cool="indeed"></div>"
91
+ ```
92
+
93
+ ###Building and appending/prepending tags:
94
+ ```ruby
95
+
96
+ BOB.new("div").append("span").s()
97
+ #=> "<div></div><span></span>"
98
+ BOB.new("div").prepend("span").s()
99
+ #=> "<span></span><div></div>"
100
+ ```
101
+
102
+ ###Building with inserting tags:
103
+ ```ruby
104
+
105
+ BOB.new("div").insert("span").s()
106
+ #=> "<div><span></span></div>"
107
+ ```
108
+
109
+ ###Handling basic nesting
110
+ When appending, prepending, or inserting you will effectively branch downwards, meaning that the latest element is your current active. Example:
111
+
112
+ ```ruby
113
+
114
+ BOB.new("div").append("span").id("some_id").s()
115
+ #=> "<div></div><span id=\"some_id\"></span>"
116
+ ```
117
+
118
+ In this simlpe example we see that it is the `span` that receives the `id`, not the div. If we wanted to affect the `div` in stead (in this trivial, nonsensical, example), we would do:
119
+
120
+ ```ruby
121
+
122
+ BOB.new("div").append("span").up().id("some_id").s()
123
+ #=> "<div id=\"some_id\"></div><span></span>"
124
+ ```
125
+
126
+ We effectively traversed backwards, or up, the stack. This is the basics of managing nesting and branching. Let's have a look at how to build usefull branches.
127
+
128
+ **It is very improtant to keep track of what is "in focus" when you are applying the next pipe.**
129
+
130
+
131
+ ###Branching out
132
+ Say you want HTML that looks like this:
133
+
134
+ ```ruby
135
+
136
+ <ul><li>1</li><li>2</li><li>3</li></ul>
137
+ ```
138
+
139
+ To do such branching, without having to re-write all parts manually, you can use the `do` method:
140
+
141
+ ```ruby
142
+
143
+ BOB.new("ul").do([1,2,3]).insert("li").content(BOB.data).s()
144
+ #=> <ul><li>1</li><li>2</li><li>3</li></ul>
145
+ ```
146
+
147
+ Here you see `BOB.data` which is a special variable which represend the individal data points when the chain in being executed. It can be used for anything within the scope of the `do`, eg.
148
+
149
+ ```ruby
150
+
151
+ data = [1,2,3]
152
+ BOB.new("ul").do(data).insert("li", {"data-property": BOB.data}).id(BOB.data).s()
153
+ #=> <ul><li id="1" data-property="1"></li><li id="2" data-property="2"></li><li id="3" data-property="3"></li></ul>
154
+ ```
155
+
156
+ However, if you use the `up` command and go out of the scope of `do`, `BOB.data` might not work. The behaviour is undefined so errors and/or strange behaviour might occur. Eg:
157
+
158
+ ```ruby
159
+
160
+ BOB.new("ul").do([1,2,3]).insert("li").up().id(BOB.data).s() //INVALID
161
+ #=> The BOB.data will not be set and you will get the output of: "<ul><li></li><li></li><li></li></ul>".
162
+ ```
163
+
164
+ ###Processing data and BOB.data
165
+ BOB.data is a function, so **you cannot manipulate `BOB.data` directly.**
166
+
167
+ It is adviced to do the data manipulation prior to the `do` pipe. However it is possible to manipulate BOB.data inline like this:
168
+
169
+ ```ruby
170
+
171
+ BOB.new("ul").do([1,2,3]).insert("li").content(lambda{return BOB.data() + 2}).s()
172
+ #=> <ul><li>3</li><li>4</li><li>5</li></ul>
173
+ //Or you can predefine a set of manipulations
174
+ data_modifier = lambda{return BOB.data() + 2}
175
+ BOB.new("ul").do([1,2,3]).insert("li").content(data_modifier).s()
176
+ #=> <ul><li>3</li><li>4</li><li>5</li></ul>
177
+ ```
178
+
179
+
180
+ <a name="shorthand"></a>
181
+ ###Short hand syntax
182
+ Writing out these pipes can be tiresom if you are building big and complex structures, so you can utilize these shorthand methods.
183
+
184
+ Long Version | Short Version
185
+ ------------ | -------------
186
+ .insert | .i
187
+ .append | .a
188
+ .prepend | .p
189
+ .content | .co
190
+ .style | .st
191
+ .classs | .cl
192
+ .id | .id
193
+ .style | .st
194
+ .toString | .s
195
+ .do | .d
196
+ .up | .u
197
+ BOB.data | BOB.d
198
+
199
+ Now you can get tight and cozy syntax like this:
200
+
201
+ ```ruby
202
+
203
+ BOB.new("div").i("img", {"src":"some.png"}).u().d([1,2,3]).i("p.number").co(BOB.d).s()
204
+ #=> "<div><img src="some.png"></img><p class="number">1</p><p class="number">2</p><p class="number">3</p></div>"
205
+ ```
206
+
207
+ ###Some complex examples
208
+ Better examples coming
209
+
210
+ ```ruby
211
+
212
+ data = ["Team member1", "team member2", "team member3"]
213
+ BOB.new("ul").do(data).insert("li.team").content(BOB.data).s()
214
+ #=> "<ul><li class="team">Team member1</li><li class="team">team member2</li><li class="team">team member3</li></ul>"
215
+
216
+ BOB.new("div#wrapper").insert("div#searchbar").up().insert("footer").do(["team","contact","buy"]).insert("h2").content(BOB.data).s()
217
+ #=> "<div id="wrapper"><div id="searchbar"></div><footer><h2>team</h2><h2>contact</h2><h2>buy</h2></footer></div>"
218
+
219
+ BOB.new("div#wrapper").insert("div#searchbar").up().insert("footer").do(["team","contact","buy"]).insert("h2",{"onclick": lambda{return ("alert('" + BOB.data() + "');") }}).content(BOB.data).s()
220
+ #=> "<div id="wrapper"><div id="searchbar"></div><footer><h2 onclick="alert('team');">team</h2><h2 onclick="alert('contact');">contact</h2><h2 onclick="alert('buy');">buy</h2></footer></div>"
221
+
222
+ BOB.new("div#wrapper").insert("div#searchbar").up().insert("footer").do(["team","contact","buy"]).insert("h2",{"onclick": lambda{return ("alert('" + BOB.data() + "');") }}).content(BOB.data).up().up().prepend("a",{"href": "http://www.google.com"}).content("google").s()
223
+ #=> "<a href="http://www.google.com">google</a><div id="wrapper"><div id="searchbar"></div><footer><h2 onclick="alert('team');">team</h2><h2 onclick="alert('contact');">contact</h2><h2 onclick="alert('buy');">buy</h2></footer></div>"
224
+ ```
225
+
226
+ ##Important notes:
227
+ Please help contribute to this project. It is brand new, and there are probably loads of features that can be added.
228
+
229
+ ###Planned features
230
+ - Adding nested data-aquisition data, eg: BOB.new("div").do(["a", "b"]).do([1,2]).in("a").classs(BOB.data[0]).co(BOB.data[1])
231
+ #=> <div><a class="a">1</a><a class="a">2</a><a class="b">1</a><a class="b">2</a></div>
232
+
233
+ ##License
234
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ task :default => :spec
5
+ RSpec::Core::RakeTask.new
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "BOB"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,149 @@
1
+ module BOB
2
+ class ChildArray
3
+ def initialize (dataset, parent)
4
+ @dataset = dataset
5
+ @parent = parent
6
+ @bobs = []
7
+ end
8
+
9
+ #No point in doing this on parent. Does not make sense to do ".do(data).id(BOB::Element.data)"
10
+ def content (content)
11
+ self.co(content)
12
+ end
13
+ def co (content)
14
+ @dataset.each_with_index do |item, i|
15
+ BOB::Element.data = item
16
+ @bobs[i].co(content) if @bobs[i]
17
+ end
18
+ return self
19
+ end
20
+ def style (style)
21
+ self.st(style)
22
+ end
23
+ def st (style)
24
+ @dataset.each_with_index do |item, i|
25
+ BOB::Element.data = item
26
+ @bobs[i].st(style) if @bobs[i]
27
+ end
28
+ return self
29
+ end
30
+ def classs (object_class)
31
+ self.cl(object_class)
32
+ end
33
+ def cl (object_class)
34
+ @dataset.each_with_index do |item, i|
35
+ BOB::Element.data = item
36
+ @bobs[i].cl(object_class) if @bobs[i]
37
+ end
38
+ return self
39
+ end
40
+ def id (object_id)
41
+ @dataset.each_with_index do |item, i|
42
+ BOB::Element.data = item
43
+ @bobs[i].id(object_id) if @bobs[i]
44
+ end
45
+ return self
46
+ end
47
+ def insert (data, options=nil)
48
+ self.i(data, options)
49
+ end
50
+ def i (data, options=nil)
51
+ @dataset.each_with_index do |item, i|
52
+ BOB::Element.data = item
53
+ if @bobs[i]
54
+ @bobs[i] = @bobs[i].insert(data, options)
55
+ else
56
+ @bobs.push(@parent.insert(data, options))
57
+ end
58
+ end
59
+
60
+ return self
61
+ end
62
+ def append (data, options=nil)
63
+ self.a(data, options)
64
+ end
65
+ def a (data, options=nil)
66
+ @dataset.each_with_index do |item, i|
67
+ BOB::Element.data = item
68
+ if @bobs[i]
69
+ @bobs[i] = @bobs[i].a(data, options)
70
+ else
71
+ @bobs.push(@parent.a(data, options))
72
+ end
73
+ end
74
+ return self
75
+ end
76
+
77
+ def prepend (data, options=nil)
78
+ self.p(data, options)
79
+ end
80
+ def p (data, options=nil)
81
+ @dataset.each_with_index do |item, i|
82
+ BOB::Element.data = item
83
+ if @bobs[i]
84
+ @bobs[i] = @bobs[i].p(data, options)
85
+ else
86
+ @bobs.push(@parent.p(data, options))
87
+ end
88
+ end
89
+
90
+ return self
91
+ end
92
+ def prettyPrint
93
+ self.pp()
94
+ end
95
+ def pp
96
+ self.s(true)
97
+ end
98
+ def toString
99
+ self.s()
100
+ end
101
+ def s (pretty = false)
102
+ if @parent
103
+ return @parent.s(pretty)
104
+ else
105
+ html_string = ""
106
+ for bob in @bobs
107
+ bob.parent = false
108
+ html_string += bob.s(pretty)
109
+ end
110
+ return html_string
111
+ end
112
+ end
113
+
114
+ #TODO: do(data).do(data2) does not behave correctly
115
+ def do (data)
116
+ self.d(data)
117
+ end
118
+ def d (data)
119
+ for bob in @bobs
120
+ BOB::Element.data = item
121
+ if @bobs[i]
122
+ @bobs[i] = @bobs[i].d(data)
123
+ else
124
+ @bobs.push(@parent.d(data))
125
+ end
126
+ end
127
+
128
+ return self
129
+ end
130
+ def up
131
+ self.u()
132
+ end
133
+ def u
134
+ unless @bobs[0]
135
+ BOB::Element.data = nil
136
+ return @parent
137
+ end
138
+ [0...@bobs.length].each do |i|
139
+ @bobs[i] = @bobs[i].u()
140
+ end
141
+ if @bobs[0] == @parent
142
+ BOB::Element.data = nil
143
+ return @parent
144
+ else
145
+ return self
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,259 @@
1
+ # BOB
2
+
3
+ # build or bail
4
+ module BOB
5
+ class Element
6
+ attr_accessor :object_content, :parent
7
+ @@data = nil
8
+
9
+ def self.find(selector)
10
+ #TODO: this selects an existing element, and append/prepend/inserts into that
11
+ end
12
+ def self.data
13
+ return @@data
14
+ end
15
+ def self.data=(data)
16
+ @@data = data
17
+ end
18
+ def self.d
19
+ return @@data
20
+ end
21
+ def self.get_or_create_bob(data, options, parent)
22
+ child_bob = nil
23
+ if data.is_a?(Element)
24
+ child_bob = data
25
+ else
26
+ child_bob = Element.new(data, options, parent)
27
+ end
28
+ return child_bob
29
+ end
30
+
31
+ def self.toVariable(data)
32
+ if data.respond_to?(:call)
33
+ return data.call
34
+ else
35
+ return data
36
+ end
37
+ end
38
+
39
+ def initialize (selector, options=nil, parent=nil, preBob=nil, contentBob=nil, postBob=nil)
40
+ if selector.include?(" ")
41
+ raise StandardError, "Invalid Element selector. \"" + selector.to_s + "\" contains \" \"(space). Only allowed is \"tag\", \"tag.class\", or \"tag#id\"."
42
+ end
43
+
44
+ @parent = parent
45
+
46
+ #Flatting so Element.data gets parsed out to the correct thing.
47
+ @options = {}
48
+ if options
49
+ options.each do |key, value|
50
+ @options[key] = Element.toVariable(value)
51
+ end
52
+ end
53
+
54
+ @preBob = preBob
55
+ @innerBob = contentBob
56
+ @postBob = postBob
57
+
58
+ @type = selector
59
+ @object_class = nil
60
+ @object_id = nil
61
+ @object_content = ""
62
+ @object_style = nil
63
+
64
+ if selector.include?(".")
65
+ @type, @object_class = selector.split(".")
66
+ elsif selector.include?("#")
67
+ @type, @object_id = selector.split("#")
68
+ end
69
+ end
70
+
71
+ def content (content)
72
+ self.co(content)
73
+ end
74
+ def co (content)
75
+ child = self.i("")
76
+ child.object_content = Element.toVariable(content)
77
+ return self
78
+ end
79
+ def style (style)
80
+ self.st(style)
81
+ end
82
+ def st (style)
83
+ @object_style = Element.toVariable(style)
84
+ return self
85
+ end
86
+ def classs (object_class)
87
+ self.cl(object_class)
88
+ end
89
+ def cl (object_class)
90
+ @object_class = Element.toVariable(object_class)
91
+ return self
92
+ end
93
+ def id (object_id)
94
+ @object_id = Element.toVariable(object_id)
95
+ return self
96
+ end
97
+ def insert (data, options=nil)
98
+ self.i(data,options)
99
+ end
100
+ def i (data, options=nil)
101
+ child_bob = Element.get_or_create_bob(data, options, self)
102
+ if @innerBob
103
+ @innerBob.a(child_bob)
104
+ else
105
+ @innerBob = child_bob
106
+ end
107
+ return child_bob
108
+ end
109
+ def append (data, options=nil)
110
+ self.a(data, options)
111
+ end
112
+ def a (data, options=nil)
113
+ par = self
114
+ par = @parent if @parent
115
+ new_bob = Element.get_or_create_bob(data, options, par)
116
+ if @postBob
117
+ @postBob.a(new_bob)
118
+ else
119
+ @postBob = new_bob
120
+ end
121
+ end
122
+ def prepend (data, options=nil)
123
+ self.p(data, options)
124
+ end
125
+ def p (data, options=nil)
126
+ par = self
127
+ par = @parent if @parent
128
+ new_bob = Element.get_or_create_bob(data, options, par)
129
+ if @preBob
130
+ @preBob.p(new_bob)
131
+ else
132
+ @preBob = new_bob
133
+ end
134
+ end
135
+ def do (dataset)
136
+ self.d(dataset)
137
+ end
138
+ def d (dataset)
139
+ child_array = ChildArray.new(dataset, self)
140
+ # @doData = dataset
141
+ # @inDO = true
142
+ return child_array
143
+ end
144
+ def up
145
+ @parent
146
+ end
147
+ def u
148
+ @parent
149
+ end
150
+ def prettyPrint
151
+ self.pp()
152
+ end
153
+ def pp
154
+ self.s(true)
155
+ end
156
+ def toString
157
+ self.s()
158
+ end
159
+ def s (pretty = false)
160
+ #this makes the toString bubble to the top if it is cast on a sub-element
161
+ if @parent
162
+ return @parent.s(pretty)
163
+ end
164
+
165
+ #kill parents so they will print out.
166
+ @innerBob.parent = nil if @innerBob
167
+ @preBob.parent = nil if @preBob
168
+ @postBob.parent = nil if @postBob
169
+
170
+ prepend = ''
171
+ append = ''
172
+ printself = ''
173
+ content_b = ''
174
+
175
+ content_b = @innerBob.s(pretty) if @innerBob
176
+ prepend = @preBob.s(pretty) if @preBob
177
+ append = @postBob.s(pretty) if @postBob
178
+ #TODO: Make special case for img (or those without content?) and no-type tag, which is pure text content.
179
+
180
+ if @type != ""
181
+ printself += '<' + @type + ' '
182
+ @options.each do |key, value|
183
+ unless key == 'style' && @object_style || key == 'id' && @object_id || key == 'class' && @object_class
184
+ printself << key.to_s() + '="' + value.to_s() + '" '
185
+ end
186
+ end
187
+
188
+ printself << 'class="' + @object_class.to_s() + '" ' if @object_class
189
+ printself << 'id="' + @object_id.to_s() + '" ' if @object_id
190
+ printself << 'style="' + @object_style.to_s() + '" ' if @object_style
191
+
192
+ printself = printself[0..-2]
193
+ closable = (["area",
194
+ "base",
195
+ "br",
196
+ "col",
197
+ "embed",
198
+ "hr",
199
+ "img",
200
+ "input",
201
+ "keygen",
202
+ "link",
203
+ "menuitem",
204
+ "meta",
205
+ "param",
206
+ "source",
207
+ "track",
208
+ "wbr",
209
+ "basefont",
210
+ "bgsound",
211
+ "frame",
212
+ "isindex"].include?(@type))
213
+
214
+ if closable && content_b == ''
215
+ printself << ' />'
216
+ else
217
+ if pretty
218
+ if content_b && content_b != ""
219
+ content_b = "\n\t" + content_b.gsub("\n", "\n\t").to_s() + "\n"
220
+ else
221
+ content_b = "\n"
222
+ end
223
+ end
224
+ printself.to_s() << '>' + content_b.to_s() + '</' + @type.to_s() + '>'
225
+ end
226
+ else
227
+ #pure text element (no type)
228
+ printself = @object_content #it should not have any innerBob as it is never exposed when we are setting object_content
229
+ end
230
+ if pretty
231
+ prepend = prepend + "\n\t" if prepend && prepend != ""
232
+ printself = printself.gsub("\n", "\n\t") if prepend && prepend != ""
233
+ append = "\n" + append if append && append != ""
234
+ end
235
+
236
+
237
+ return prepend.to_s() + printself.to_s() + append.to_s()
238
+ end
239
+
240
+ # Element.new("div",{test: "lol"}).do(["data"]).add("p",funtion(d){self.insert("div",{a: d.height})})
241
+
242
+ # Element.new("ul",{class: "lol"})
243
+ # .do(dataset)
244
+ # .insert("li", {dataProp: Element.data}) #?
245
+ # .content(Element.data) #?
246
+ # .insert("p")
247
+ # .content("lol")
248
+ # .up()
249
+ # .up()
250
+ # .insert("p")
251
+ # .content("lol2")
252
+ # .up()
253
+ # .append("ul",{class: "shiet"})
254
+ # .append("ul",{class: "shiet"})
255
+ # .prepend("ul",{class: "shiet"})
256
+ # .toString();
257
+
258
+ end
259
+ end
@@ -0,0 +1,3 @@
1
+ module BOB
2
+ VERSION = "0.1.0"
3
+ end
data/lib/BOB.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "BOB/version"
2
+ require "BOB/element"
3
+ require "BOB/child_array"
4
+
5
+ module BOB
6
+ # Your code goes here...
7
+ def self.new(*args)
8
+ BOB::Element.new(*args)
9
+ end
10
+
11
+ def self.data(*args)
12
+ BOB::Element.method(:data)
13
+ end
14
+ def self.d(*args)
15
+ BOB::Element.method(:d)
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: BOBrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stephan Nordnes Eriksen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codeclimate-test-reporter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.4'
69
+ description: BOB is a simple and powerfull ruby pipe system for building complex XML
70
+ and HTML structures.
71
+ email:
72
+ - stephanruler@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - BOB.gemspec
81
+ - BOB.png
82
+ - CODE_OF_CONDUCT.md
83
+ - Gemfile
84
+ - Gemfile.lock
85
+ - LICENSE.txt
86
+ - README.md
87
+ - Rakefile
88
+ - bin/console
89
+ - bin/setup
90
+ - lib/BOB.rb
91
+ - lib/BOB/child_array.rb
92
+ - lib/BOB/element.rb
93
+ - lib/BOB/version.rb
94
+ homepage: https://github.com/stephan-nordnes-eriksen/BOBrb
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.7
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: 'BOB: Powerful XML and HTML building'
118
+ test_files: []