Sha256: e54f2189b0d852bb6fae1a9b35517047933c22a3a6adc30d58208d3fb5c19355

Contents?: true

Size: 1.38 KB

Versions: 57

Compression:

Stored size: 1.38 KB

Contents

module Extension
  module Forms
    module Questions
      class AskType
        include ShopifyCLI::MethodObject

        property! :ctx
        property :type
        property :prompt,
          converts: :to_proc,
          default: -> { CLI::UI::Prompt.method(:ask) }

        def call(project_details)
          specifications = Models::Specifications.new(
            fetch_specifications: Tasks::FetchSpecifications.new(context: ctx, api_key: project_details.app.api_key)
          )

          project_details.tap do |p|
            p.type = type.nil? ? choose_type(specifications) : validate_given_type(specifications, type)
          end
        end

        private

        def validate_given_type(specifications, type)
          return specifications[type] if specifications.valid?(type)
          ctx.abort(ctx.message("create.invalid_type")) unless type.nil?
        end

        def choose_type(specifications)
          abort_due_to_missing_specifications if specifications.none?

          prompt.call(ctx.message("create.ask_type")) do |handler|
            specifications.each do |type|
              handler.option("#{type.name} #{type.tagline}") { type }
            end
          end
        end

        def abort_due_to_missing_specifications
          ctx.puts(@ctx.message("create.no_available_extensions"))
          raise ShopifyCLI::AbortSilent
        end
      end
    end
  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
shopify-cli-2.36.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.35.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.34.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.33.1 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.33.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.32.1 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.32.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.31.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.30.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.29.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.28.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.27.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.26.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.25.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.24.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.23.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.22.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.21.0 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.20.1 lib/project_types/extension/forms/questions/ask_type.rb
shopify-cli-2.20.0 lib/project_types/extension/forms/questions/ask_type.rb