Class: FlatKit::Output::IO

Inherits:
FlatKit::Output show all
Defined in:
lib/flat_kit/output/io.rb

Constant Summary collapse

STDOUTS =
%w[ stdout STDOUT - <stdout> ]
STDERRS =
%w[ stderr STDERR <stderr> ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FlatKit::Output

from, #tell

Methods included from DescendantTracker

#children, #find_child, #find_children, #inherited

Constructor Details

#initialize(obj) ⇒ IO

Returns a new instance of IO.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/flat_kit/output/io.rb', line 36

def initialize(obj)
  @count = 0
  if self.class.is_stdout?(obj) then
    @name = "<STDOUT>"
    @io = $stdout
  elsif self.class.is_stderr?(obj) then
    @name = "<STDERR>"
    @io = $stderr
  elsif obj.kind_of?(::File) then
    @name = obj.path
    @io = obj
  elsif obj.kind_of?(::StringIO) then
    @name = obj.inspect
    @io = obj
  elsif obj.kind_of?(::IO) then
    @name = obj.inspect
    @io = obj
  else
    raise ::FlatKit::Error, "Unable to create #{self.class} from #{obj.class} : #{obj.inspect}"
  end
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



4
5
6
# File 'lib/flat_kit/output/io.rb', line 4

def count
  @count
end

Class Method Details

.handles?(obj) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
# File 'lib/flat_kit/output/io.rb', line 9

def self.handles?(obj)
  return true if is_stderr?(obj)
  return true if is_stdout?(obj)
  return true if [ ::File, ::StringIO, ::IO ].any? { |klass| obj.kind_of?(klass) }
  return false
end

.is_stderr?(obj) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
# File 'lib/flat_kit/output/io.rb', line 16

def self.is_stderr?(obj)
  case obj
  when String
    return true if STDERRS.include?(obj)
  when ::IO
    return true if obj == ::STDERR
  end
  return false
end

.is_stdout?(obj) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
# File 'lib/flat_kit/output/io.rb', line 26

def self.is_stdout?(obj)
  case obj
  when String
    return true if STDOUTS.include?(obj)
  when ::IO
    return true if obj == ::STDOUT
  end
  return false
end

Instance Method Details

#closeObject

this goes to an io stream and we are not in charge of opening it



63
64
65
# File 'lib/flat_kit/output/io.rb', line 63

def close
  @io.close
end

#ioObject

internal api method for testing



68
69
70
# File 'lib/flat_kit/output/io.rb', line 68

def io
  @io
end

#nameObject



58
59
60
# File 'lib/flat_kit/output/io.rb', line 58

def name
  @name
end