Skip to content

Commit

Permalink
feat(bindings/ruby): implement an IO like class
Browse files Browse the repository at this point in the history
  • Loading branch information
erickguan committed Nov 23, 2024
1 parent 169eb80 commit e68a5da
Show file tree
Hide file tree
Showing 8 changed files with 596 additions and 24 deletions.
1 change: 1 addition & 0 deletions bindings/ruby/lib/opendal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

require_relative "opendal_ruby/version"
require_relative "opendal_ruby/opendal_ruby"
require_relative "opendal_ruby/io"
49 changes: 49 additions & 0 deletions bindings/ruby/lib/opendal_ruby/io.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module OpenDAL
class IO
# Reads all lines from the stream into an array.
# Raises `EOFError` when the end of the file is reached.
def readlines
results = []

loop do
results << readline
rescue EOFError
break
end

results
end

# Rewinds the stream to the beginning.
def rewind
seek(0, ::IO::SEEK_SET)
end

# Sets the file position to `new_position`.
def pos=(new_position)
seek(new_position, ::IO::SEEK_SET)
end

alias_method :pos, :tell

# Checks if the stream is at the end of the file.
def eof
position = tell
seek(0, ::IO::SEEK_END)
tell == position
end

alias_method :eof?, :eof

# Returns the total length of the stream.
def length
current_position = tell
seek(0, ::IO::SEEK_END)
tell.tap { self.pos = current_position }
end

alias_method :size, :length
end
end
2 changes: 1 addition & 1 deletion bindings/ruby/lib/opendal_ruby/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
# frozen_string_literal: true

module OpenDAL
VERSION = "0.1.9"
VERSION = "0.1.10"
end
2 changes: 2 additions & 0 deletions bindings/ruby/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub use ::opendal as ocore;

mod capability;
mod metadata;
mod opendal_io;
mod operator;

pub fn format_magnus_error(err: ocore::Error) -> Error {
Expand All @@ -39,6 +40,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
let _ = operator::include(&gem_module);
let _ = metadata::include(&gem_module);
let _ = capability::include(&gem_module);
let _ = opendal_io::include(&gem_module);

Ok(())
}
Loading

0 comments on commit e68a5da

Please sign in to comment.