Class: FlatKit::Input::IO

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

Constant Summary collapse

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

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.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/flat_kit/input/io.rb', line 22

def initialize(obj)
  if self.class.is_stdin?(obj) then
    @name = "<STDIN>"
    @io = $stdin
  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

Class Method Details

.handles?(obj) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
# File 'lib/flat_kit/input/io.rb', line 6

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

.is_stdin?(obj) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
# File 'lib/flat_kit/input/io.rb', line 12

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

Instance Method Details

#closeObject

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



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

def close
  @io.close
end

#ioObject



49
50
51
# File 'lib/flat_kit/input/io.rb', line 49

def io
  @io
end

#nameObject



40
41
42
# File 'lib/flat_kit/input/io.rb', line 40

def name
  @name
end