progressive_io 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.extra_files << "../some/external/dependency.rb"
7
+ #
8
+ # at.libs << ":../some/external"
9
+ #
10
+ # at.add_exception 'vendor'
11
+ #
12
+ # at.add_mapping(/dependency.rb/) do |f, _|
13
+ # at.files_matching(/test_.*rb$/)
14
+ # end
15
+ #
16
+ # %w(TestA TestB).each do |klass|
17
+ # at.extra_class_map[klass] = "test/test_misc.rb"
18
+ # end
19
+ # end
20
+
21
+ # Autotest.add_hook :run_command do |at|
22
+ # system "rake build"
23
+ # end
File without changes
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2011-08-04
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
@@ -0,0 +1,7 @@
1
+ .autotest
2
+ History.txt
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/progressive_io.rb
7
+ test/test_progressive_io.rb
@@ -0,0 +1,50 @@
1
+ = progressive_io
2
+
3
+ * http://github.com/julik/progressive_io
4
+
5
+ == DESCRIPTION:
6
+
7
+ A wrapper for IO objects that allows a callback to be set which is called when an object is read from
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Wraps any IO
12
+
13
+ == SYNOPSIS:
14
+
15
+ io = ProgressiveIO.new(File.open("/bigfile.dat")) do | pos, total_size |
16
+ puts "Read %d bytes of %d" % [ pos, total_size ]
17
+ end
18
+
19
+ == REQUIREMENTS:
20
+
21
+ * Ruby 1.8.6+
22
+
23
+ == INSTALL:
24
+
25
+ * gem install progressive_io
26
+
27
+ == LICENSE:
28
+
29
+ (The MIT License)
30
+
31
+ Copyright (c) 2011 Julik Tarkhanov <me@julik.nl>
32
+
33
+ Permission is hereby granted, free of charge, to any person obtaining
34
+ a copy of this software and associated documentation files (the
35
+ 'Software'), to deal in the Software without restriction, including
36
+ without limitation the rights to use, copy, modify, merge, publish,
37
+ distribute, sublicense, and/or sell copies of the Software, and to
38
+ permit persons to whom the Software is furnished to do so, subject to
39
+ the following conditions:
40
+
41
+ The above copyright notice and this permission notice shall be
42
+ included in all copies or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
45
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
47
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
48
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
49
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
50
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.spec 'progressive_io' do | p |
7
+ p.developer('Julik Tarkhanov', 'me@ujulik.nl')
8
+ p.readme_file = 'README.rdoc'
9
+ p.extra_rdoc_files = FileList['*.rdoc'] + FileList['*.txt']
10
+ p.extra_dev_deps = {"flexmock" => "~> 0.8"}
11
+ p.clean_globs = File.read(File.dirname(__FILE__) + "/.gitignore").split(/\s/).to_a
12
+ end
13
+
14
+ # vim: syntax=ruby
@@ -0,0 +1,86 @@
1
+ require "delegate"
2
+
3
+ class ProgressiveIO < DelegateClass(IO)
4
+ VERSION = '1.0.0'
5
+
6
+ # Get or set the total size of the contained IO. If the passed IO is a File object
7
+ # the size will be preset automatically
8
+ attr_accessor :total_size
9
+ attr_accessor :progress_block
10
+
11
+ # The constructor accepts an IO object and the block that will be called when the IO is read.
12
+ # If the passed IO is a File-like object that responds to #stat then the size will be computed
13
+ # automatically
14
+ def initialize(with_io, &blk)
15
+ __setobj__(with_io)
16
+ @total_size = with_io.stat.size if with_io.respond_to?(:stat)
17
+ @progress_block = blk.to_proc if blk
18
+ end
19
+
20
+ # Report offset at each line
21
+ def each(sep_string = $/, &blk)
22
+ # Report offset at each call of the iterator
23
+ result = super(sep_string) do | line |
24
+ yield(line)
25
+ notify_read
26
+ end
27
+ end
28
+ alias_method :each_line, :each
29
+
30
+ def each_byte(&blk)
31
+ # Report offset at each call of the iterator
32
+ super { |b| yield(b); notify_read }
33
+ end
34
+
35
+ def getc
36
+ returning(super) { notify_read }
37
+ end
38
+
39
+ def gets
40
+ returning(super) { notify_read }
41
+ end
42
+
43
+ def read(*a)
44
+ returning(super) { notify_read }
45
+ end
46
+
47
+ def readbytes(*a)
48
+ returning(super) { notify_read }
49
+ end
50
+
51
+ def readchar
52
+ returning(super) { notify_read }
53
+ end
54
+
55
+ def readline(*a)
56
+ returning(super) { notify_read }
57
+ end
58
+
59
+ def readlines(*a)
60
+ returning(super) { notify_read }
61
+ end
62
+
63
+ def seek(*a)
64
+ returning(super) { notify_read }
65
+ end
66
+
67
+ def ungetc(*a)
68
+ returning(super) { notify_read }
69
+ end
70
+
71
+ def pos=(p)
72
+ returning(super) { notify_read }
73
+ end
74
+
75
+ private
76
+ # The "returning" idiom copied from ActiveSupport. We know that modern Rubies have
77
+ # Object#tap but why mandate newer Rubies for something as small as this?
78
+ def returning(r)
79
+ yield(r); r
80
+ end
81
+
82
+ # This method will be called when something is read
83
+ def notify_read
84
+ @progress_block.call(pos, @total_size) if @progress_block
85
+ end
86
+ end
@@ -0,0 +1,141 @@
1
+ require "test/unit"
2
+ require "progressive_io"
3
+ require "flexmock"
4
+ require "flexmock/test_unit"
5
+ require "stringio"
6
+
7
+ # http://redmine.ruby-lang.org/issues/4882
8
+ # https://github.com/jimweirich/flexmock/issues/4
9
+ # https://github.com/julik/flexmock/commit/4acea00677e7b558bd564ec7c7630f0b27d368ca
10
+ class FlexMock::PartialMockProxy
11
+ def singleton?(method_name)
12
+ @obj.singleton_methods.include?(method_name.to_s)
13
+ end
14
+ end
15
+
16
+ class TestProgressiveIO < Test::Unit::TestCase
17
+
18
+ def e(s)
19
+
20
+ # Make a mock File object from a string
21
+ io = StringIO.new(s)
22
+ mock_stat = flexmock(:size => s.length)
23
+ flexmock(io).should_receive(:stat).and_return(mock_stat)
24
+
25
+ ProgressiveIO.new(io)
26
+ end
27
+
28
+ def test_each
29
+ io, messages = e("Mary\nHad\nA little\nLamb"), []
30
+
31
+ io.progress_block = lambda do | offset, total |
32
+ messages.push([offset, total])
33
+ end
34
+
35
+ lines = []
36
+ io.each {|line| lines.push(line) }
37
+ assert_equal ["Mary\n", "Had\n", "A little\n", "Lamb"], lines
38
+ assert_equal [[5, 22], [9, 22], [18, 22], [22, 22]], messages
39
+ end
40
+
41
+ def test_each_byte
42
+ io, messages = e("123"), []
43
+
44
+ io.progress_block = lambda do | offset, total |
45
+ messages.push([offset, total])
46
+ end
47
+
48
+ bytes = []
49
+ io.each_byte{|s| bytes << s }
50
+ assert_equal [49, 50, 51], bytes
51
+ assert_equal [[1, 3], [2, 3], [3, 3]], messages
52
+ end
53
+
54
+ def test_getc
55
+ io = e("123")
56
+ io.progress_block = lambda do | offset, total |
57
+ assert_equal [1, 3], [offset, total]
58
+ end
59
+ if RUBY_VERSION < "1.9"
60
+ assert_equal 49, io.getc
61
+ else
62
+ assert_equal "1", io.getc
63
+ end
64
+ end
65
+
66
+ def test_gets
67
+ io = e("Mary\nHad\nA little\nLamb")
68
+ io.progress_block = lambda do | offset, total |
69
+ assert_equal [5, 22], [offset, total]
70
+ end
71
+ assert_equal "Mary\n", io.gets
72
+ end
73
+
74
+ def test_read
75
+ io = e("Mary\nHad\nA little\nLamb")
76
+ io.progress_block = lambda do | offset, total |
77
+ assert_equal [15, 22], [offset, total]
78
+ end
79
+ assert_equal "Mary\nHad\nA litt", io.read(15)
80
+ end
81
+
82
+ def test_readchar
83
+ io = e("123")
84
+ io.progress_block = lambda do | offset, total |
85
+ assert_equal [1, 3], [offset, total]
86
+ end
87
+
88
+ if RUBY_VERSION < "1.9"
89
+ assert_equal 49, io.getc
90
+ else
91
+ assert_equal "1", io.getc
92
+ end
93
+ end
94
+
95
+ def test_readline
96
+ io = e("Mary\nHad\nA little\nLamb")
97
+ io.progress_block = lambda do | offset, total |
98
+ assert_equal [5, 22], [offset, total]
99
+ end
100
+ assert_equal "Mary\n", io.readline
101
+ end
102
+
103
+ def test_readlines
104
+ io = e("Mary\nHad\nA little\nLamb")
105
+ m = []
106
+ io.progress_block = lambda do | offset, total |
107
+ m.push([offset, total])
108
+ end
109
+
110
+ assert_equal ["Mary\n", "Had\n", "A little\n", "Lamb"], io.readlines
111
+ assert_equal [[22, 22]], m
112
+ end
113
+
114
+ def test_seek
115
+ io = e("Mary\nHad\nA little\nLamb")
116
+ io.progress_block = lambda do | offset, total |
117
+ assert_equal [6, 22], [offset, total]
118
+ end
119
+ io.seek(6)
120
+ end
121
+
122
+ def test_ungetc
123
+ io = e("Mary\nHad\nA little\nLamb")
124
+ m = []
125
+ io.progress_block = lambda do | offset, total |
126
+ m.push([offset, total])
127
+ end
128
+
129
+ io.getc
130
+ io.ungetc(2)
131
+ assert_equal [[1, 22], [0, 22]], m
132
+ end
133
+
134
+ def test_poseq
135
+ io = e("Mary\nHad\nA little\nLamb")
136
+ io.progress_block = lambda do | offset, total |
137
+ assert_equal [2, 22], [offset, total]
138
+ end
139
+ io.pos = 2
140
+ end
141
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: progressive_io
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Julik Tarkhanov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-08-04 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: flexmock
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "0.8"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: hoe
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: "2.10"
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: A wrapper for IO objects that allows a callback to be set which is called when an object is read from
38
+ email:
39
+ - me@ujulik.nl
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - History.txt
46
+ - Manifest.txt
47
+ - README.rdoc
48
+ files:
49
+ - .autotest
50
+ - History.txt
51
+ - Manifest.txt
52
+ - README.rdoc
53
+ - Rakefile
54
+ - lib/progressive_io.rb
55
+ - test/test_progressive_io.rb
56
+ - .gemtest
57
+ homepage: http://github.com/julik/progressive_io
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --main
63
+ - README.rdoc
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: progressive_io
81
+ rubygems_version: 1.8.5
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: A wrapper for IO objects that allows a callback to be set which is called when an object is read from
85
+ test_files:
86
+ - test/test_progressive_io.rb