class Hiptest::HandlebarsHelper
Public Class Methods
new(handlebars, context)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 9 def initialize(handlebars, context) @handlebars = handlebars @context = context end
register_helpers(handlebars, context)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 3 def self.register_helpers(handlebars, context) instance = Hiptest::HandlebarsHelper.new(handlebars, context) instance.register_string_helpers instance.register_custom_helpers end
Public Instance Methods
as_hh_join(context, items, joiner, name, block, else_block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 70 def as_hh_join(context, items, joiner, name, block, else_block = nil) joiner = joiner.to_s joiner.gsub!(/\\t/, "\t") joiner.gsub!(/\\n/, "\n") if block.nil? || block.items.empty? "#{items.join(joiner)}" else if items.empty? && else_block return else_block.fn(context) end items.map do |item| context.with_temporary_context(name => item) do block.fn(context) end end.join(joiner) end end
compute_block_value(context, value, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 57 def compute_block_value(context, value, block) value.is_a?(Handlebars::Tree::Block) ? value.fn(context) : value end
hh_case(context, expression, block_whens, block_else = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 277 def hh_case(context, expression, block_whens, block_else = nil) result = nil context.with_temporary_context(__case_expression: expression, __case_result: nil) do block_whens.fn(context) context.get("__case_result") || (block_else && block_else.fn(context).chomp) || '' end end
hh_clear_empty_lines(context, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 117 def hh_clear_empty_lines(context, block) block.fn(context).split("\n").map do |line| line unless line.strip.empty? end.compact.join("\n") end
hh_close_curly(context, block, else_block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 232 def hh_close_curly (context, block, else_block = nil) "}" end
hh_comment(context, commenter, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 209 def hh_comment (context, commenter, block) block.fn(context).split("\n").map do |line| stripped_line = line.strip stripped_line.empty? ? commenter : "#{commenter} #{line}" end.join("\n") end
hh_curly(context, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 224 def hh_curly (context, block) "{#{block.fn(context)}}" end
hh_debug(context, block, _)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 271 def hh_debug(context, block, _) require 'pry' binding.pry "" end
hh_description_with_annotations(context, commenter, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 216 def hh_description_with_annotations (context, commenter, block) value = compute_block_value(context, commenter, block) value = value.split("\n").map do |line| line.strip.downcase.start_with?('given', 'when', 'then', 'and', 'but', '*', '#') ? "\"#{line}\"" : line end.join("\n") value end
hh_escape_backslashes_and_double_quotes(context, s, block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 177 def hh_escape_backslashes_and_double_quotes (context, s, block = nil) s = compute_block_value(context, s, block) if s s.gsub('\\') { |c| c*2 }. gsub('"', '\\"') else "" end end
hh_escape_double_quotes(context, s, block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 160 def hh_escape_double_quotes (context, s, block = nil) s = compute_block_value(context, s, block) s ? s.gsub('"', '\\"') : "" end
hh_escape_new_line(context, s, block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 188 def hh_escape_new_line(context, s, block = nil) s = compute_block_value(context, s, block) s ? s.gsub("\n", '\\n') : "" end
hh_escape_quotes(context, s, block = nil)
click to toggle source
kept for backward compatibility of customized templates
# File lib/hiptest-publisher/handlebars_helper.rb, line 156 def hh_escape_quotes (context, s, block = nil) hh_escape_double_quotes(context, s, block) end
hh_escape_single_quotes(context, s, block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 165 def hh_escape_single_quotes (context, s, block = nil) # weird \\\\, see http://stackoverflow.com/questions/7074337/why-does-stringgsub-double-content s = compute_block_value(context, s, block) s ? s.gsub('\'', "\\\\'") : "" end
hh_first(context, list, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 132 def hh_first(context, list, block) hh_index(context, list, 0, block) end
hh_if_includes(context, expression, element, block_true, block_false = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 304 def hh_if_includes(context, expression, element, block_true, block_false = nil) if expression.respond_to?(:include?) && expression.include?(element) block_true.fn(context) elsif block_false block_false.fn(context) else '' end end
hh_indent(context, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 110 def hh_indent(context, block) indentation = @context[:indentation] || ' ' indentation = "\t" if indentation == '\t' hh_prepend(context, indentation, block) end
hh_index(context, list, index, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 123 def hh_index(context, list, index, block) current_this = context.get('this') context.add_item(:this, list[index.to_i]) rendered = block.fn(context) context.add_item(:this, current_this) return rendered end
hh_join(context, items, joiner, block, else_block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 66 def hh_join(context, items, joiner, block, else_block = nil) as_hh_join(context, items, joiner, :this, block, else_block) end
hh_join_gherkin_dataset(context, items, block, else_block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 90 def hh_join_gherkin_dataset(context, items, block, else_block = nil) items.map! { |item| item.gsub(/\|/, "\\|").gsub("\n", "\\n") } hh_join(context, items, ' | ', block, else_block) end
hh_last(context, list, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 136 def hh_last(context, list, block) hh_index(context, list, list.size - 1, block) end
hh_open_curly(context, block, else_block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 228 def hh_open_curly (context, block, else_block = nil) "{" end
hh_prepend(context, str, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 102 def hh_prepend(context, str, block) block.fn(context).split("\n").map do |line| indented = "#{str}#{line}" indented = "" if indented.strip.empty? indented end.join("\n") end
hh_relative_path(context, filename, path_prefix = nil, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 240 def hh_relative_path(context, filename, path_prefix = nil, block) levels_count = context.get('context.relative_path').count('/') name = "" name << path_prefix if path_prefix if levels_count == 0 name << filename else name << "../" * levels_count name << filename.to_s.gsub(/\A\.\//, '') end name end
hh_remove_double_quotes(context, s, block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 145 def hh_remove_double_quotes (context, s, block = nil) s = compute_block_value(context,s, block) s ? s.gsub('"', '') : "" end
hh_remove_last_character(context, character = '', block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 258 def hh_remove_last_character(context, character = '', block) txt = block.fn(context) return txt[-1] == character ? txt[0..-2] : txt end
hh_remove_quotes(context, s, block = nil)
click to toggle source
kept for backward compatibility of customized templates
# File lib/hiptest-publisher/handlebars_helper.rb, line 141 def hh_remove_quotes (context, s, block = nil) hh_remove_double_quotes(context, s, block) end
hh_remove_single_quotes(context, s, block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 150 def hh_remove_single_quotes (context, s, block = nil) s = compute_block_value(context,s, block) s ? s.gsub('\'', '') : "" end
hh_remove_surrounding_quotes(context, s, block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 193 def hh_remove_surrounding_quotes(context, s, block = nil) s = compute_block_value(context, s, block) if s.nil? "" elsif surrounded_with?(s, "'") || surrounded_with?(s, '"') s.slice(1...-1) else s end end
hh_replace(context, str, replacement, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 267 def hh_replace(context, str, replacement, block) return block.fn(context).gsub(str, replacement) end
hh_strip_regexp_delimiters(context, regexp, block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 253 def hh_strip_regexp_delimiters(context, regexp, block = nil) regexp = compute_block_value(context, regexp, block) return regexp.gsub(/(^\^)|(\$$)/, '') end
hh_tab(context, block, else_block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 236 def hh_tab (context, block, else_block = nil) "\t" end
hh_to_string(context, value, block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 61 def hh_to_string(context, value, block = nil) value = compute_block_value(context, value, block) "#{value.to_s}" end
hh_trim_surrounding_characters(context, character, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 263 def hh_trim_surrounding_characters(context, character, block) return block.fn(context).gsub(/(\A(#{character})*)|((#{character})*\z)/, '') end
hh_unescape_single_quotes(context, s, block = nil)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 171 def hh_unescape_single_quotes (context, s, block = nil) # weird \\\\, see http://stackoverflow.com/questions/7074337/why-does-stringgsub-double-content s = compute_block_value(context, s, block) s ? s.gsub("\\'", "'") : "" end
hh_when(context, value, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 286 def hh_when(context, value, block) return if context.get("__case_result") expression = context.get("__case_expression") if expression == value context.add_item(:__case_result, block.fn(context)) end end
hh_when_includes(context, value, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 295 def hh_when_includes(context, value, block) return if context.get("__case_result") expression = context.get("__case_expression") if expression.respond_to?(:include?) && expression.include?(value) context.add_item(:__case_result, block.fn(context)) end end
hh_with(context, var, name, block)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 96 def hh_with(context, var, name, block) context.with_temporary_context(name => var) do block.fn(context) end end
register_custom_helpers()
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 43 def register_custom_helpers self.class.instance_methods.each do |method_name| if method_name.to_s.start_with? 'hh_' @handlebars.register_helper(method_name.to_s.gsub(/hh_/, '')) do |*args| send(method_name, *args) end elsif method_name.to_s.start_with? 'as_hh_' @handlebars.register_as_helper(method_name.to_s.gsub(/as_hh_/, '')) do |*args| send(method_name, *args) end end end end
register_string_helpers()
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 14 def register_string_helpers string_helpers = [ :literate, :normalize, :normalize_lower, :normalize_with_dashes, :normalize_with_spaces, :underscore, :capitalize, :camelize, :camelize_lower, :camelize_upper, :clear_extension, :downcase, :strip ] string_helpers.each do |helper| @handlebars.register_helper(helper) do |context, block| if block.is_a? Handlebars::Tree::Block value = block.fn(context) else value = block end "#{value.to_s.send(helper)}" end end end
surrounded_with?(main, sub)
click to toggle source
# File lib/hiptest-publisher/handlebars_helper.rb, line 205 def surrounded_with?(main, sub) main.start_with?(sub) && main.end_with?(sub) end