Sha256: af30c931cdc10502c927bde09cd68498aaf79ee73973cbf0d93b2471936eebda

Contents?: true

Size: 1.81 KB

Versions: 15

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

##
# Usage example:
#
# result = ConvenientService::Examples::Dry::Gemfile::Services::FormatHeader.result(
#   parsed_content: {
#     ruby: [
#       %(ruby "3.0.1")
#     ],
#     source: [
#       %(source "https://rubygems.org")
#     ],
#     git_source: [
#       %(git_source(:github) { |repo| "https://github.com/\#{repo}.git" })
#     ]
#   },
#   skip_frozen_string_literal: true
# )
#
# NOTE: Check the corresponding spec file to see more examples.
#
module ConvenientService
  module Examples
    module Dry
      module Gemfile
        module Services
          class FormatHeader
            FROZEN_STRING_LITERAL = "# frozen_string_literal: true"
            EMPTY_LINE = ""
            ENTER = "\n"

            include DryService::Config

            option :parsed_content
            option :skip_frozen_string_literal, default: -> { false }

            contract do
              schema do
                required(:parsed_content).hash do
                  optional(:ruby).array(:string)
                  optional(:source).array(:string)
                  optional(:git_source).array(:string)
                end

                optional(:skip_frozen_string_literal).value(:bool?)
              end
            end

            def result
              success(formatted_content: format_content)
            end

            private

            def format_content
              content = EMPTY_LINE.dup

              content << FROZEN_STRING_LITERAL << ENTER << ENTER unless skip_frozen_string_literal

              parsed_content.slice(:source, :git_source, :ruby).each do |_group, lines|
                content << lines.join(ENTER) << ENTER << ENTER
              end

              content.chomp!

              content
            end
          end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
convenient_service-0.12.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.11.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.10.1 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.10.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.9.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.8.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.7.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.6.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.5.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.4.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.3.1 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.3.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.2.1 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.2.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb
convenient_service-0.1.0 lib/convenient_service/examples/dry/gemfile/services/format_header.rb