Class: Handlebars::Helpers::StringFormatting::SurroundIf

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

Overview

Surround If will surround a value with prefix and suffix, if value is not empty

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



33
34
35
36
37
38
39
# File 'lib/handlebars/helpers/string_formatting/surround_if.rb', line 33

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, if value is not empty

Examples:


puts SurroundIf.new.parse('product category', '["', '"]', 'titleize')

["Product Category"]

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)

    value surrounded by prefix and suffix



28
29
30
31
# File 'lib/handlebars/helpers/string_formatting/surround_if.rb', line 28

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