Class: Hiptest::Renderer

Inherits:
Nodes::Walker show all
Includes:
RenderContextMaker
Defined in:
lib/hiptest-publisher/renderer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RenderContextMaker

#walk_call, #walk_dataset, #walk_ifthen, #walk_item, #walk_parameter, #walk_scenario, #walk_scenarios, #walk_tag, #walk_template, #walk_test, #walk_tests

Methods inherited from Nodes::Walker

#walk_node

Constructor Details

#initialize(context) ⇒ Renderer

Returns a new instance of Renderer.



19
20
21
22
23
24
25
26
27
# File 'lib/hiptest-publisher/renderer.rb', line 19

def initialize(context)
  super(:children_first)
  @rendered = {}
  @context = context
  @handlebars = Handlebars::Context.new
  register_partials()

  Hiptest::HandlebarsHelper.register_helpers(@handlebars, @context)
end

Instance Attribute Details

#renderedObject (readonly)

Returns the value of attribute rendered.



8
9
10
# File 'lib/hiptest-publisher/renderer.rb', line 8

def rendered
  @rendered
end

Class Method Details

.render(node, language, context) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/hiptest-publisher/renderer.rb', line 11

def self.render(node, language, context)
  context[:language] = language

  renderer = Hiptest::Renderer.new(context)
  renderer.walk_node(node)
  renderer.rendered[node]
end

Instance Method Details

#call_node_walker(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hiptest-publisher/renderer.rb', line 29

def call_node_walker(node)
  if node.is_a? Hiptest::Nodes::Node
    @rendered_children = {}
    node.children.each {|name, child| @rendered_children[name] = @rendered[child]}
    @rendered[node] = render_node(node, super(node))
  elsif node.is_a? Array
    @rendered[node] = node.map {|item| @rendered[item]}
  else
    @rendered[node] = node
  end
end

#get_template_by_name(name, extension) ⇒ Object



71
72
73
74
75
76
# File 'lib/hiptest-publisher/renderer.rb', line 71

def get_template_by_name(name, extension)
  searched_folders.map do |path|
    template_path = File.join(path, "#{name}.#{extension}")
    template_path if File.file?(template_path)
  end.compact.first
end

#get_template_path(node, extension = 'hbs') ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/hiptest-publisher/renderer.rb', line 78

def get_template_path(node, extension = 'hbs')
  normalized_name = node.class.name.split('::').last.downcase
  unless @context[:forced_templates][normalized_name].nil?
    normalized_name = @context[:forced_templates][normalized_name]
  end

  get_template_by_name(normalized_name, extension)
end

#register_partialsObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/hiptest-publisher/renderer.rb', line 50

def register_partials()
  searched_folders.reverse.each do |path|
    next unless File.directory?(path)
    Dir.entries(path).select do |file_name|
      file_path = File.join(path, file_name)
      next unless File.file?(file_path) && file_name.start_with?('_')
      @handlebars.register_partial(file_name[1..-5], File.read(file_path))
    end
  end
end

#render_node(node, render_context) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/hiptest-publisher/renderer.rb', line 61

def render_node(node, render_context)
  render_context = {} if render_context.nil?
  render_context[:node] = node
  render_context[:rendered_children] = @rendered_children
  render_context[:context] = @context

  template = get_template_path(node)
  @handlebars.compile(File.read(template)).send(:call, render_context)
end

#searched_foldersObject



41
42
43
44
45
46
47
48
# File 'lib/hiptest-publisher/renderer.rb', line 41

def searched_folders()
  folders = []
  if @context.has_key?(:framework)
    folders << "#{@context[:language]}/#{@context[:framework]}"
  end
  folders << [@context[:language], 'common']
  folders.flatten.map {|path| "#{hiptest_publisher_path}/lib/templates/#{path}"}
end