Class: HashToHTMLList

Inherits:
Object
  • Object
show all
Defined in:
lib/twb/hashtohtml.rb,
lib/twb/util/hashtohtml.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ HashToHTMLList

Returns a new instance of HashToHTMLList.


2
3
4
5
6
7
8
# File 'lib/twb/hashtohtml.rb', line 2

def initialize(hash)
  @hash      = hash
  @indent    = "  "
  @tag_space = ""
  @level     = 0
  @out       = []
end

Instance Method Details

#append(tag, value = nil) ⇒ Object


10
11
12
13
14
15
# File 'lib/twb/hashtohtml.rb', line 10

def append(tag,value=nil)
  str = @indent * @level + "#{tag}"
  str += @tag_space + value unless value.nil?
  str += "\n"
  @out << str
end

#li(collection) ⇒ Object


21
22
23
24
25
26
27
# File 'lib/twb/hashtohtml.rb', line 21

def li(hash)
  @level += 1
  hash.each do |key,value|
    open_tag('li',key) { ul(value) if value.is_a?(Hash) }
  end
  @level -= 1
end

#listObject


29
30
31
32
# File 'lib/twb/hashtohtml.rb', line 29

def list
  ul(@hash)
  @out.join
end

#open_tag(tag, value = nil, &block) ⇒ Object


34
35
36
37
38
# File 'lib/twb/hashtohtml.rb', line 34

def open_tag(tag,value=nil,&block)
  append("<#{tag}>",value)
  yield if block_given?
  append("</#{tag}>")
end

#to_sObject


41
42
43
# File 'lib/twb/util/hashtohtml.rb', line 41

def to_s
  @out
end

#ul(hash) ⇒ Object


17
18
19
# File 'lib/twb/hashtohtml.rb', line 17

def ul(hash)
  open_tag('ul') { li(hash) }
end