Class: External::Base

Inherits:
Object
  • Object
show all
Includes:
Chunkable, Enumerable
Defined in:
lib/external/base.rb

Overview

Base provides shared IO and Array-like methods used by ExternalArchive, ExternalArray, and ExternalIndex.

Direct Known Subclasses

ExternalArchive, ExternalIndex

Constant Summary collapse

TEMPFILE_BASENAME =

The default tempfile basename for Base instances initialized without an io.

"external_base"

Instance Attribute Summary collapse

Attributes included from Chunkable

#default_blksize, #length

Attributes included from Enumerable

#enumerate_to_a

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Chunkable

#chunk, #default_span, range_begin_and_end, #reverse_chunk, split_range, split_span

Methods included from Enumerable

#all?, #any?, #collect, #detect, #each_with_index, #entries, #find, #find_all, #include?, #map, #member?, #select

Constructor Details

#initialize(io = nil) ⇒ Base

Creates a new instance of self with the specified io. A nil io causes initialization with a Tempfile; a string io will be converted into a StringIO.



55
56
57
58
59
60
61
62
63
# File 'lib/external/base.rb', line 55

def initialize(io=nil)
  self.io = case io
  when nil then Tempfile.new(TEMPFILE_BASENAME)
  when String then StringIO.new(io)
  else io
  end
  
  @enumerate_to_a = true
end

Instance Attribute Details

#ioObject

The underlying io for self.



46
47
48
# File 'lib/external/base.rb', line 46

def io
  @io
end

Class Method Details

.open(path = nil, mode = "rb", *argv) ⇒ Object

Initializes an instance of self with File.open(path, mode) as an io. As with File.open, the instance will be passed to the block and closed when the block returns. If no block is given, open returns the new instance.

Nil may be provided as an fd, in which case a Tempfile will be used (in which case mode gets ignored as Tempfiles always open in ‘r+’ mode).



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/external/base.rb', line 26

def open(path=nil, mode="rb", *argv)
  path = File.open(path, mode) unless path == nil
  base = new(path, *argv)
  
  if block_given?
    begin
      yield(base)
    ensure
      base.close
    end
  else
    base
  end
end

Instance Method Details

#anotherObject

Returns another instance of self. Must be implemented in a subclass.

Raises:

  • (NotImplementedError)


116
117
118
# File 'lib/external/base.rb', line 116

def another
  raise NotImplementedError
end

#close(path = nil, overwrite = false) ⇒ Object

Closes io. If a path is specified, io will be dumped to it. If io is a File or Tempfile, the existing file is moved (not dumped) to path. Raises an error if path already exists and overwrite is not specified.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/external/base.rb', line 74

def close(path=nil, overwrite=false)
  result = !io.closed?
  
  if path 
    if File.exists?(path) && !overwrite
      raise ArgumentError, "already exists: #{path}"
    end
  
    case io
    when File, Tempfile
      io.close unless io.closed?
      FileUtils.move(io.path, path)
    else
      io.flush
      io.rewind
      File.open(path, "w") do |file|
         file << io.read(io.default_blksize) while !io.eof?
      end
    end
  end
  
  io.close unless io.closed?
  result
end

#closed?Boolean

True if io is closed.

Returns:

  • (Boolean)


66
67
68
# File 'lib/external/base.rb', line 66

def closed?
  io.closed?
end

#dupObject

Returns a duplicate of self. This can be a slow operation as it may involve copying the full contents of one large file to another.



109
110
111
112
# File 'lib/external/base.rb', line 109

def dup
  flush
  another.concat(self)
end

#empty?Boolean

Returns true if self contains no elements

Returns:

  • (Boolean)


125
126
127
# File 'lib/external/base.rb', line 125

def empty?
  length == 0
end

#eql?(another) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/external/base.rb', line 129

def eql?(another)
  self == another
end

#first(n = nil) ⇒ Object

Returns the first n entries (default 1)



134
135
136
# File 'lib/external/base.rb', line 134

def first(n=nil)
  n.nil? ? self[0] : self[0,n]
end

#flushObject

Flushes the io and resets the io length. Returns self



100
101
102
103
104
# File 'lib/external/base.rb', line 100

def flush
  io.flush
  io.reset_length
  self
end

#inspectObject



154
155
156
# File 'lib/external/base.rb', line 154

def inspect
  "#<#{self.class}:#{object_id} #{ellipse_inspect(self)}>"
end

#slice(one, two = nil) ⇒ Object

Alias for []



139
140
141
# File 'lib/external/base.rb', line 139

def slice(one, two = nil)
  self[one, two]
end

#to_aryObject

Returns self. – Warning – errors show up when this doesn’t return an Array… however to return an array with to_ary may mean converting a Base into an Array for insertions… see/modify convert_to_ary



149
150
151
# File 'lib/external/base.rb', line 149

def to_ary
  self
end