Module: Mova::Interpolation::Sprintf::Overridable

Included in:
Mova::Interpolation::Sprintf
Defined in:
lib/mova/interpolation/sprintf.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#missing_placeholder(placeholder, values) ⇒ String

Returns default replacement for missing placeholder.

Examples:

Wrap missing placeholders in HTML tag

interpolator = Mova::Interpolation::Sprintf.new.tap do |i|
  def i.missing_placeholder(placeholder, values)
    "<span class='error'>#{placeholder}<span>"
  end
end
interpolator.call("%{my} %{notes}", my: "your") #=> "your <span class='error'>notes</span>"

Raise an exception in case of missing placeholder

interpolator = Mova::Interpolation::Sprintf.new.tap do |i|
  def i.missing_placeholder(placeholder, values)
    raise KeyError.new("#{placeholder.inspect} is missing, #{values.inspect} given")
  end
end
interpolator.call("%{my} %{notes}", my: "your") #=> KeyError: :notes is missing, {my: "your"} given

Parameters:

  • placeholder (Symbol)
  • values (Hash{Symbol => String})

    all given values for interpolation

Returns:

  • (String)

    default replacement for missing placeholder

Since:

  • 0.1.0



66
67
68
# File 'lib/mova/interpolation/sprintf.rb', line 66

def missing_placeholder(placeholder, values)
  "%{#{placeholder}}"
end