time_left 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ Rakefile
3
+ init.rb
4
+ lib/time_left.rb
5
+ Manifest
data/README.rdoc ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('time_left', '0.1.0') do |p|
6
+ p.description = "Print out time that left doing some process"
7
+ p.url = ""
8
+ p.author = "Artur Meisters"
9
+ p.email = "arturs@ithouse.lv"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'time_left'
data/lib/time_left.rb ADDED
@@ -0,0 +1,88 @@
1
+ class TimeLeft
2
+ # Monitor time that is left to do some stuff.
3
+ # Usage
4
+ # total_records=1000
5
+ # t=TimeLeft.new(:total=>total_records)
6
+ # 1.upto(1000) do |i|
7
+ # t.tick(i) do
8
+ # show_when i%100==0
9
+ # use_format "%S.%U"
10
+ # end
11
+ # end
12
+ #
13
+ # t2=TimeLeft.new()
14
+ # while data_processed?
15
+ # data=collect_data_from_remote
16
+ # t.tick do
17
+ # data=collect_data_from_remote unless data
18
+ # self.total=data.size if data
19
+ # show_when Time.now-self.start_time>0.5
20
+ # end
21
+ # end
22
+ attr_accessor :start_time,:visible,:format,:total,:value
23
+
24
+ def initialize(options={})
25
+ self.reset(options)
26
+ end
27
+
28
+ def reset(options={})
29
+ options.each{|k,v| self.send(:"#{k}=",v)}
30
+ end
31
+
32
+ # Start to show time
33
+ # ===Example
34
+ # t=TimeLeft.new(:total=>1000)
35
+ # 1.upto(10) do |i|
36
+ # t.tick(i) do
37
+ # show_when i>1
38
+ # end
39
+ # end
40
+ def tick(value=nil,&block)
41
+ self.start_time=Time.now if !self.start_time
42
+ self.value=value || Time.now.to_i
43
+
44
+ if block_given?
45
+ self.instance_eval(&block)
46
+ else
47
+ self.visible=true
48
+ end
49
+ if self.visible
50
+ self.time_out
51
+ self.visible=false
52
+ end
53
+ end
54
+
55
+ # Use conditions to output time.
56
+ # ===Exammple
57
+ # show_when a%5==0
58
+ # show_when Time.now-self.start_time>0.5 # every half second
59
+ def show_when conditions
60
+ self.visible=true if conditions
61
+ end
62
+
63
+ # Available format
64
+ # * %H - Hours of day (0..23)
65
+ # * %M - Minutes of hour (0..59)
66
+ # * %S - Seconds of minute (0..59)
67
+ # * %U - Miliseconds of second (0..999)
68
+ def use_format format
69
+ self.format=format
70
+ end
71
+
72
+ def time_out
73
+ if self.total.to_f>0
74
+ part_done=self.value.to_f/self.total.to_f
75
+ end
76
+ if part_done>0
77
+ spent_time=(Time.now-self.start_time)
78
+ total_time=spent_time/part_done
79
+ difference=total_time-spent_time
80
+ if difference>0
81
+ time_left=Time.at(difference)
82
+ h,m,s,ms=(time_left.hour-2),time_left.min,time_left.sec,time_left.usec
83
+ h,m,s,ms=(h>9 ? h.to_s : "0#{h}"),(m>9 ? m.to_s : "0#{m}"),(s>9 ? s.to_s : "0#{s}"),(ms>99 ? ms.to_s : (ms>9 ? "0#{ms}" : "00#{ms}"))[0..2]
84
+ puts self.format.gsub(/%H/,h).gsub(/%M/,m).gsub(/%S/,s).gsub(/%U/,ms)
85
+ end
86
+ end
87
+ end
88
+ end
data/time_left.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{time_left}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Artur Meisters"]
9
+ s.date = %q{2010-04-28}
10
+ s.description = %q{Print out time that left doing some process}
11
+ s.email = %q{arturs@ithouse.lv}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/time_left.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "init.rb", "lib/time_left.rb", "Manifest", "time_left.gemspec"]
14
+ s.homepage = %q{}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Time_left", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{time_left}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Print out time that left doing some process}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_left
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artur Meisters
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-04-28 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Print out time that left doing some process
17
+ email: arturs@ithouse.lv
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/time_left.rb
25
+ files:
26
+ - README.rdoc
27
+ - Rakefile
28
+ - init.rb
29
+ - lib/time_left.rb
30
+ - Manifest
31
+ - time_left.gemspec
32
+ has_rdoc: true
33
+ homepage: ""
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --line-numbers
39
+ - --inline-source
40
+ - --title
41
+ - Time_left
42
+ - --main
43
+ - README.rdoc
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "1.2"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project: time_left
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Print out time that left doing some process
65
+ test_files: []
66
+