Sha256: 61f98234988a923db5387f31d6e4138a0704e12389812b22a65bf806be3c52b3

Contents?: true

Size: 1.28 KB

Versions: 68

Compression:

Stored size: 1.28 KB

Contents

module ExpressTemplates

  # Tracks current indent level scoped to the current thread.
  #
  # May be used to track multiple indents simultaneously through
  # namespacing.
  class Indenter

    DEFAULT = 2
    WHITESPACE = " "*DEFAULT

    # Returns whitespace for the named indenter or yields to a block
    # for the named indentor.
    #
    # The block is passed the current whitespace indent.
    #
    # For convenience an optional second parameter is passed to the block
    # containing a newline at the beginning of the indent.
    def self.for name
      if block_given?
        current_indenters[name] += 1
        begin
          indent = WHITESPACE * current_indenters[name]
          yield indent, "\n#{indent}"
        ensure
          if current_indenters[name].eql?(-1)
            # if we have long-lived threads for some reason
            # we want to clean up after ourselves
            current_indenters.delete(name)
          else
            current_indenters[name] -= 1
          end
        end
      else
        return WHITESPACE * current_indenters[name]
      end
    end

    private
      # For thread safety, scope indentation to the current thread
      def self.current_indenters
        Thread.current[:indenters] ||= Hash.new {|hsh, key| hsh[key] = -1 }
      end

  end

end

Version data entries

68 entries across 68 versions & 2 rubygems

Version Path
express_admin-1.4.5 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_templates-0.9.4 lib/express_templates/indenter.rb
express_templates-0.9.3 lib/express_templates/indenter.rb
express_admin-1.4.4 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.4.3 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_templates-0.9.1 lib/express_templates/indenter.rb
express_admin-1.4.2 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_templates-0.9.0 lib/express_templates/indenter.rb
express_admin-1.4.1 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.4.0 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_templates-0.8.0 lib/express_templates/indenter.rb
express_admin-1.3.2 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.3.1 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_templates-0.7.1 lib/express_templates/indenter.rb
express_templates-0.7.0 lib/express_templates/indenter.rb
express_admin-1.3.0 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.2.1 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.2.0 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_templates-0.5.0 lib/express_templates/indenter.rb
express_templates-0.4.2 lib/express_templates/indenter.rb