Sha256: 4dc1099fc054067b59455619c097aa0c47693ad5a4d55c8d3ae0d2170631bce8

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Troo
  module Commands
    class Status
      class << self
        # @param  []
        # @return [String]
        def dispatch(klass)
          new(klass).report_status
        end
      end

      # @param  []
      # @return []
      def initialize(klass)
        @klass = klass
      end

      # @return [String]
      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

1 entries across 1 versions & 1 rubygems

Version Path
troo-0.0.13 lib/troo/cli/commands/status.rb