html_builder 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.
@@ -1,16 +1,17 @@
1
1
  module HTML
2
2
  class Builder < BasicObject
3
- def self.build(namespace = ::HTML, &block)
4
- new(namespace).build(&block)
3
+ def self.build(object = nil, namespace = ::HTML, &block)
4
+ new(object, namespace).build(&block)
5
5
  end
6
6
 
7
- def initialize(namespace = ::HTML)
7
+ def initialize(object = nil, namespace = ::HTML)
8
8
  @namespace = namespace
9
+ @object = object
9
10
  end
10
11
 
11
12
  def build(&block)
12
13
  if block.arity > 0
13
- yield self
14
+ instance_exec(@object, &block)
14
15
  else
15
16
  instance_eval(&block)
16
17
  end
@@ -1,17 +1,15 @@
1
1
  require "html/builder"
2
2
  require "html/tag"
3
- require "html/sequence"
4
3
 
5
4
  module HTML
6
5
  class List
7
- def initialize(enum)
6
+ def initialize(tag, enum)
7
+ @tag = tag
8
8
  @enum = enum
9
9
  end
10
10
 
11
11
  def to_html
12
- Builder.build do |html|
13
- html.tag(:ul, html.sequence(:li, @enum))
14
- end
12
+ @enum.map { |item| Tag.new(@tag, item).to_html }.join
15
13
  end
16
14
  end
17
15
  end
@@ -0,0 +1,38 @@
1
+ require "html/tag"
2
+ require "html/builder"
3
+
4
+ module HTML
5
+ class Table < Struct.new(:data)
6
+ def to_html
7
+ Builder.build(data) do |data|
8
+ tag(:table, thead(data) + tbody(data))
9
+ end
10
+ end
11
+ end
12
+
13
+ class Thead < Struct.new(:data)
14
+ def to_html
15
+ return "" unless data && data.respond_to?(:columns)
16
+ Builder.build(data.columns) do |cols|
17
+ tag(:thead, tr(cols))
18
+ end
19
+ end
20
+ end
21
+
22
+ class Tbody < Struct.new(:data)
23
+ def to_html
24
+ return "" unless data && data.respond_to?(:rows)
25
+ Builder.build(data.rows) do |rows|
26
+ tag(:tbody, rows.map { |cols| tr(cols) })
27
+ end
28
+ end
29
+ end
30
+
31
+ class Tr < Struct.new(:columns)
32
+ def to_html
33
+ Builder.build(columns) do |cols|
34
+ tag(:tr, list(:td, cols))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -6,7 +6,8 @@ module HTML
6
6
  end
7
7
 
8
8
  def to_html
9
- "<#{@tag}>#{@content}</#{@tag}>"
9
+ content = [*@content].map(&:to_s).join
10
+ "<#{@tag}>#{content}</#{@tag}>"
10
11
  end
11
12
  end
12
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-19 00:00:00.000000000 Z
12
+ date: 2012-05-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -19,7 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - lib/html/builder.rb
21
21
  - lib/html/list.rb
22
- - lib/html/sequence.rb
22
+ - lib/html/table.rb
23
23
  - lib/html/tag.rb
24
24
  - README.md
25
25
  homepage: https://github.com/smartlogic/html_builder
@@ -1,17 +0,0 @@
1
- require "html/builder"
2
- require "html/tag"
3
-
4
- module HTML
5
- class Sequence
6
- def initialize(tag, enum)
7
- @tag = tag
8
- @enum = enum
9
- end
10
-
11
- def to_html
12
- Builder.build do |html|
13
- @enum.map { |item| html.tag @tag, item }.join
14
- end
15
- end
16
- end
17
- end