Sha256: eef2a149b38193c3afb521062e8adbf401d622465a56091a4a3eacbd0b1b09a2

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

module Garterbelt
  class ClosedTag < Renderer
    attr_accessor :type, :attributes, :css_class
    
    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
    
    # Convenience method chaining ---------------------------
    
    def id(identifier)
      raise ArgumentError, "Id must be a String or Symbol" unless [String, Symbol].include?(identifier.class)
      self.attributes[:id] = identifier
      self
    end
    
    def c(*args)
      self.css_class += args
      self
    end
    
    # Rendering -----------------------------------------------
    
    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
    
    def template
      if style == :text 
        type == :br ? "\n" : ""
      else 
        "#{indent}<#{type}#{rendered_attributes}>#{line_end}"
      end
    end
    
    def render
      str = template
      self.output << str
      str
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
garterbelt-0.1.1 lib/renderers/closed_tag.rb
garterbelt-0.1.0 lib/renderers/closed_tag.rb
garterbelt-0.0.9 lib/renderers/closed_tag.rb
garterbelt-0.0.8 lib/renderers/closed_tag.rb