Sha256: 974fe9f44e06e28829bf75c28f5679911029cf18b4322bcbfee8f8d079a02b2a

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

module Troo
  module Commands
    class Refresh
      attr_reader :id

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

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

      # @return []
      def refresh
        return not_found    if not_found?
        return many_success if multiple_refreshed?
        success
      end

      private

      attr_reader :klass

      def many_success
        "Multiple #{type}s refreshed."
      end

      def multiple_refreshed?
        resource.count > 1
      end

      def success
        "'#{resource_name}' refreshed."
      end

      def not_found
        return [error, no_default].join(' ') unless id
        error
      end

      def not_found?
        resource.nil? || resource.none?
      end

      def error
        "#{type.capitalize} cannot be found."
      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_name
        resource.first.decorator.name
      end

      def resource
        return [] unless local
        remote
      end

      def remote
        @remote ||= Retrieval::Remote
          .fetch(klass.remote, local.external_id)
      end

      def local
        @local ||= Retrieval::Local
          .retrieve(klass, id, allow_remote: false)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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