class VisualizeRuby::InputCoercer

Attributes

input[R]
name[R]

Public Class Methods

new(input, name:) click to toggle source
# File lib/visualize_ruby/input_coercer.rb, line 7
def initialize(input, name:)
  @input = input
  @name = name
end

Public Instance Methods

close!() click to toggle source
# File lib/visualize_ruby/input_coercer.rb, line 36
def close!
  @temp_file.close! if @temp_file
end
load_file() click to toggle source
# File lib/visualize_ruby/input_coercer.rb, line 49
def load_file
  load(to_file.path)
end
read() click to toggle source
# File lib/visualize_ruby/input_coercer.rb, line 25
def read
  case input
  when String
    input
  when File, Tempfile, Pathname
    to_file.read
  else
    raise ArgumentError, "#{name} was given an unknown type #{input.class}"
  end
end
temp_file() click to toggle source
# File lib/visualize_ruby/input_coercer.rb, line 40
def temp_file
  @temp_file ||= begin
    file = Tempfile.new(%w(calling_code .rb))
    file.write(input)
    file.rewind
    file
  end
end
to_file() click to toggle source
# File lib/visualize_ruby/input_coercer.rb, line 12
def to_file
  @to_file ||= case input
               when File
                 input
               when Pathname
                 File.open(input)
               when String
                 temp_file
               else
                 raise ArgumentError, "#{name} was given an unknown type #{input.class}"
               end
end
to_proc() click to toggle source
# File lib/visualize_ruby/input_coercer.rb, line 53
def to_proc
  if input.is_a?(Proc)
    input
  else
    Proc.new { load_file }
  end
end