Sha256: 71b1bce05d8e41a4ae9f9822123f9200d81987a17901fc7bdc56574f9afb63f0

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

##
# Usage example:
#
# result = ConvenientService::Examples::Rails::V1::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 Rails
      module V1
        class Gemfile
          module Services
            class FormatHeader
              FROZEN_STRING_LITERAL = "# frozen_string_literal: true"
              EMPTY_LINE = ""
              ENTER = "\n"

              include RailsService::Config

              ##
              # NOTE: `attribute` has no `hash` type.
              # https://github.com/rails/rails/blob/v7.0.4/activemodel/lib/active_model/type.rb#L42
              #
              # TODO: Implement custom types.
              #
              attribute :parsed_content
              attribute :skip_frozen_string_literal, :boolean, default: false

              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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
convenient_service-0.17.0 lib/convenient_service/examples/rails/v1/gemfile/services/format_header.rb
convenient_service-0.16.0 lib/convenient_service/examples/rails/v1/gemfile/services/format_header.rb
convenient_service-0.15.0 lib/convenient_service/examples/rails/v1/gemfile/services/format_header.rb
convenient_service-0.14.0 lib/convenient_service/examples/rails/v1/gemfile/services/format_header.rb