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



61
62
63
64
65
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 61

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



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

def hh_close_curly (context, block)
  "}"
end

#hh_comment(context, commenter, block) ⇒ Object



75
76
77
78
79
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 75

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



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

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

#hh_escape_quotes(context, s, block) ⇒ Object



71
72
73
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 71

def hh_escape_quotes (context, s, block)
  "#{s.gsub(/"/) {|_| '\\"' }}"
end

#hh_indent(context, block) ⇒ Object



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

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) ⇒ Object



45
46
47
48
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 45

def hh_join(context, items, joiner, block)
  joiner = "\t" if joiner == '\t'
  "#{items.join(joiner)}"
end

#hh_open_curly(context, block) ⇒ Object



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

def hh_open_curly (context, block)
  "{"
end

#hh_remove_quotes(context, s, block) ⇒ Object



67
68
69
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 67

def hh_remove_quotes (context, s, block)
  "#{s.gsub('"', '')}"
end

#hh_tab(context, block) ⇒ Object



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

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

#hh_to_string(context, value, block) ⇒ Object



41
42
43
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 41

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

#register_custom_helpersObject



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

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
# File 'lib/hiptest-publisher/handlebars_helper.rb', line 14

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

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