Class: Rubified::Tag

Inherits:
Object show all
Defined in:
lib/rubified/tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, embedded = false) ⇒ Tag

The arguments to this method is a hash key of the parameters to the HTML tag.



12
13
14
15
16
17
18
19
# File 'lib/rubified/tag.rb', line 12

def initialize(params={}, embedded=false)
  # The name of the tag, e.g. font
  @tname = self.class.to_s.split("::").last.downcase
  # Any parameters of this tag, e.g. if you have the HTML tag:
  #   <div id="foo" class="bar">
  # then the parameters would be +id+ and +class+.
  @params, @embedded = params, embedded
end

Class Method Details

.new_tag(name, paired = true) ⇒ Object

Create and returns a new tag class with the name name. To create a tag that does NOT use a matched pair, set paired to false. Tags like <img> need this.



4
5
6
7
8
9
# File 'lib/rubified/tag.rb', line 4

def self.new_tag(name, paired=true)
  newbie = Class.new(self)
  const_set(name.capitalize, newbie)
  newbie.const_set(:Paired, paired)
  newbie
end

Instance Method Details

#to_html(embedded) ⇒ Object

Dumps this tag to an HTML string.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubified/tag.rb', line 22

def to_html(embedded)
  pstring = ""
  @params.each {|key, val| pstring << " #{key}=\"#{val}\""}
  raw = "<#{@tname}#{pstring}>\n"
  if block_given?
    raw << (yield.to_html(true))
  end
  if self.class::Paired
    raw << "</#{@tname}>\n"
  end
  raw
end