Class: RailsConsoleAi::PrefixedIO

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_console_ai/prefixed_io.rb

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ PrefixedIO

Returns a new instance of PrefixedIO.



3
4
5
# File 'lib/rails_console_ai/prefixed_io.rb', line 3

def initialize(io)
  @io = io
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



55
56
57
# File 'lib/rails_console_ai/prefixed_io.rb', line 55

def method_missing(method, *args, &block)
  @io.send(method, *args, &block)
end

Instance Method Details

#flushObject



47
48
49
# File 'lib/rails_console_ai/prefixed_io.rb', line 47

def flush
  @io.flush
end


40
41
42
43
44
45
# File 'lib/rails_console_ai/prefixed_io.rb', line 40

def print(*args)
  if (capture = Thread.current[:capture_io])
    return capture.print(*args)
  end
  @io.print(*args)
end

#puts(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails_console_ai/prefixed_io.rb', line 20

def puts(*args)
  if (capture = Thread.current[:capture_io])
    return capture.puts(*args)
  end
  prefix = Thread.current[:log_prefix]
  if prefix
    args = [""] if args.empty?
    args.each do |a|
      line = a.to_s
      if line.strip.empty?
        @io.write("\n")
      else
        @io.write("#{prefix} #{line}\n")
      end
    end
  else
    @io.puts(*args)
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rails_console_ai/prefixed_io.rb', line 51

def respond_to_missing?(method, include_private = false)
  @io.respond_to?(method, include_private) || super
end

#write(str) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rails_console_ai/prefixed_io.rb', line 7

def write(str)
  if (capture = Thread.current[:capture_io])
    return capture.write(str)
  end
  prefix = Thread.current[:log_prefix]
  if prefix && str.is_a?(String) && !str.strip.empty?
    prefixed = str.gsub(/^(?=.)/, "#{prefix} ")
    @io.write(prefixed)
  else
    @io.write(str)
  end
end