Sha256: d45faae7cd1c4b29d607847fa9e3bec0ac8ec365555c05819c37fd788dde7699

Contents?: true

Size: 963 Bytes

Versions: 2

Compression:

Stored size: 963 Bytes

Contents

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

      class << self
        def dispatch(klass, id = nil)
          new(klass, id).render
        end
      end

      def initialize(klass, id = nil)
        @klass, @id = klass, id
      end

      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.10 lib/troo/cli/commands/show.rb
troo-0.0.9 lib/troo/cli/commands/show.rb