lib/jsonnet/vm.rb in jsonnet-0.3.0 vs lib/jsonnet/vm.rb in jsonnet-0.4.0
- old
+ new
@@ -12,13 +12,13 @@
# @param options [Hash] options to {.new} or options to {#evaluate}
# @return [String]
# @see #evaluate
def evaluate(snippet, options = {})
snippet_check = ->(key, value) { key.to_s.match(/^filename|multi$/) }
- snippet_options = options.select &snippet_check
- vm_options = options.reject &snippet_check
- new(vm_options).evaluate(snippet, snippet_options)
+ snippet_options = options.select(&snippet_check)
+ vm_options = options.reject(&snippet_check)
+ new(vm_options).evaluate(snippet, **snippet_options)
end
##
# Convenient method to evaluate a Jsonnet file.
#
@@ -28,13 +28,13 @@
# @param options [Hash] options to {.new} or options to {#evaluate_file}
# @return [String]
# @see #evaluate_file
def evaluate_file(filename, options = {})
file_check = ->(key, value) { key.to_s.match(/^encoding|multi$/) }
- file_options = options.select &file_check
- vm_options = options.reject &file_check
- new(vm_options).evaluate_file(filename, file_options)
+ file_options = options.select(&file_check)
+ vm_options = options.reject(&file_check)
+ new(vm_options).evaluate_file(filename, **file_options)
end
end
##
# initializes a new VM with the given configuration.
@@ -87,17 +87,42 @@
def evaluate_file(filename, encoding: Encoding.default_external, multi: false)
eval_file(filename, encoding, multi)
end
##
+ # Format Jsonnet file.
+ #
+ # @param [String] filename filename of a Jsonnet source file.
+ # @return [String] a formatted Jsonnet representation
+ # @raise [FormatError] raised when the formatting results an error.
+ def format_file(filename, encoding: Encoding.default_external)
+ fmt_file(filename, encoding)
+ end
+
+ ##
+ # Format Jsonnet snippet.
+ #
+ # @param [String] jsonnet Jsonnet source string. Must be encoded in ASCII-compatible encoding.
+ # @param [String] filename filename of the source. Used in stacktrace.
+ # @return [String] a formatted Jsonnet representation
+ # @raise [FormatError] raised when the formatting results an error.
+ # @raise [UnsupportedEncodingError] raised when the encoding of jsonnt is not ASCII-compatible.
+ def format(jsonnet, filename: "(jsonnet)")
+ fmt_snippet(jsonnet, filename)
+ end
+
+ ##
# Lets the given block handle "import" expression of Jsonnet.
# @yieldparam [String] base base path to resolve "rel" from.
# @yieldparam [String] rel a relative or absolute path to the file to be imported
# @yieldreturn [Array<String>] a pair of the content of the imported file and
# its path.
- def handle_import
- self.import_callback = to_method(Proc.new)
+ def handle_import(&block)
+ if block.nil?
+ raise ArgumentError, 'handle_import requires a block'
+ end
+ self.import_callback = to_method(block)
nil
end
##
# Define a function (native extension) in the VM and let the given block
@@ -110,11 +135,14 @@
#
# @note Currently it cannot define keyword or optional paramters in Jsonnet.
# Also all the positional optional parameters of the body are interpreted
# as required parameters. And the body cannot have keyword, rest or
# keyword rest paramters.
- def define_function(name, body = nil)
- body = body ? body.to_proc : Proc.new
+ def define_function(name, body = nil, &block)
+ body = body ? body.to_proc : block
+ if body.nil?
+ raise ArgumentError, 'define_function requires a body argument or a block'
+ end
params = body.parameters.map.with_index do |(type, name), i|
raise ArgumentError, "rest or keyword parameters are not allowed: #{type}" \
unless [:req, :opt].include? type
name || "p#{i}"