Sha256: 5070e59e3330eaf69fbe8bc88d5401afcaa470a0a5ff6a66243520df83d381f6

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

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

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

      # @param  []
      # @param  [, NilClass]
      # @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

2 entries across 2 versions & 1 rubygems

Version Path
troo-0.0.15 lib/troo/cli/commands/show.rb
troo-0.0.14 lib/troo/cli/commands/show.rb