Module: OpendkimCookbook::TemplateHelpers

Defined in:
libraries/template_helpers.rb

Overview

Some helpers to use from opendkim cookbook templates.

Examples:

self.class.send(:include, ::OpendkimCookbook::TemplateHelpers)
value_to_s(true) #=> "true"
value_to_s([1, 'string', { a: 1 }]) #=> "1,string,a=1"

Instance Method Summary collapse

Instance Method Details

#array_to_s(ary) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a Ruby array to a configuration list separated by commas.

Examples:

array_to_s([3, '4']) #=> "a=1,b=2,c=3,4"

Parameters:

  • ary (Array)

    array to convert.

Returns:

  • (String)

    configuration value as string.


36
37
38
# File 'libraries/template_helpers.rb', line 36

def array_to_s(ary)
  ary.map { |x| value_to_s(x) }.join(',')
end

#hash_to_s(hs) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a Ruby hash to a configuration list separated by commas.

Examples:

hash_to_s({ a: 1, b: '2' }) #=> "a=1,b=2"

Parameters:

  • hs (Hash)

    hash to convert.

Returns:

  • (String)

    configuration value as string.


47
48
49
50
# File 'libraries/template_helpers.rb', line 47

def hash_to_s(hs)
  value_ary = hs.map { |k, v| "#{k}=#{value_to_s(v)}" }
  array_to_s(value_ary)
end

#value_to_s(v) ⇒ String

Converts a Ruby configuration value to its string representation.

Examples:

value_to_s(true') #=> "true"
value_to_s([1, 'string', { a: 1 }]) #=> "1,string,a=1"
value_to_s({ a: 1, 'b' => '2', 'c' => [3, '4'] }) #=> "a=1,b=2,c=3,4"

Parameters:

  • v (Mixed)

    the configuration value.

Returns:

  • (String)

    the configuration value as string.


60
61
62
63
64
65
66
67
# File 'libraries/template_helpers.rb', line 60

def value_to_s(v)
  case v
  when Array then array_to_s(v)
  when Hash then hash_to_s(v)
  else
    v.to_s
  end
end