io-access_lazy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 09661a38935bbfd6267f9aa1ddad688585061cd5
4
+ data.tar.gz: acc5667311824cc15a131f1ec8efeb97bcbd5293
5
+ SHA512:
6
+ metadata.gz: 30c252855ea8e0dcb5248cb9d69d18e09daf0d1d506726eb07fa4035bbafd53581fa7090b76aed013a55dfad47566667365a9a1ec89934a3c072c591f1fdb653
7
+ data.tar.gz: 690c244494384b5bf419ae4445ed2260ca52d5c267b4b082f1d95e276d25d9d556a06d742a041490626632689f032a9e09a47a04dde3ab69426c43859742933d
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in io-access_lazy.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,4 @@
1
+ Copyright (c) 2013 USAMI Kenta
2
+
3
+ GPLv3 or NYSLれ。
4
+
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # IO::AccessLazy
2
+
3
+ * <del>さいふおとした。<strong>泣いてる</strong>。</del>
4
+ * <ins>回収されたらしい。世の中捨てたもんじゃない ヾ(〃><)ノ゙☆</ins>
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'io-access_lazy'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install io-access_lazy
19
+
20
+ ## 日本語でおk (Japanese)
21
+
22
+ * IOオブジェクトから任意の行を遅延して取得します
23
+ * シェルスクリプティングだと `head(1)` とか `tail(1)` を使ってやるイメージです
24
+ * **タブンネ**
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ fp = open("hoge.txt", 'r')
30
+ fp.read
31
+ # => "A
32
+ # B
33
+ # C
34
+ # D
35
+ # E
36
+ # F
37
+ # G
38
+ # H"
39
+ fp_lazy = IO::AccessLazy.new(fp)
40
+ line5 = fp_lazy[5]
41
+ # => "F"
42
+ line2_6 = fp_lazy[2..4]
43
+ # => ["C\n", "D\n", "E\n"]
44
+ line3_l = fp_lazy[3..-1]
45
+ # => ["D\n", "E\n", "F\n", "G\n", "H\n"]
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/hoge.txt ADDED
File without changes
@@ -0,0 +1,23 @@
1
+ # -*- coding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'io/access_lazy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "io-access_lazy"
8
+ spec.version = IO::AccessLazy::VERSION
9
+ spec.authors = ["USAMI Kenta"]
10
+ spec.email = ["tadsan@zonu.me"]
11
+ spec.description = %q{This gem provide lazy access method like array to IO object.}
12
+ spec.summary = %q{NURUPO}
13
+ spec.homepage = "http://dt.zonu.me/"
14
+ spec.license = "GPL Version 3 or NYSL"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,87 @@
1
+ require 'io/access_lazy/version'
2
+
3
+ # a class of the Ruby core library
4
+ class IO; end
5
+
6
+ # AccessLazy class
7
+ class IO::AccessLazy
8
+ # @param [IO] io
9
+ # @param [Hash] options
10
+ def initialize (io, options={})
11
+ set_io! (io)
12
+ set_option! (options)
13
+ end
14
+
15
+ attr_reader :io
16
+ attr_reader :seek_method
17
+
18
+ # @param [Fixnum,Range] index
19
+ # @param [Boolean] cache
20
+ def [] (index, cache=nil)
21
+ case index
22
+ when Fixnum
23
+ io_init! unless cache
24
+ seek(index)
25
+ when Range
26
+ unless check_range(index)
27
+ raise ArgumentError, 'require IO object or String'
28
+ end
29
+ io_init!
30
+ index.map{|i| self[i, true] }
31
+ end
32
+ end
33
+
34
+ # @param [Fixnum] index
35
+ # @return [String]
36
+ def seek (index)
37
+ @point.upto(index){ |i|
38
+ tmp = io.gets
39
+ @point += 1
40
+ return tmp if index == i
41
+ }
42
+ end
43
+
44
+ # @param [IO] io
45
+ def set_io! (io)
46
+ case io
47
+ when IO
48
+ @io = io
49
+ when String
50
+ @io = StringIO.new(io)
51
+ else
52
+ raise ArgumentError, 'index is invalid range'
53
+ end
54
+ end
55
+
56
+ # @param [Hash] options
57
+ def set_option! (options)
58
+ # Set IO seek method
59
+ @seek_method = options[:seek_method] || :gets
60
+ end
61
+
62
+ # @note Side-Effect!
63
+ def cache_init!
64
+ @cache = []
65
+ return true
66
+ end
67
+
68
+ # @note Side-Effect!
69
+ def io_init!
70
+ @point = io.rewind
71
+ return true
72
+ end
73
+
74
+ # @param [Range] range
75
+ # @return [Boolean]
76
+ # @note This is a alias method.
77
+ # @see AccessLazy.check_range
78
+ def check_range (range); self.class.check_range(range); end
79
+
80
+ # @param [Range] range
81
+ # @return [Boolean]
82
+ def self.check_range (range)
83
+ lower = range.begin
84
+ upper = range.end
85
+ (lower >= 0) && ((lower <= upper) && (upper >= 0) || (upper == -1))
86
+ end
87
+ end
@@ -0,0 +1,5 @@
1
+ # -*- coding: utf-8 -*-
2
+ class IO::AccessLazy
3
+ # Version number of IO::AccessLazy
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe IO::AccessLazy do
4
+ describe '#[]' do
5
+ let(:sample){ str = <<-EOT
6
+ A
7
+ B
8
+ C
9
+ D
10
+ E
11
+ F
12
+ G
13
+ EOT
14
+ return IO::AccessLazy.new(str)
15
+ }
16
+ it{ expect(sample).to be_a IO::AccessLazy }
17
+ it{ expect(sample[0]).to eq "A\n" }
18
+ it{ expect(sample[2..3]).to eq ["C\n", "D\n"] }
19
+ it{ expect(sample[4..-1]).to eq ["E\n", "F\n", "G\n"] }
20
+ end
21
+ describe '.check_range' do
22
+ context 'lower limit is 0' do
23
+ it{ expect(IO::AccessLazy.check_range(0..0)).to be_true }
24
+ it{ expect(IO::AccessLazy.check_range(0..-1)).to be_true }
25
+ it{ expect(IO::AccessLazy.check_range(0..10)).to be_true }
26
+ end
27
+
28
+ context 'lower limit is negative' do
29
+ it{ expect(IO::AccessLazy.check_range(-1..0)).to be_false }
30
+ it{ expect(IO::AccessLazy.check_range(-3..5)).to be_false }
31
+ end
32
+
33
+ context 'upper limit is negative' do
34
+ it{ expect(IO::AccessLazy.check_range(0..-5)).to be_false }
35
+ it{ expect(IO::AccessLazy.check_range(-1..-1)).to be_false }
36
+ end
37
+
38
+ context 'lower limit is greater than upper limit' do
39
+ it{ expect(IO::AccessLazy.check_range(10..5)).to be_false }
40
+ it{ expect(IO::AccessLazy.check_range(8..0)).to be_false }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,4 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $:.unshift(lib)
3
+
4
+ require 'io/access_lazy'
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: io-access_lazy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - USAMI Kenta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: This gem provide lazy access method like array to IO object.
42
+ email:
43
+ - tadsan@zonu.me
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - hoge.txt
54
+ - io-access_lazy.gemspec
55
+ - lib/io/access_lazy.rb
56
+ - lib/io/access_lazy/version.rb
57
+ - spec/io-access_lazy_spec.rb
58
+ - spec/spec_helper.rb
59
+ homepage: http://dt.zonu.me/
60
+ licenses:
61
+ - GPL Version 3 or NYSL
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.0.0
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: NURUPO
83
+ test_files:
84
+ - spec/io-access_lazy_spec.rb
85
+ - spec/spec_helper.rb
86
+ has_rdoc: