debian-mirror-gitlab/app/models/license_template.rb

59 lines
1.5 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-11-20 20:47:30 +05:30
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
2021-03-11 19:13:27 +05:30
attr_reader :key, :name, :project, :category, :nickname, :url, :meta
2018-11-20 20:47:30 +05:30
2021-03-11 19:13:27 +05:30
def initialize(key:, name:, project:, category:, content:, nickname: nil, url: nil, meta: {})
2018-12-05 23:21:45 +05:30
@key = key
2018-11-20 20:47:30 +05:30
@name = name
2021-03-11 19:13:27 +05:30
@project = project
2018-11-20 20:47:30 +05:30
@category = category
@content = content
@nickname = nickname
@url = url
@meta = meta
end
2021-03-11 19:13:27 +05:30
def project_id
project&.id
end
2018-11-20 20:47:30 +05:30
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
2020-06-23 00:09:42 +05:30
def resolve!(project_name: nil, fullname: nil, year: Time.current.year.to_s)
2018-11-20 20:47:30 +05:30
# 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