Class: LicenseTemplate

Inherits:
Object
  • Object
show all
Defined in:
app/models/license_template.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, name:, project:, category:, content:, nickname: nil, url: nil, meta: {}) ⇒ LicenseTemplate

Returns a new instance of LicenseTemplate.


17
18
19
20
21
22
23
24
25
26
# File 'app/models/license_template.rb', line 17

def initialize(key:, name:, project:, category:, content:, nickname: nil, url: nil, meta: {})
  @key = key
  @name = name
  @project = project
  @category = category
  @content = content
  @nickname = nickname
  @url = url
  @meta = meta
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.


15
16
17
# File 'app/models/license_template.rb', line 15

def category
  @category
end

#keyObject (readonly)

Returns the value of attribute key.


15
16
17
# File 'app/models/license_template.rb', line 15

def key
  @key
end

#metaObject (readonly)

Returns the value of attribute meta.


15
16
17
# File 'app/models/license_template.rb', line 15

def meta
  @meta
end

#nameObject (readonly)

Returns the value of attribute name.


15
16
17
# File 'app/models/license_template.rb', line 15

def name
  @name
end

#nicknameObject (readonly)

Returns the value of attribute nickname.


15
16
17
# File 'app/models/license_template.rb', line 15

def nickname
  @nickname
end

#projectObject (readonly)

Returns the value of attribute project.


15
16
17
# File 'app/models/license_template.rb', line 15

def project
  @project
end

#urlObject (readonly)

Returns the value of attribute url.


15
16
17
# File 'app/models/license_template.rb', line 15

def url
  @url
end

Instance Method Details

#contentObject

Returns the text of the license


38
39
40
41
42
43
44
# File 'app/models/license_template.rb', line 38

def content
  if @content.respond_to?(:call)
    @content = @content.call
  else
    @content
  end
end

#popular?Boolean Also known as: featured?

Returns:

  • (Boolean)

32
33
34
# File 'app/models/license_template.rb', line 32

def popular?
  category == :Popular
end

#project_idObject


28
29
30
# File 'app/models/license_template.rb', line 28

def project_id
  project&.id
end

#resolve!(project_name: nil, fullname: nil, year: Time.current.year.to_s) ⇒ Object

Populate placeholders in the LicenseTemplate content


47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/license_template.rb', line 47

def resolve!(project_name: nil, fullname: nil, year: Time.current.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