lib/template_params/assertion.rb in template_params-0.1.0 vs lib/template_params/assertion.rb in template_params-0.1.1
- old
+ new
@@ -1,10 +1,11 @@
+# frozen_string_literal: true
require "method_source"
module TemplateParams
+ # A simple assertion suitable for view template preconditions.
class Assertion
-
# @api public
def initialize(type, options)
@type = type
@options = options
end
@@ -43,11 +44,11 @@
# Raises a `TypeError` if `value` is not of `@type`.
# @api private
def assert_type(value)
unless @type.nil? || value.is_a?(@type) || allow_nil && value.nil?
- fail TypeError, format("Expected %s, got %s", @type, value.class)
+ raise TypeError, format("Expected %s, got %s", @type, value.class)
end
end
# Given a `NameError` and the block, return a string like:
#
@@ -57,13 +58,14 @@
#
# `Proc#source` is provided by the `method_source` gem.
#
# @api private
def udef_msg(name_error, block)
- msg = "Undefined template parameter: #{name_error}"
+ prefix = "Undefined template parameter: #{name_error}"
if block.respond_to?(:source)
- msg << ": " + block.source.strip
+ format("%s: %s", prefix, block.source.strip)
+ else
+ prefix
end
- msg
end
end
end