Class: Hiptest::HandlebarsHelper
- Inherits:
-
Object
- Object
- Hiptest::HandlebarsHelper
- Defined in:
- lib/hiptest-publisher/handlebars_helper.rb
Class Method Summary collapse
Instance Method Summary collapse
- #compute_block_value(context, value, block) ⇒ Object
- #hh_clear_empty_lines(context, block) ⇒ Object
- #hh_close_curly(context, block) ⇒ Object
- #hh_comment(context, commenter, block) ⇒ Object
- #hh_curly(context, block) ⇒ Object
- #hh_debug(context, block) ⇒ Object
- #hh_escape_backslashes_and_double_quotes(context, s, block = nil) ⇒ Object
- #hh_escape_double_quotes(context, s, block = nil) ⇒ Object
- #hh_escape_new_line(context, s, block = nil) ⇒ Object
-
#hh_escape_quotes(context, s, block = nil) ⇒ Object
kept for backward compatibility of customized templates.
- #hh_escape_single_quotes(context, s, block = nil) ⇒ Object
- #hh_first(context, list, block) ⇒ Object
- #hh_if_includes(context, array, element, block_true, block_false = nil) ⇒ Object
- #hh_indent(context, block) ⇒ Object
- #hh_index(context, list, index, block) ⇒ Object
- #hh_join(context, items, joiner, block, else_block = nil) ⇒ Object
- #hh_join_gherkin_dataset(context, items, block, else_block = nil) ⇒ Object
- #hh_last(context, list, block) ⇒ Object
- #hh_open_curly(context, block) ⇒ Object
- #hh_prepend(context, str, block) ⇒ Object
- #hh_relative_path(context, filename, path_prefix = nil, block) ⇒ Object
- #hh_remove_double_quotes(context, s, block = nil) ⇒ Object
- #hh_remove_last_character(context, character = '', block) ⇒ Object
-
#hh_remove_quotes(context, s, block = nil) ⇒ Object
kept for backward compatibility of customized templates.
- #hh_remove_single_quotes(context, s, block = nil) ⇒ Object
- #hh_remove_surrounding_quotes(context, s, block = nil) ⇒ Object
- #hh_replace(context, str, replacement, block) ⇒ Object
- #hh_strip_regexp_delimiters(context, regexp, block = nil) ⇒ Object
- #hh_tab(context, block) ⇒ Object
- #hh_to_string(context, value, block = nil) ⇒ Object
- #hh_trim_surrounding_characters(context, character, block) ⇒ Object
- #hh_unescape_single_quotes(context, s, block = nil) ⇒ Object
- #hh_unless(context, condition, block, else_block = nil) ⇒ Object
- #hh_with(context, var, name, block) ⇒ Object
-
#initialize(handlebars, context) ⇒ HandlebarsHelper
constructor
A new instance of HandlebarsHelper.
- #register_custom_helpers ⇒ Object
- #register_string_helpers ⇒ Object
- #surrounded_with?(main, sub) ⇒ Boolean
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(, context) = @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(, context) instance = Hiptest::.new(, context) instance.register_string_helpers instance.register_custom_helpers end |
Instance Method Details
#compute_block_value(context, value, block) ⇒ Object
52 53 54 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 52 def compute_block_value(context, value, block) value.is_a?(::Tree::Block) ? value.fn(context) : value end |
#hh_clear_empty_lines(context, block) ⇒ Object
127 128 129 130 131 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 127 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) ⇒ Object
233 234 235 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 233 def hh_close_curly (context, block) "}" end |
#hh_comment(context, commenter, block) ⇒ Object
219 220 221 222 223 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 219 def hh_comment (context, commenter, block) block.fn(context).split("\n").map do |line| "#{commenter} #{line}" end.join("\n") end |
#hh_curly(context, block) ⇒ Object
225 226 227 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 225 def hh_curly (context, block) "{#{block.fn(context)}}" end |
#hh_debug(context, block) ⇒ Object
272 273 274 275 276 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 272 def hh_debug(context, block) require 'pry' binding.pry "" end |
#hh_escape_backslashes_and_double_quotes(context, s, block = nil) ⇒ Object
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 187 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
170 171 172 173 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 170 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
198 199 200 201 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 198 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
166 167 168 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 166 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
175 176 177 178 179 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 175 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
142 143 144 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 142 def hh_first(context, list, block) hh_index(context, list, 0, block) end |
#hh_if_includes(context, array, element, block_true, block_false = nil) ⇒ Object
278 279 280 281 282 283 284 285 286 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 278 def hh_if_includes(context, array, element, block_true, block_false = nil) if array.kind_of?(Array) && array.include?(element) block_true.fn(context) elsif block_false block_false.fn(context) else '' end end |
#hh_indent(context, block) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 120 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
133 134 135 136 137 138 139 140 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 133 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 61 def hh_join(context, items, joiner, 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 current_this = context.get('this') result = items.map do |item| context.add_item(:this, item) block.fn(context) end.join(joiner) context.add_item(:this, current_this) result end end |
#hh_join_gherkin_dataset(context, items, block, else_block = nil) ⇒ Object
85 86 87 88 89 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 85 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
146 147 148 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 146 def hh_last(context, list, block) hh_index(context, list, list.size - 1, block) end |
#hh_open_curly(context, block) ⇒ Object
229 230 231 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 229 def hh_open_curly (context, block) "{" end |
#hh_prepend(context, str, block) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 112 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
241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 241 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
155 156 157 158 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 155 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
259 260 261 262 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 259 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
151 152 153 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 151 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
160 161 162 163 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 160 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
203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 203 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
268 269 270 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 268 def hh_replace(context, str, replacement, block) return block.fn(context).gsub(str, replacement) end |
#hh_strip_regexp_delimiters(context, regexp, block = nil) ⇒ Object
254 255 256 257 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 254 def hh_strip_regexp_delimiters(context, regexp, block=nil) regexp = compute_block_value(context, regexp, block) return regexp.gsub(/(^\^)|(\$$)/, '') end |
#hh_tab(context, block) ⇒ Object
237 238 239 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 237 def hh_tab (context, block) "\t" end |
#hh_to_string(context, value, block = nil) ⇒ Object
56 57 58 59 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 56 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
264 265 266 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 264 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
181 182 183 184 185 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 181 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_unless(context, condition, block, else_block = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 100 def hh_unless(context, condition, block, else_block = nil) condition = !condition.empty? if condition.respond_to?(:empty?) if !condition block.fn(context) elsif else_block else_block.fn(context) else "" end end |
#hh_with(context, var, name, block) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 91 def hh_with(context, var, name, block) name = name.to_s current_value = context.get(name) context.add_item(name, var) result = block.fn(context) context.add_item(name, current_value) result end |
#register_custom_helpers ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 43 def register_custom_helpers self.class.instance_methods.each do |method_name| next unless method_name.to_s.start_with? 'hh_' .register_helper(method_name.to_s.gsub(/hh_/, '')) do |*args| send(method_name, *args) end end end |
#register_string_helpers ⇒ Object
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| .register_helper(helper) do |context, block| if block.is_a? ::Tree::Block value = block.fn(context) else value = block end "#{value.to_s.send(helper)}" end end end |
#surrounded_with?(main, sub) ⇒ Boolean
215 216 217 |
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 215 def surrounded_with?(main, sub) main.start_with?(sub) && main.end_with?(sub) end |