Class: Handlebars::Helpers::StringFormatting::Surround

Inherits:
BaseSafeStringHelper show all
Defined in:
lib/handlebars/helpers/string_formatting/surround.rb

Overview

Surround will surround a value with prefix and suffix, an empty value is considered valid data

Instance Method Summary collapse

Methods inherited from BaseSafeStringHelper

#wrapper

Methods inherited from BaseHelper

#parse_json, #struct_to_hash, #tokenizer, #wrapper

Instance Method Details

#handlebars_helperObject



39
40
41
42
43
44
45
# File 'lib/handlebars/helpers/string_formatting/surround.rb', line 39

def handlebars_helper
  proc do |_context, value, prefix, suffix, formats|
    # Handle optional: formats
    formats = nil if formats.is_a?(V8::Object)
    wrapper(parse(value, prefix, suffix, formats))
  end
end

#parse(value, prefix, suffix, formats) ⇒ String

Parse will surround a value with prefix and suffix, an empty value is considered valid data

Examples:


puts Surround.new.parse('product category', '"', '"', 'titleize')

"Product Category"

puts Surround.new.parse(nil, '"', '"', 'titleize')

""

Parameters:

  • value (String)
    • value to surround

  • prefix (String)
    • prefix to insert in front of value

  • suffix (String)
    • suffix to append to value

  • formats (String)
    • list of formats to apply to value, defaults to none

Returns:

  • (String)

    prefix + value + suffix



34
35
36
37
# File 'lib/handlebars/helpers/string_formatting/surround.rb', line 34

def parse(value, prefix, suffix, formats)
  format_as = Handlebars::Helpers::StringFormatting::FormatAs.new
  "#{prefix}#{format_as.parse(value, formats)}#{suffix}"
end