Class: IO::AccessLazy
- Inherits:
-
Object
- Object
- IO::AccessLazy
- Defined in:
- lib/io/access_lazy.rb,
lib/io/access_lazy/version.rb
Overview
AccessLazy class
Constant Summary collapse
- VERSION =
Version number of IO::AccessLazy
"0.0.1"
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#seek_method ⇒ Object
readonly
Returns the value of attribute seek_method.
Class Method Summary collapse
Instance Method Summary collapse
- #[](index, cache = nil) ⇒ Object
- #cache_init! ⇒ Object
- #check_range(range) ⇒ Boolean
-
#initialize(io, options = {}) ⇒ AccessLazy
constructor
A new instance of AccessLazy.
- #io_init! ⇒ Object
- #seek(index) ⇒ String
- #set_io!(io) ⇒ Object
- #set_option!(options) ⇒ Object
Constructor Details
#initialize(io, options = {}) ⇒ AccessLazy
Returns a new instance of AccessLazy.
10 11 12 13 |
# File 'lib/io/access_lazy.rb', line 10 def initialize (io, ={}) set_io! (io) set_option! () end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
15 16 17 |
# File 'lib/io/access_lazy.rb', line 15 def io @io end |
#seek_method ⇒ Object (readonly)
Returns the value of attribute seek_method.
16 17 18 |
# File 'lib/io/access_lazy.rb', line 16 def seek_method @seek_method end |
Class Method Details
.check_range(range) ⇒ Boolean
82 83 84 85 86 |
# File 'lib/io/access_lazy.rb', line 82 def self.check_range (range) lower = range.begin upper = range.end (lower >= 0) && ((lower <= upper) && (upper >= 0) || (upper == -1)) end |
Instance Method Details
#[](index, cache = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/io/access_lazy.rb', line 20 def [] (index, cache=nil) case index when Fixnum io_init! unless cache seek(index) when Range unless check_range(index) raise ArgumentError, 'require IO object or String' end io_init! index.map{|i| self[i, true] } end end |
#cache_init! ⇒ Object
Note:
Side-Effect!
63 64 65 66 |
# File 'lib/io/access_lazy.rb', line 63 def cache_init! @cache = [] return true end |
#check_range(range) ⇒ Boolean
Note:
This is a alias method.
78 |
# File 'lib/io/access_lazy.rb', line 78 def check_range (range); self.class.check_range(range); end |
#io_init! ⇒ Object
Note:
Side-Effect!
69 70 71 72 |
# File 'lib/io/access_lazy.rb', line 69 def io_init! @point = io.rewind return true end |
#seek(index) ⇒ String
36 37 38 39 40 41 42 |
# File 'lib/io/access_lazy.rb', line 36 def seek (index) @point.upto(index){ |i| tmp = io.gets @point += 1 return tmp if index == i } end |
#set_io!(io) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/io/access_lazy.rb', line 45 def set_io! (io) case io when IO @io = io when String @io = StringIO.new(io) else raise ArgumentError, 'index is invalid range' end end |
#set_option!(options) ⇒ Object
57 58 59 60 |
# File 'lib/io/access_lazy.rb', line 57 def set_option! () # Set IO seek method @seek_method = [:seek_method] || :gets end |