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_templates-0.11.10 lib/express_templates/indenter.rb
express_templates-0.11.9 lib/express_templates/indenter.rb
express_templates-0.11.8 lib/express_templates/indenter.rb
express_templates-0.11.7 lib/express_templates/indenter.rb
express_templates-0.11.6 lib/express_templates/indenter.rb
express_templates-0.11.5 lib/express_templates/indenter.rb
express_templates-0.11.4 lib/express_templates/indenter.rb
express_admin-1.7.5 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.7.4 vendor/gems/express_templates/express_templates/lib/express_templates/indenter.rb
express_admin-1.7.3 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.7.2 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.7.1 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.6.13 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.6.12 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.6.11 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.6.10 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.6.9 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.6.8 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_admin-1.6.7 vendor/gems/express_templates/lib/express_templates/indenter.rb
express_templates-0.11.3 lib/express_templates/indenter.rb