Class: Console::Output::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/console/output/encoder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, encoding = ::Encoding::UTF_8) ⇒ Encoder

Returns a new instance of Encoder.



9
10
11
12
# File 'lib/console/output/encoder.rb', line 9

def initialize(output, encoding = ::Encoding::UTF_8)
	@output = output
	@encoding = encoding
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



16
17
18
# File 'lib/console/output/encoder.rb', line 16

def encoding
  @encoding
end

#outputObject (readonly)

Returns the value of attribute output.



14
15
16
# File 'lib/console/output/encoder.rb', line 14

def output
  @output
end

Instance Method Details

#call(subject = nil, *arguments, **options, &block) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/console/output/encoder.rb', line 18

def call(subject = nil, *arguments, **options, &block)
	subject = encode(subject)
	arguments = encode(arguments)
	options = encode(options)
	
	@output.call(subject, *arguments, **options, &block)
end

#encode(value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/console/output/encoder.rb', line 26

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