Sha256: ab97b0d3ecd96608b9f2e8179991e0e08a605e2fc3e8b514f6e2f3cd028d30b2

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require "erb"
require "cgi"

module Vanity
  
  # Render method available to templates (when used by Vanity command line,
  # outside Rails).
  module Render
    
    # Render the named template.  Used for reporting and the dashboard.
    def render(path, locals = {})
      locals[:playground] = self
      keys = locals.keys
      struct = Struct.new(*keys)
      struct.send :include, Render
      locals = struct.new(*locals.values_at(*keys))
      dir, base = File.split(path)
      path = File.read(File.join(dir, "_#{base}"))
      ERB.new(path, nil, '<').result(locals.instance_eval { binding })
    end

    # Escape HTML.
    def h(html)
      CGI.escape_html(html)
    end

  end

  # Commands available when running Vanity from the command line (see bin/vanity).
  module Commands
    class << self
      include Render

      # Generate an HTML report.  Outputs to the named file, or stdout with no
      # arguments.
      def report(output = nil)
        html = render(Vanity.template("report"))
        if output
          File.open output, 'w' do |file|
            file.write html
          end
          puts "New report available in #{output}"
        else
          $stdout.write html
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vanity-1.0.0 lib/vanity/commands/report.rb
vanity-0.4.0 lib/vanity/commands/report.rb