Class: TemplateFinder

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/finders/template_finder.rb

Constant Summary collapse

VENDORED_TEMPLATES =
HashWithIndifferentAccess.new(
  dockerfiles: ::Gitlab::Template::DockerfileTemplate,
  gitignores: ::Gitlab::Template::GitignoreTemplate,
  gitlab_ci_ymls: ::Gitlab::Template::GitlabCiYmlTemplate,
  issues: ::Gitlab::Template::IssueTemplate,
  merge_requests: ::Gitlab::Template::MergeRequestTemplate
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, project, params = {}) ⇒ TemplateFinder

Returns a new instance of TemplateFinder.



41
42
43
44
45
46
47
# File 'app/finders/template_finder.rb', line 41

def initialize(type, project, params = {})
  @type = type
  @project = project
  @params = params

  @vendored_templates = VENDORED_TEMPLATES.fetch(type)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



36
37
38
# File 'app/finders/template_finder.rb', line 36

def params
  @params
end

#projectObject (readonly)

Returns the value of attribute project.



36
37
38
# File 'app/finders/template_finder.rb', line 36

def project
  @project
end

#typeObject (readonly)

Returns the value of attribute type.



36
37
38
# File 'app/finders/template_finder.rb', line 36

def type
  @type
end

Class Method Details

.all_template_names(project, type) ⇒ Object



23
24
25
26
27
# File 'app/finders/template_finder.rb', line 23

def all_template_names(project, type)
  return {} unless type_allowed?(type)

  build(type, project).template_names
end

.build(type, project, params = {}) ⇒ Object



15
16
17
18
19
20
21
# File 'app/finders/template_finder.rb', line 15

def build(type, project, params = {})
  if type.to_s == 'licenses'
    LicenseTemplateFinder.new(project, params) # rubocop: disable CodeReuse/Finder
  elsif type_allowed?(type)
    new(type, project, params)
  end
end

.type_allowed?(type) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'app/finders/template_finder.rb', line 29

def type_allowed?(type)
  return true if type.to_s == 'licenses'

  VENDORED_TEMPLATES.key?(type)
end

Instance Method Details

#executeObject



49
50
51
52
53
54
55
# File 'app/finders/template_finder.rb', line 49

def execute
  if params[:name]
    vendored_templates.find(params[:name], project)
  else
    vendored_templates.all(project)
  end
end

#template_namesObject



57
58
59
# File 'app/finders/template_finder.rb', line 57

def template_names
  vendored_templates.template_names(project)
end