Class: Handlebars::Helpers::StringFormatting::Padr

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

Overview

Add padding to the right 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



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

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 = nil, char = nil) ⇒ String

Parse will Add padding to the right of the value.

Examples:


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

[aaa       ]

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

[aaa                           ]

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

aaa-------

Parameters:

  • value (String)
    • value to apply padding to

  • count (Integer) (defaults to: nil)
    • how much padding to apply. defaults to configuration.padr_count

  • char (String) (defaults to: nil)
    • character to pad with. defaults to configuration.padr_char

Returns:

  • (String)

    value with padding to right



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

def parse(value, count = nil, char = nil)
  value = '' if value.nil?
  count = Handlebars::Helpers.configuration.padr_count if count.nil?
  count = count.to_i if count.is_a?(String)
  char = Handlebars::Helpers.configuration.padr_char if char.nil?
  value.to_s.ljust(count, char)
end