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

#hh_clear_empty_lines(context, block) ⇒ Object



79
80
81
82
83
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 79

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



126
127
128
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 126

def hh_close_curly (context, block)
  "}"
end

#hh_comment(context, commenter, block) ⇒ Object



112
113
114
115
116
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 112

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



118
119
120
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 118

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

#hh_debug(context, block) ⇒ Object



134
135
136
137
138
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 134

def hh_debug(context, block)
  require 'pry'
  binding.pry
  ""
end

#hh_escape_double_quotes(context, s, block) ⇒ Object



103
104
105
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 103

def hh_escape_double_quotes (context, s, block)
  s ? s.gsub('"', '\\"') : ""
end

#hh_escape_quotes(context, s, block) ⇒ Object

kept for backward compatibility of customized templates



99
100
101
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 99

def hh_escape_quotes (context, s, block)
  hh_escape_double_quotes(context, s, block)
end

#hh_escape_single_quotes(context, s, block) ⇒ Object



107
108
109
110
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 107

def hh_escape_single_quotes (context, s, block)
  # weird \\\\, see http://stackoverflow.com/questions/7074337/why-does-stringgsub-double-content
  s ? s.gsub('\'', "\\\\'") : ""
end

#hh_indent(context, block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 68

def hh_indent(context, block)
  indentation = @context[:indentation] || '  '
  indentation = "\t" if indentation == '\t'

  block.fn(context).split("\n").map do |line|
    indented = "#{indentation}#{line}"
    indented = "" if indented.strip.empty?
    indented
  end.join("\n")
end

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 47

def hh_join(context, items, joiner, block, else_block = nil)
  joiner = "\t" if joiner == '\t'

  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_open_curly(context, block) ⇒ Object



122
123
124
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 122

def hh_open_curly (context, block)
  "{"
end

#hh_remove_double_quotes(context, s, block) ⇒ Object



90
91
92
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 90

def hh_remove_double_quotes (context, s, block)
  s ? s.gsub('"', '') : ""
end

#hh_remove_quotes(context, s, block) ⇒ Object

kept for backward compatibility of customized templates



86
87
88
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 86

def hh_remove_quotes (context, s, block)
  hh_remove_double_quotes(context, s, block)
end

#hh_remove_single_quotes(context, s, block) ⇒ Object



94
95
96
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 94

def hh_remove_single_quotes (context, s, block)
  s ? s.gsub('\'', '') : ""
end

#hh_tab(context, block) ⇒ Object



130
131
132
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 130

def hh_tab (context, block)
  "\t"
end

#hh_to_string(context, value, block) ⇒ Object



43
44
45
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 43

def hh_to_string(context, value, block)
  "#{value.to_s}"
end

#register_custom_helpersObject



34
35
36
37
38
39
40
41
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 34

def register_custom_helpers
  self.class.instance_methods.each do |method_name|
    next unless method_name.to_s.start_with? 'hh_'
    @handlebars.register_helper(method_name.to_s.gsub(/hh_/, '')) do |*args|
      send(method_name, *args)
    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
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 14

def register_string_helpers
  string_helpers = [
    :literate,
    :normalize,
    :normalize_lower,
    :underscore,
    :camelize,
    :camelize_lower,
    :camelize_upper,
    :clear_extension,
    :downcase
  ]

  string_helpers.each do |helper|
    @handlebars.register_helper(helper) do |context, value|
      "#{value.to_s.send(helper)}"
    end
  end
end