Sha256: fb367bf9ecc15f502f0ff66097864bb760c4e7c01b22f79c615f565ae7291904

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

namespace :praxis do

  desc 'List routes, format=json or table, default table'
  task :routes, [:format] => [:environment] do |t, args|
    require 'terminal-table'

    table = Terminal::Table.new title: "Routes",
    headings:  [
      "Version", "Path", "Verb",
      "Resource", "Action", "Implementation", "Options"
    ]

    rows = []
    Praxis::Application.instance.resource_definitions.each do |resource_definition|
      resource_definition.actions.each do |name, action|
        method = begin
          m = resource_definition.controller.instance_method(name)
        rescue
          nil
        end

        method_name = method ? "#{method.owner.name}##{method.name}" : 'n/a'

        row = {
          resource: resource_definition.name,
          action: name,
          implementation: method_name,
        }

        if action.routes.empty?
          warn "Warning: No routes defined for #{resource_definition.name}##{name}."
          rows << row
        else
          route = action.route
          rows << row.merge({
            version: route.version,
            verb: route.verb,
            path: route.path,
            options: route.options
          })
        end
      end
    end
    case args[:format] || "table"
    when "json"
      puts JSON.pretty_generate(rows)
    when "table"
      rows.each do |row|
        formatted_options = row[:options].map{|(k,v)| "#{k}:#{v.to_s}"}.join("\n")
        row_data = row.values_at(:version, :path, :verb, :resource,
                                    :action, :implementation)
        row_data << formatted_options
        table.add_row(row_data)
      end
      puts table
    else
      raise "unknown output format: #{args[:format]}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
praxis-2.0.pre.6 lib/praxis/tasks/routes.rb