Class: BlockHTML

Inherits:
Object
  • Object
show all
Defined in:
lib/block-html.rb,
lib/block-html/form.rb

Direct Known Subclasses

DOCTYPE, Form, Form::Element, Tag, Text, XML

Defined Under Namespace

Classes: Attrs, DOCTYPE, EscapedText, Form, Renderer, Tag, Text, XML

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env_instance = nil, &block) ⇒ BlockHTML

Returns a new instance of BlockHTML.



4
5
6
7
8
9
10
11
12
# File 'lib/block-html.rb', line 4

def initialize(env_instance=nil, &block)
  @parent, @nodes, @env_instance = nil, [], env_instance

  @env_instance && @env_instance.instance_variables.each do |v|
    self.instance_variable_set(v, @env_instance.instance_variable_get(v))
  end

  instance_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(tag, attrs = {}, &block) ⇒ Object



86
87
88
# File 'lib/block-html.rb', line 86

def method_missing(tag, attrs={}, &block)
  tag(tag, attrs, &block)
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



2
3
4
# File 'lib/block-html.rb', line 2

def parent
  @parent
end

Instance Method Details

#<<(tree) ⇒ Object



14
15
16
17
18
# File 'lib/block-html.rb', line 14

def <<(tree)
  tree.parent = self
  @nodes << tree
  tree
end

#_pObject



58
# File 'lib/block-html.rb', line 58

alias :_p :p

#doctype(attrs = {}) ⇒ Object



50
51
52
# File 'lib/block-html.rb', line 50

def doctype(attrs={})
  self << DOCTYPE.new(attrs)
end

#each(&block) ⇒ Object



38
39
40
# File 'lib/block-html.rb', line 38

def each(&block)
  @nodes.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/block-html.rb', line 42

def empty?
  @nodes.empty?
end

#escaped_text(text = '') ⇒ Object



69
70
71
72
# File 'lib/block-html.rb', line 69

def escaped_text(text='')
  self << EscapedText.new(text)
  self
end

#form(attrs = {}, &block) ⇒ Object



74
75
76
77
78
# File 'lib/block-html.rb', line 74

def form(attrs={}, &block)
  self << Form.new(@env_instance) {
    form(attrs, &block)
  }
end

#p(attrs = {}, &block) ⇒ Object



60
61
62
# File 'lib/block-html.rb', line 60

def p(attrs={}, &block)
  tag('p', attrs, &block)
end

#path(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/block-html.rb', line 20

def path(&block)
  node = if block_given?
    block.call(self)
  else
    self
  end

  if parent.nil?
    [node]
  else
    parent.path(&block).push(node)
  end
end

#render(renderer) ⇒ Object



80
81
82
83
84
# File 'lib/block-html.rb', line 80

def render(renderer)
  each do |node|
    node.render(renderer)
  end
end

#rootObject



34
35
36
# File 'lib/block-html.rb', line 34

def root
  path.shift
end

#tag(tag, attrs = {}, &block) ⇒ Object



54
55
56
# File 'lib/block-html.rb', line 54

def tag(tag, attrs={}, &block)
  self << Tag.new(tag, attrs, @env_instance, &block)
end

#text(text = '') ⇒ Object



64
65
66
67
# File 'lib/block-html.rb', line 64

def text(text='')
  self << Text.new(text)
  self
end

#to_s(indent = 0) ⇒ Object



90
91
92
# File 'lib/block-html.rb', line 90

def to_s(indent=0)
  Renderer.new(self, indent).to_s
end

#xml(attrs = {}) ⇒ Object



46
47
48
# File 'lib/block-html.rb', line 46

def xml(attrs={})
  self << XML.new(attrs)
end