Class: FlatKit::Input::IO

Inherits:
FlatKit::Input show all
Defined in:
lib/flat_kit/input/io.rb

Overview

Internal: Handler for non-filebased input. Generally this is just stdin

Constant Summary collapse

STDINS =
%w[stdin STDIN - <stdin>].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FlatKit::Input

from

Methods included from DescendantTracker

#children, #find_child, #find_children, #inherited

Constructor Details

#initialize(obj) ⇒ IO

Returns a new instance of IO.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/flat_kit/input/io.rb', line 27

def initialize(obj)
  super()
  if self.class.stdin?(obj)
    @name = "<STDIN>"
    @io = $stdin
  elsif obj.is_a?(::IO)
    @name = (obj.respond_to?(:path) && obj.path) || obj.inspect
    @io = obj
  elsif obj.is_a?(::StringIO)
    @name = obj.inspect
    @io = obj
  else
    raise ::FlatKit::Error, "Unable to create #{self.class} from #{obj.class} : #{obj.inspect}"
  end
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



43
44
45
# File 'lib/flat_kit/input/io.rb', line 43

def io
  @io
end

#nameObject (readonly)

Returns the value of attribute name.



43
44
45
# File 'lib/flat_kit/input/io.rb', line 43

def name
  @name
end

Class Method Details

.handles?(obj) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.handles?(obj)
  return true if stdin?(obj)
  return true if [::File, ::StringIO, ::IO].any? { |klass| obj.is_a?(klass) }

  false
end

.stdin?(obj) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.stdin?(obj)
  case obj
  when String
    return true if STDINS.include?(obj)
  when ::IO
    return true if obj == $stdin
  end
  false
end

Instance Method Details

#closeObject

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



46
47
48
# File 'lib/flat_kit/input/io.rb', line 46

def close
  @io.close
end