Class: Handlebars::Helpers::StringFormatting::PrependIf

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

Overview

Append If will prepend the prefix to value, 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



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

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

#parse(value, prefix, formats) ⇒ String

Parse will Append If will prepend the prefix to value, if value is not empty

Examples:


puts PrependIf.new.parse('turn to symbol', ':', 'snake')

:turn_to_symbol

Parameters:

  • value (String)
    • value to add prepend too

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

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

Returns:

  • (String)

    prefix + value when value exists, otherwise ”



27
28
29
30
# File 'lib/handlebars/helpers/string_formatting/prepend_if.rb', line 27

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