Sha256: a47c523802641b07bd8588d250612e12f7412a3e22a6d1463c50dcef6726db61

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require "dry/monads"
require "pathname"

module Milestoner
  module Configuration
    module Transformers
      module Gems
        # Conditionally updates project URI based on specification home page URL.
        class URI
          include Import[:spec_loader]
          include Dry::Monads[:result]

          def initialize(key = :project_uri, path: "#{Pathname.pwd.basename}.gemspec", **)
            super(**)
            @key = key
            @path = path
          end

          def call attributes
            process attributes
            Success attributes
          rescue KeyError => error
            Failure step: :transform,
                    payload: "Unable to transform #{key.inspect}, missing specifier: " \
                             "\"#{error.message[/<.+>/]}\"."
          end

          private

          attr_reader :key, :path

          def process attributes
            attributes.fetch(key) { spec_loader.call(path).homepage_url }
                      .then { |value| value.match?(/%<.+>s/) ? format(value, attributes) : value }
                      .then { |value| attributes.merge! key => value unless value.empty? }
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
milestoner-18.12.0 lib/milestoner/configuration/transformers/gems/uri.rb