Class: Eql::TemplateLoader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/eql/template_loader.rb

Overview

Loader class loads templates and caches them.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder) ⇒ TemplateLoader

Returns a new instance of TemplateLoader.

Parameters:



16
17
18
# File 'lib/eql/template_loader.rb', line 16

def initialize(builder)
  @builder = builder
end

Instance Attribute Details

#builderEql::Builder (readonly)

Returns:



11
12
13
# File 'lib/eql/template_loader.rb', line 11

def builder
  @builder
end

Class Method Details

.cacheHash{String => String}

Templates cache

Returns:

  • (Hash{String => String})


88
89
90
# File 'lib/eql/template_loader.rb', line 88

def self.cache
  @cache ||= {}
end

Instance Method Details

#load_file(path) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load file’s content

Parameters:

  • path (String)

    file’s path

Returns:

  • (String)


42
43
44
# File 'lib/eql/template_loader.rb', line 42

def load_file(path)
  File.read(path)
end

#load_template(name) ⇒ String

Load builder template

Parameters:

  • name (String, Symbol)

    template’s name

Returns:

  • (String)

    returns loaded template



27
28
29
30
31
# File 'lib/eql/template_loader.rb', line 27

def load_template(name)
  path = resolve_path(name)
  return load_file(path) unless Eql.config.cache_templates?
  cache[path] ||= load_file(path)
end

#resolve_path(file) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolve file’s path

Parameters:

  • file (String)

    template’s name

Returns:

  • (String)

    returns template’s path

Raises:

  • (RuntimeError)

    when can’t wind a file



72
73
74
75
76
77
78
79
80
81
# File 'lib/eql/template_loader.rb', line 72

def resolve_path(file)
  paths = template_path(file)

  paths.each do |path|
    filepath = Dir.glob(path).first
    return filepath if filepath.present?
  end

  raise "Unable to find query template with #{paths.inspect} location"
end

#template_path(file) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

File template to find

Parameters:

  • file (String, Symbol)

    template’s name

Returns:

  • (Array<String>)

    returns file path pattern



55
56
57
58
59
# File 'lib/eql/template_loader.rb', line 55

def template_path(file)
  @builder.path.map do |path|
    [File.join(path, file.to_s), @builder.adapter.extension].join
  end
end