time-iterator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Guardfile +6 -0
- data/HISTORY.md +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +9 -0
- data/lib/time-iterator.rb +2 -0
- data/lib/time_iterator.rb +54 -0
- data/lib/time_iterator/core_ext.rb +18 -0
- data/lib/time_iterator/version.rb +3 -0
- data/spec/lib/time_iterator/core_ext_spec.rb +21 -0
- data/spec/lib/time_iterator_spec.rb +23 -0
- data/spec/spec_helper.rb +14 -0
- data/time-iterator.gemspec +24 -0
- metadata +116 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require ./lib/time-iterator --color --format documentation
|
data/Gemfile
ADDED
data/Guardfile
ADDED
data/HISTORY.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 vidIQ, inc.
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# TimeIterator
|
2
|
+
|
3
|
+
Utility class to iterate through times with a given step size.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'time-iterator'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install time-iterator
|
19
|
+
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
require "time-iterator"
|
25
|
+
|
26
|
+
Time.utc(1970, 1, 1).up_to(Time.now).step(24 * 60 * 60).count
|
27
|
+
# => 15842
|
28
|
+
```
|
29
|
+
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require "time_iterator/version"
|
2
|
+
|
3
|
+
|
4
|
+
# Wraper class that allows easily iterate through the times
|
5
|
+
# with given step size (1 second by default).
|
6
|
+
class TimeIterator
|
7
|
+
|
8
|
+
include Enumerable
|
9
|
+
|
10
|
+
|
11
|
+
# @param [Time] start
|
12
|
+
# @param [Time] stop
|
13
|
+
# @param [#to_f] step
|
14
|
+
def initialize start, stop, step = 1
|
15
|
+
@start, @stop, @step = start, stop, step.to_f
|
16
|
+
|
17
|
+
raise "Iteration step should be higher than zero." if 0.00 > @step
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
# Returns new iterator with same start/stop values but new step size.
|
22
|
+
#
|
23
|
+
# @param [#to_f] step
|
24
|
+
# @return [TimeIterator]
|
25
|
+
def step step
|
26
|
+
self.class.new @start, @stop, step
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
# Iterates through time range.
|
31
|
+
#
|
32
|
+
# @yield [time]
|
33
|
+
# @return [TimeIterator]
|
34
|
+
def each &block
|
35
|
+
if @start < @stop
|
36
|
+
curr = @start + 0
|
37
|
+
|
38
|
+
while curr <= @stop
|
39
|
+
yield curr
|
40
|
+
curr += @step
|
41
|
+
end
|
42
|
+
else
|
43
|
+
curr = @stop + 0
|
44
|
+
|
45
|
+
while curr >= @start
|
46
|
+
yield curr
|
47
|
+
curr -= @step
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "time_iterator"
|
2
|
+
|
3
|
+
class Time
|
4
|
+
# Create new time iterator from `self` till specified `time`.
|
5
|
+
# Syntax sugar for (where `self` is `start`):
|
6
|
+
#
|
7
|
+
# start = Time.new 1970, 1, 1
|
8
|
+
# stop = Time.new 1970, 2, 1
|
9
|
+
#
|
10
|
+
# TimeIterator.new start, stop
|
11
|
+
#
|
12
|
+
# @return [TimeIterator]
|
13
|
+
def up_to time
|
14
|
+
TimeIterator.new self, time
|
15
|
+
end
|
16
|
+
|
17
|
+
alias :down_to :up_to
|
18
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
|
4
|
+
describe Time do
|
5
|
+
|
6
|
+
it { should respond_to :up_to }
|
7
|
+
it { should respond_to :down_to }
|
8
|
+
|
9
|
+
context "#up_to" do
|
10
|
+
it "should return TimeIterator instance" do
|
11
|
+
Time.now.up_to(Time.now).should be_instance_of TimeIterator
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "#down_to" do
|
16
|
+
it "should return TimeIterator instance" do
|
17
|
+
Time.now.down_to(Time.now).should be_instance_of TimeIterator
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
|
4
|
+
describe TimeIterator do
|
5
|
+
|
6
|
+
let(:start) { Time.now - 60 }
|
7
|
+
let(:stop) { start + 60 }
|
8
|
+
let(:iter) { TimeIterator.new start, stop, 30 }
|
9
|
+
|
10
|
+
|
11
|
+
it "should include Enumerable" do
|
12
|
+
iter.should be_kind_of Enumerable
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
context "#each" do
|
17
|
+
it "should yield Time steps" do
|
18
|
+
lambda{ |b| iter.each(&b) }
|
19
|
+
.should yield_successive_args(start, start + 30, start + 60)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.expand_path('../lib/time_iterator/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "time-iterator"
|
6
|
+
spec.version = TimeIterator::VERSION
|
7
|
+
spec.homepage = "https://github.com/vid-io/time-iterator"
|
8
|
+
spec.authors = ["Aleksey V Zapparov"]
|
9
|
+
spec.email = ["ixti@member.fsf.org"]
|
10
|
+
spec.license = "MIT"
|
11
|
+
spec.summary = "time-iterator-#{TimeIterator::VERSION}"
|
12
|
+
spec.description = <<-DESC
|
13
|
+
Utility class to iterate through times with a given step size.
|
14
|
+
DESC
|
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
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time-iterator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Aleksey V Zapparov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: ! ' Utility class to iterate through times with a given step size.
|
63
|
+
|
64
|
+
'
|
65
|
+
email:
|
66
|
+
- ixti@member.fsf.org
|
67
|
+
executables: []
|
68
|
+
extensions: []
|
69
|
+
extra_rdoc_files: []
|
70
|
+
files:
|
71
|
+
- .gitignore
|
72
|
+
- .rspec
|
73
|
+
- Gemfile
|
74
|
+
- Guardfile
|
75
|
+
- HISTORY.md
|
76
|
+
- LICENSE.txt
|
77
|
+
- README.md
|
78
|
+
- Rakefile
|
79
|
+
- lib/time-iterator.rb
|
80
|
+
- lib/time_iterator.rb
|
81
|
+
- lib/time_iterator/core_ext.rb
|
82
|
+
- lib/time_iterator/version.rb
|
83
|
+
- spec/lib/time_iterator/core_ext_spec.rb
|
84
|
+
- spec/lib/time_iterator_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- time-iterator.gemspec
|
87
|
+
homepage: https://github.com/vid-io/time-iterator
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 1.8.23
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: time-iterator-0.1.0
|
112
|
+
test_files:
|
113
|
+
- spec/lib/time_iterator/core_ext_spec.rb
|
114
|
+
- spec/lib/time_iterator_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
has_rdoc:
|