Sha256: bf4c381e5041f9b394988163c028badb279a3445a40af1f80853fc2a1e2c7c6c

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Troo
  module Commands
    class Show
      attr_reader :id, :type

      class << self
        # @param  []
        # @param  []
        # @return [String]
        def dispatch(klass, id = nil)
          new(klass, id).render
        end
      end

      # @param  []
      # @param  []
      # @return [Troo::Commands::Show]
      def initialize(klass, id = nil)
        @klass, @id = klass, id
      end

      # @return [String]
      def render
        if resource
          presenter
        elsif no_default?
          [error, no_default].join(' ')
        else
          error
        end
      end

      private

      attr_reader :klass

      def presenter
        resource.presenter.show
      end

      def error
        "#{type.capitalize} cannot be found."
      end

      def no_default?
        id.nil? && resource.nil?
      end

      def no_default
        "Specify an <id> or use 'troo default #{type} <id>' " \
        "to set a default #{type} first."
      end

      def type
        klass.type.to_s
      end

      def resource
        @resource ||= klass.retrieve(id)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
troo-0.0.13 lib/troo/cli/commands/show.rb
troo-0.0.12 lib/troo/cli/commands/show.rb
troo-0.0.11 lib/troo/cli/commands/show.rb