Sha256: 79374dbe040b1fb39d8c61db1283b5ca9045892f6a358dbd7ec65616cf51640a

Contents?: true

Size: 1.25 KB

Versions: 8

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require "cff"
require "dry/monads"
require "pathname"

module Milestoner
  module Configuration
    module Transformers
      module Citations
        # Conditionally updates project URI based on citation URL.
        class URI
          include Dry::Monads[:result]

          def initialize key = :project_uri,
                         path: Pathname.pwd.join("CITATION.cff"),
                         citation: CFF::File
            @key = key
            @path = path
            @citation = citation
          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, :citation

          def process attributes
            attributes.fetch(key) { citation.open(path).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

8 entries across 8 versions & 1 rubygems

Version Path
milestoner-18.11.0 lib/milestoner/configuration/transformers/citations/uri.rb
milestoner-18.10.0 lib/milestoner/configuration/transformers/citations/uri.rb
milestoner-18.9.0 lib/milestoner/configuration/transformers/citations/uri.rb
milestoner-18.8.0 lib/milestoner/configuration/transformers/citations/uri.rb
milestoner-18.7.0 lib/milestoner/configuration/transformers/citations/uri.rb
milestoner-18.6.0 lib/milestoner/configuration/transformers/citations/uri.rb
milestoner-18.5.0 lib/milestoner/configuration/transformers/citations/uri.rb
milestoner-18.4.0 lib/milestoner/configuration/transformers/citations/uri.rb