Class: External::Base
- Inherits:
-
Object
- Object
- External::Base
- 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
Constant Summary collapse
- TEMPFILE_BASENAME =
The default tempfile basename for Base instances initialized without an io.
"external_base"
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
The underlying io for self.
Attributes included from Chunkable
Attributes included from Enumerable
Class Method Summary collapse
-
.open(path = nil, mode = "rb", *argv) ⇒ Object
Initializes an instance of self with File.open(path, mode) as an io.
Instance Method Summary collapse
-
#another ⇒ Object
Returns another instance of self.
-
#close(path = nil, overwrite = false) ⇒ Object
Closes io.
-
#closed? ⇒ Boolean
True if io is closed.
-
#dup ⇒ Object
Returns a duplicate of self.
-
#empty? ⇒ Boolean
Returns true if self contains no elements.
- #eql?(another) ⇒ Boolean
-
#first(n = nil) ⇒ Object
Returns the first n entries (default 1).
-
#flush ⇒ Object
Flushes the io and resets the io length.
-
#initialize(io = nil) ⇒ Base
constructor
Creates a new instance of self with the specified io.
- #inspect ⇒ Object
-
#slice(one, two = nil) ⇒ Object
Alias for [].
-
#to_ary ⇒ Object
Returns self.
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.
60 61 62 63 64 65 66 67 68 |
# File 'lib/external/base.rb', line 60 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
#io ⇒ Object
The underlying io for self.
51 52 53 |
# File 'lib/external/base.rb', line 51 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 40 41 42 43 44 |
# File 'lib/external/base.rb', line 26 def open(path=nil, mode="rb", *argv) begin io = path == nil ? nil : File.open(path, mode) base = new(io, *argv) rescue(Errno::ENOENT) io.close if io raise end if block_given? begin yield(base) ensure base.close end else base end end |
Instance Method Details
#another ⇒ Object
Returns another instance of self. Must be implemented in a subclass.
121 122 123 |
# File 'lib/external/base.rb', line 121 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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/external/base.rb', line 79 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.
71 72 73 |
# File 'lib/external/base.rb', line 71 def closed? io.closed? end |
#dup ⇒ Object
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.
114 115 116 117 |
# File 'lib/external/base.rb', line 114 def dup flush another.concat(self) end |
#empty? ⇒ Boolean
Returns true if self contains no elements
130 131 132 |
# File 'lib/external/base.rb', line 130 def empty? length == 0 end |
#eql?(another) ⇒ Boolean
134 135 136 |
# File 'lib/external/base.rb', line 134 def eql?(another) self == another end |
#first(n = nil) ⇒ Object
Returns the first n entries (default 1)
139 140 141 |
# File 'lib/external/base.rb', line 139 def first(n=nil) n.nil? ? self[0] : self[0,n] end |
#flush ⇒ Object
Flushes the io and resets the io length. Returns self
105 106 107 108 109 |
# File 'lib/external/base.rb', line 105 def flush io.flush io.reset_length self end |
#inspect ⇒ Object
159 160 161 |
# File 'lib/external/base.rb', line 159 def inspect "#<#{self.class}:#{object_id} #{ellipse_inspect(self)}>" end |
#slice(one, two = nil) ⇒ Object
Alias for []
144 145 146 |
# File 'lib/external/base.rb', line 144 def slice(one, two = nil) self[one, two] end |
#to_ary ⇒ Object
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
154 155 156 |
# File 'lib/external/base.rb', line 154 def to_ary self end |