Class: Hiptest::HandlebarsHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/hiptest-publisher/handlebars_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handlebars, context) ⇒ HandlebarsHelper

Returns a new instance of HandlebarsHelper.



9
10
11
12
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 9

def initialize(handlebars, context)
  @handlebars = handlebars
  @context = context
end

Class Method Details

.register_helpers(handlebars, context) ⇒ Object



3
4
5
6
7
# 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

Instance Method Details

#as_hh_join(context, items, joiner, name, block, else_block = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



57
58
59
# 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) ⇒ Object



277
278
279
280
281
282
283
284
# 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) ⇒ Object



117
118
119
120
121
# 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) ⇒ Object



232
233
234
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 232

def hh_close_curly (context, block, else_block = nil)
  "}"
end

#hh_comment(context, commenter, block) ⇒ Object



209
210
211
212
213
214
# 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) ⇒ Object



224
225
226
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 224

def hh_curly (context, block)
  "{#{block.fn(context)}}"
end

#hh_debug(context, block, _) ⇒ Object



271
272
273
274
275
# 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) ⇒ Object



216
217
218
219
220
221
222
# 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) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# 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) ⇒ Object



160
161
162
163
# 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) ⇒ Object



188
189
190
191
# 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) ⇒ Object

kept for backward compatibility of customized templates



156
157
158
# 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) ⇒ Object



165
166
167
168
169
# 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) ⇒ Object



132
133
134
# 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) ⇒ Object



304
305
306
307
308
309
310
311
312
# 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) ⇒ Object



110
111
112
113
114
115
# 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) ⇒ Object



123
124
125
126
127
128
129
130
# 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) ⇒ Object



66
67
68
# 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) ⇒ Object



90
91
92
93
94
# 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(/\|/, "\\|")}

  hh_join(context, items, ' | ', block, else_block)
end

#hh_last(context, list, block) ⇒ Object



136
137
138
# 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) ⇒ Object



228
229
230
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 228

def hh_open_curly (context, block, else_block = nil)
  "{"
end

#hh_prepend(context, str, block) ⇒ Object



102
103
104
105
106
107
108
# 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) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
# 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) ⇒ Object



145
146
147
148
# 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) ⇒ Object



258
259
260
261
# 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) ⇒ Object

kept for backward compatibility of customized templates



141
142
143
# 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) ⇒ Object



150
151
152
153
# 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) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
# 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) ⇒ Object



267
268
269
# 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) ⇒ Object



253
254
255
256
# 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) ⇒ Object



236
237
238
# 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) ⇒ Object



61
62
63
64
# 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) ⇒ Object



263
264
265
# 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) ⇒ Object



171
172
173
174
175
# 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) ⇒ Object



286
287
288
289
290
291
292
293
# 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) ⇒ Object



295
296
297
298
299
300
301
302
# 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) ⇒ Object



96
97
98
99
100
# 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_helpersObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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_helpersObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 205

def surrounded_with?(main, sub)
  main.start_with?(sub) && main.end_with?(sub)
end