Class: Handlebars::Helpers::StringFormatting::Padl

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

Overview

Add padding to the left of the value.

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



46
47
48
49
50
51
52
53
54
# File 'lib/handlebars/helpers/string_formatting/padl.rb', line 46

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

#parse(value, count, char) ⇒ String

Parse will Add padding to the left of the value.

Examples:


puts "[#{Padl.new.parse('aaa', 10)}]"

[       aaa]

puts "[#{Padl.new.parse('aaa')}]"

[                           aaa]

puts Padl.new.parse('aaa', '10', '-')

-------aaa

Parameters:

  • value (String)
    • value to apply padding to

  • count (Integer)
    • how much padding to apply. defaults to configuration.padl_count

  • char (String)
    • character to pad with. defaults to configuration.padl_char

Returns:

  • (String)

    value with padding to left



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

def parse(value, count, char)
  value = '' if value.nil?
  count = Handlebars::Helpers.configuration.padl_count if count.nil?
  count = count.to_i if count.is_a?(String)
  char = Handlebars::Helpers.configuration.padl_char if char.nil?
  value.to_s.rjust(count, char)
end