Class: HashToHTMLList
- Inherits:
-
Object
- Object
- HashToHTMLList
- Defined in:
- lib/twb/hashtohtml.rb,
lib/twb/util/hashtohtml.rb
Instance Method Summary collapse
- #append(tag, value = nil) ⇒ Object
-
#initialize(hash) ⇒ HashToHTMLList
constructor
A new instance of HashToHTMLList.
- #li(collection) ⇒ Object
- #list ⇒ Object
- #open_tag(tag, value = nil, &block) ⇒ Object
- #to_s ⇒ Object
- #ul(hash) ⇒ Object
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 |
#list ⇒ Object
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_s ⇒ Object
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 |