Class: IO::AccessLazy

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, options = {}) ⇒ AccessLazy

Returns a new instance of AccessLazy.

Parameters:

  • io (IO)
  • options (Hash) (defaults to: {})


10
11
12
13
# File 'lib/io/access_lazy.rb', line 10

def initialize (io, options={})
  set_io! (io)
  set_option! (options)
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



15
16
17
# File 'lib/io/access_lazy.rb', line 15

def io
  @io
end

#seek_methodObject (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

Parameters:

  • range (Range)

Returns:

  • (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

Parameters:

  • index (Fixnum, Range)
  • cache (Boolean) (defaults to: nil)


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.

Parameters:

  • range (Range)

Returns:

  • (Boolean)

See Also:



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

Parameters:

  • index (Fixnum)

Returns:

  • (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

Parameters:



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

Parameters:

  • options (Hash)


57
58
59
60
# File 'lib/io/access_lazy.rb', line 57

def set_option! (options)
  # Set IO seek method
  @seek_method = options[:seek_method] || :gets
end