GitLab steht Mittwoch, den 08. Juli, zwischen 09:00 und 13:00 Uhr aufgrund von Wartungsarbeiten nicht zur Verfügung.

license_template.rb 1.42 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7 8 9 10 11 12 13 14
class LicenseTemplate
  PROJECT_TEMPLATE_REGEX =
    %r{[\<\{\[]
      (project|description|
      one\sline\s.+\swhat\sit\sdoes\.) # matching the start and end is enough here
    [\>\}\]]}xi.freeze
  YEAR_TEMPLATE_REGEX = /[<{\[](year|yyyy)[>}\]]/i.freeze
  FULLNAME_TEMPLATE_REGEX =
    %r{[\<\{\[]
      (fullname|name\sof\s(author|copyright\sowner))
    [\>\}\]]}xi.freeze

15
  attr_reader :key, :name, :category, :nickname, :url, :meta
16

17 18
  def initialize(key:, name:, category:, content:, nickname: nil, url: nil, meta: {})
    @key = key
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    @name = name
    @category = category
    @content = content
    @nickname = nickname
    @url = url
    @meta = meta
  end

  def popular?
    category == :Popular
  end
  alias_method :featured?, :popular?

  # Returns the text of the license
  def content
    if @content.respond_to?(:call)
      @content = @content.call
    else
      @content
    end
  end

  # Populate placeholders in the LicenseTemplate content
  def resolve!(project_name: nil, fullname: nil, year: Time.now.year.to_s)
    # Ensure the string isn't shared with any other instance of LicenseTemplate
    new_content = content.dup
    new_content.gsub!(YEAR_TEMPLATE_REGEX, year) if year.present?
    new_content.gsub!(PROJECT_TEMPLATE_REGEX, project_name) if project_name.present?
    new_content.gsub!(FULLNAME_TEMPLATE_REGEX, fullname) if fullname.present?

    @content = new_content

    self
  end
end