Class: Handlebars::Helpers::StringFormatting::FormatAs

Inherits:
BaseHelper
  • Object
show all
Defined in:
lib/handlebars/helpers/string_formatting/format_as.rb

Overview

Format As: Chain a list of string formatters to run sequentially

Instance Method Summary collapse

Methods inherited from BaseHelper

#parse_json, #struct_to_hash, #tokenizer, #wrapper

Instance Method Details

#handlebars_helperObject



37
38
39
# File 'lib/handlebars/helpers/string_formatting/format_as.rb', line 37

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

#parse(value, formats) ⇒ String

Parse will execute a chain of string formatters and run them sequentially

Examples:


puts FormatAs.new.parse('the quick brown fox', 'pluralize,dashify')

the-quick-brown-foxes

Parameters:

  • value (String)
    • value to format

  • formats (String)
    • comma delimited list of formats

Returns:

  • (String)

    returns a value that has been processed by multiple formatters



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/handlebars/helpers/string_formatting/format_as.rb', line 25

def parse(value, formats)
  return '' if value.nil?
  return value if formats.nil? || formats.empty?

  formats = formats.split(',') if formats.is_a?(String)
  formats = formats.map(&:to_sym)
  formats.each do |format|
    value = format_value(value, format)
  end
  value
end