Module: LogMagic::TemplatingUtils

Instance Method Summary collapse

Instance Method Details

#enricheObject



19
20
21
# File 'lib/log_magic/utils/templating_utils.rb', line 19

def enriche
  @object_to_enriche.gsub!(match_regex, rendered_template)
end

#initialize(object_to_enriche) ⇒ Object



2
3
4
# File 'lib/log_magic/utils/templating_utils.rb', line 2

def initialize(object_to_enriche)
  @object_to_enriche = object_to_enriche
end

#match_regexObject



23
24
25
# File 'lib/log_magic/utils/templating_utils.rb', line 23

def match_regex
  /(?:[^\w]|\A)#{term_name}[^\w]/
end

#rendered_templateObject



6
7
8
9
# File 'lib/log_magic/utils/templating_utils.rb', line 6

def rendered_template
  engine = Haml::Engine.new(File.read(template_path))
  engine.render(self)
end

#template_dirObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/log_magic/utils/templating_utils.rb', line 31

def template_dir
  if self.class.name =~ /\w*::\w*::\w*/
    if self.class.name =~ /LogMagic::SearchkickExplainer/
      'explainer_templates/searchkick'
    elsif self.class.name =~ /LogMagic::MySqlExplainer/
      'explainer_templates/mysql'
    end
  else
    'explainer_templates'
  end
end

#template_nameObject



27
28
29
# File 'lib/log_magic/utils/templating_utils.rb', line 27

def template_name
  "#{term_name}.haml"
end

#template_pathObject



11
12
13
14
15
16
17
# File 'lib/log_magic/utils/templating_utils.rb', line 11

def template_path
  file_name = File.expand_path(
    file_name = File.join(__FILE__, '..', '..', template_dir, template_name)
  )
  fail("No template found for #{self.class.name}") unless File.exist?(file_name)
  return file_name
end