Class: TTY::Conversion::ArrayConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/conversion/converter/array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = String) ⇒ ArrayConverter

Returns a new instance of ArrayConverter.



11
12
13
14
# File 'lib/tty/conversion/converter/array.rb', line 11

def initialize(source = String)
  @source = source
  @target = Array
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/tty/conversion/converter/array.rb', line 9

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



7
8
9
# File 'lib/tty/conversion/converter/array.rb', line 7

def target
  @target
end

Instance Method Details

#convert(value, copy = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tty/conversion/converter/array.rb', line 17

def convert(value, copy = false)
  case value
  when Array
    copy ? value.dup : value
  when Hash
    Array(value)
  else
    begin
      converted = value.to_ary
    rescue Exception => e
      raise TTY::TypeError,
            "Cannot convert #{value.class} into an Array (#{e.message})"
    end
    converted
  end
end