Class: LicenseTemplate
- Inherits:
-
Object
- Object
- LicenseTemplate
- 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
- YEAR_TEMPLATE_REGEX =
/[<{\[](year|yyyy)[>}\]]/i
- FULLNAME_TEMPLATE_REGEX =
%r{[\<\{\[] (fullname|name\sof\s(author|copyright\sowner)) [\>\}\]]}xi
Instance Attribute Summary collapse
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#nickname ⇒ Object
readonly
Returns the value of attribute nickname.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#content ⇒ Object
Returns the text of the license.
-
#initialize(key:, name:, project:, category:, content:, nickname: nil, url: nil, meta: {}) ⇒ LicenseTemplate
constructor
A new instance of LicenseTemplate.
- #popular? ⇒ Boolean (also: #featured?)
- #project_id ⇒ Object
-
#resolve!(project_name: nil, fullname: nil, year: Time.current.year.to_s) ⇒ Object
Populate placeholders in the LicenseTemplate content.
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 = end |
Instance Attribute Details
#category ⇒ Object (readonly)
Returns the value of attribute category.
15 16 17 |
# File 'app/models/license_template.rb', line 15 def category @category end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
15 16 17 |
# File 'app/models/license_template.rb', line 15 def key @key end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
15 16 17 |
# File 'app/models/license_template.rb', line 15 def @meta end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'app/models/license_template.rb', line 15 def name @name end |
#nickname ⇒ Object (readonly)
Returns the value of attribute nickname.
15 16 17 |
# File 'app/models/license_template.rb', line 15 def nickname @nickname end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
15 16 17 |
# File 'app/models/license_template.rb', line 15 def project @project end |
#url ⇒ Object (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
#content ⇒ Object
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?
32 33 34 |
# File 'app/models/license_template.rb', line 32 def popular? category == :Popular end |
#project_id ⇒ Object
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 |