Sha256: a7529f4009f6285b524579586cfb9a154c4655b3474a0662bf8a6a5bf035c4b3

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

module Troo
  module Commands
    class Status
      class << self
        def dispatch(klass)
          new(klass).report_status
        end
      end

      def initialize(klass)
        @klass = klass
      end

      def report_status
        return success if resource
        error
      end

      private

      attr_reader :klass

      def success
        label + plural + " found.\n" + resource_title
      end

      def error
        if count > 0
          label + no_default_error + " (#{count})"
        else
          label + "No #{type}s found.\n"
        end
      end

      def label
        "  #{type.capitalize}s:".ljust(10)
      end

      def resource_title
        ''.rjust(10) + resource.decorator.title
      end

      def no_default_error
        Esc.red + "No default #{type} set." + Esc.reset
      end

      def plural
        if count == 1
          "#{count} #{type}"
        else
          "#{count} #{type}s"
        end
      end

      def type
        klass.type.to_s
      end

      def resource
        @resource ||= klass.default
      end

      def count
        @count ||= klass.count
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
troo-0.0.10 lib/troo/cli/commands/status.rb
troo-0.0.9 lib/troo/cli/commands/status.rb