Class: TagParts::TagItem

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tagparts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, body, ignore_empty = false) ⇒ TagItem

Returns a new instance of TagItem.



16
17
18
19
20
21
22
23
24
# File 'lib/tagparts.rb', line 16

def initialize tag, body, ignore_empty = false
  @tag  = tag.to_s
  @attr = {}
  @body = []
  @ignore_empty = ignore_empty
  body.each{|e|
    add! e
  }
end

Instance Attribute Details

#attrObject (readonly)

Returns the value of attribute attr.



25
26
27
# File 'lib/tagparts.rb', line 25

def attr
  @attr
end

#bodyObject (readonly)

Returns the value of attribute body.



25
26
27
# File 'lib/tagparts.rb', line 25

def body
  @body
end

#tagObject (readonly)

Returns the value of attribute tag.



25
26
27
# File 'lib/tagparts.rb', line 25

def tag
  @tag
end

Instance Method Details

#[](k) ⇒ Object



60
61
62
# File 'lib/tagparts.rb', line 60

def [](k)
  @attr[k]
end

#[]=(k, v) ⇒ Object



64
65
66
# File 'lib/tagparts.rb', line 64

def []=(k, v)
  @attr[k] = v
end

#add!(elem) ⇒ Object Also known as: <<



52
53
54
55
56
57
58
# File 'lib/tagparts.rb', line 52

def add!(elem)
  if elem.kind_of? Hash
    @attr.update elem
  else
    @body << elem
  end
end

#eachObject



68
69
70
71
72
# File 'lib/tagparts.rb', line 68

def each
  @body.flatten.each{|e|
    yield e
  }
end

#each_leafObject



74
75
76
77
78
79
80
81
82
# File 'lib/tagparts.rb', line 74

def each_leaf
  @body.each{|e|
    if e.kind_of? TagItem
      e.each_leaf(&Proc.new)
    else
      yield e
    end
  }
end

#each_node {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



84
85
86
87
88
89
90
91
92
93
# File 'lib/tagparts.rb', line 84

def each_node
  yield(self)
  @body.each{|e|
    if e.kind_of? TagItem
      e.each_node(&Proc.new)
    else
      yield e
    end
  }
end

#inspectObject



48
49
50
# File 'lib/tagparts.rb', line 48

def inspect
  "<TagItem: <#{@tag}#{make_attr_str}>>"
end

#make_attr_strObject



27
28
29
30
31
# File 'lib/tagparts.rb', line 27

def make_attr_str
  @attr.map{|k,v|
    " #{CGI.escapeHTML(k.to_s)}='#{CGI.escapeHTML(v)}'"
  }.join
end

#to_sObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tagparts.rb', line 33

def to_s
  if @body.size > 0 || @ignore_empty
    body = @body.flatten.map{|e|
      if e.kind_of? String
        CGI.escapeHTML(e.to_s)
      else
        e.to_s
      end
    }
    "<#{@tag}#{make_attr_str}\n>#{body}</#{@tag}>\n"
  else
    "<#{@tag}#{make_attr_str} /\n>"
  end
end