Class: Garterbelt::ClosedTag

Inherits:
Renderer show all
Defined in:
lib/renderers/closed_tag.rb

Direct Known Subclasses

ContentTag, Doctype, Xml

Instance Attribute Summary collapse

Attributes inherited from Renderer

#escape, #style, #view

Instance Method Summary collapse

Methods inherited from Renderer

#indent, #level, #line_end, #output, #output=

Constructor Details

#initialize(opts) ⇒ ClosedTag

Returns a new instance of ClosedTag.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/renderers/closed_tag.rb', line 5

def initialize(opts)
  super
  self.type = opts[:type] || raise(ArgumentError, ":type required in initialization options")
  self.attributes = opts[:attributes] || {}
  
  css_class = attributes.delete(:class)
  self.css_class = if css_class
    css_class.is_a?(Array) ? css_class : [css_class]
  else
    []
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/renderers/closed_tag.rb', line 3

def attributes
  @attributes
end

#css_classObject

Returns the value of attribute css_class.



3
4
5
# File 'lib/renderers/closed_tag.rb', line 3

def css_class
  @css_class
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/renderers/closed_tag.rb', line 3

def type
  @type
end

Instance Method Details

#c(*args) ⇒ Object



26
27
28
29
# File 'lib/renderers/closed_tag.rb', line 26

def c(*args)
  self.css_class += args
  self
end

#id(identifier) ⇒ Object

Convenience method chaining —————————

Raises:

  • (ArgumentError)


20
21
22
23
24
# File 'lib/renderers/closed_tag.rb', line 20

def id(identifier)
  raise ArgumentError, "Id must be a String or Symbol" unless [String, Symbol].include?(identifier.class)
  self.attributes[:id] = identifier
  self
end

#renderObject



55
56
57
58
59
# File 'lib/renderers/closed_tag.rb', line 55

def render
  str = template
  self.output << str
  str
end

#rendered_attributesObject

Rendering ———————————————–



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

def rendered_attributes
  str = ""
  str << " class=\"#{css_class.join(' ')}\"" unless css_class.empty?
  keys = attributes.keys.sort{|a, b| a.to_s <=> b.to_s}
  keys.each do |key|
    value = attributes[key]
    if value
      value = value.to_s.gsub('"', '\'')
      str << " #{key}=\"#{value}\""
    end
  end
  str
end

#templateObject



47
48
49
50
51
52
53
# File 'lib/renderers/closed_tag.rb', line 47

def template
  if style == :text 
    type == :br ? "\n" : ""
  else 
    "#{indent}<#{type}#{rendered_attributes}>#{line_end}"
  end
end