Class: Handlebars::Helpers::BaseHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/handlebars/helpers/base_helper.rb

Overview

base helper provides an interface to wrap your parsing logic in a Handlebars aware context

Instance Method Summary collapse

Instance Method Details

#handlebars_helperObject

Wrap the parse method in a handlebars context aware block that is used during registration



12
13
14
# File 'lib/handlebars/helpers/base_helper.rb', line 12

def handlebars_helper
  proc { |_context, value| wrapper(parse(value)) }
end

#parse(value) ⇒ Object

All child classes will generally implement this method



17
18
19
# File 'lib/handlebars/helpers/base_helper.rb', line 17

def parse(value)
  value
end

#parse_json(value, hash = {}) ⇒ Object

This needs to be in a data_helper rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Style/HashEachMethods



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/handlebars/helpers/base_helper.rb', line 37

def parse_json(value, hash = {})
  return value.map { |item| parse_json(item) } if value.is_a?(V8::Array)

  return struct_to_hash(value) if value.is_a?(OpenStruct)

  return value unless value.is_a?(V8::Object)

  value.keys.each do |key|
    hash[key] = case value[key]
                when V8::Object
                  parse_json(value[key])
                when V8::Array
                  value[key].map do |item|
                    case item
                    when V8::Object, V8::Array
                      parse_json(item)
                    when String
                      item
                    else
                      item.values
                    end
                  end
                else
                  value[key]
                end
  end

  hash
end

#struct_to_hash(data) ⇒ Object

Needs to move into a GEM and make sure I have consistency



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/handlebars/helpers/base_helper.rb', line 69

def struct_to_hash(data)
  # No test yet
  if data.is_a?(Array)
    return data.map { |v| v.is_a?(OpenStruct) ? struct_to_hash(v) : v }
  end

  data.each_pair.with_object({}) do |(key, value), hash|
    case value
    when OpenStruct
      hash[key] = struct_to_hash(value)
    when Array
      # No test yet
      values = value.map { |v| v.is_a?(OpenStruct) ? struct_to_hash(v) : v }
      hash[key] = values
    else
      hash[key] = value
    end
  end
end

#tokenizerObject

String tokenizer will clean up a string so that all sorts of case formatted strings can be represented in a consistent fashion



31
32
33
# File 'lib/handlebars/helpers/base_helper.rb', line 31

def tokenizer
  @_tokenizer ||= Handlebars::Helpers.configuration.tokenizer
end

#wrapper(value) ⇒ Object

If you need to wrap the return value in a specific Handlebars Type, eg. SafeString, then you can override this method



24
25
26
# File 'lib/handlebars/helpers/base_helper.rb', line 24

def wrapper(value)
  value
end