Class: Nodectl::Stream::File

Inherits:
Object
  • Object
show all
Defined in:
lib/nodectl/stream/file.rb

Direct Known Subclasses

FileWithMemory

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ File

Returns a new instance of File.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nodectl/stream/file.rb', line 2

def initialize(file, options = {})
  @onread  = []
  @onclose = []

  if file.respond_to? :read_nonblock
    @file = file
  else
    begin
      @file = File.new(file)
    rescue Errno::ENOENT
      raise Nodectl::NotFound, "file not found"
    end
  end

  @options = options

  if @options[:end_offset]
    offset = @file.size - @options[:end_offset]
    if offset > 0
      @file.seek(offset)
      @options[:end_offset].times do
        break if @file.readchar == "\n"
      end
    end
  end
end

Instance Method Details

#closeObject



47
48
49
50
# File 'lib/nodectl/stream/file.rb', line 47

def close
  onclose_call
  @file.close
end

#closed?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/nodectl/stream/file.rb', line 52

def closed?
  @file.closed?
end

#onclose(&blk) ⇒ Object



33
34
35
# File 'lib/nodectl/stream/file.rb', line 33

def onclose(&blk)
  @onclose << blk
end

#onread(&blk) ⇒ Object



29
30
31
# File 'lib/nodectl/stream/file.rb', line 29

def onread(&blk)
  @onread << blk
end

#read_chunkObject



37
38
39
40
41
42
43
44
45
# File 'lib/nodectl/stream/file.rb', line 37

def read_chunk
  chunk = @file.read_nonblock(4096)

  if chunk.size > 0
    onread_call(chunk)
  end
rescue Errno::EAGAIN, EOFError
  # Nothing to do
end