Sha256: 04cff12df0743a1da7288fba7522674981fea9947aff15c174909bc7c96667ac

Contents?: true

Size: 812 Bytes

Versions: 2

Compression:

Stored size: 812 Bytes

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023, by Samuel Williams.

module Console
	module Output
		class Encoder
			def initialize(output, encoding = ::Encoding::UTF_8)
				@output = output
				@encoding = encoding
			end
			
			attr :output
			
			attr :encoding
			
			def call(subject = nil, *arguments, **options, &block)
				subject = encode(subject)
				arguments = encode(arguments)
				options = encode(options)
				
				@output.call(subject, *arguments, **options, &block)
			end
			
			def encode(value)
				case value
				when String
					value.encode(@encoding, invalid: :replace, undef: :replace)
				when Array
					value.map{|item| encode(item)}
				when Hash
					value.transform_values{|item| encode(item)}
				else
					value
				end
			end
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
console-1.21.0 lib/console/output/encoder.rb
console-1.20.0 lib/console/output/encoder.rb