time_recurrence 0.0.1
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/Gemfile +3 -0
- data/Gemfile.lock +26 -0
- data/Rakefile +5 -0
- data/lib/time_recurrence.rb +4 -0
- data/lib/time_recurrence/namespace.rb +47 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/time_recurrence_spec.rb +92 -0
- data/time_recurrence.gemspec +21 -0
- metadata +87 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
time_recurrence (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
rake (0.9.2.2)
|
11
|
+
rspec (2.11.0)
|
12
|
+
rspec-core (~> 2.11.0)
|
13
|
+
rspec-expectations (~> 2.11.0)
|
14
|
+
rspec-mocks (~> 2.11.0)
|
15
|
+
rspec-core (2.11.0)
|
16
|
+
rspec-expectations (2.11.1)
|
17
|
+
diff-lcs (~> 1.1.3)
|
18
|
+
rspec-mocks (2.11.1)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
rake (~> 0.9)
|
25
|
+
rspec (~> 2.11)
|
26
|
+
time_recurrence!
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'enumerator'
|
2
|
+
|
3
|
+
module Code5
|
4
|
+
class TimeRecurrence
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
def initialize(options)
|
8
|
+
raise ArgumentError, ":starts option is required" unless options.key?(:starts)
|
9
|
+
raise ArgumentError, "invalid :starts option" unless options[:starts].is_a? Time
|
10
|
+
raise ArgumentError, ":interval option is required" unless options.key?(:interval)
|
11
|
+
raise ArgumentError, "invalid :interval option" if options[:interval] == 0
|
12
|
+
raise ArgumentError, ":repeat option is required" unless options.key?(:repeat)
|
13
|
+
raise ArgumentError, "invalid :repeat option" if options[:repeat] == 0
|
14
|
+
|
15
|
+
options[:overlap_day] = true unless options.key?(:overlap_day)
|
16
|
+
@options = options
|
17
|
+
@starts = options.delete(:starts)
|
18
|
+
@interval = options.delete(:interval)
|
19
|
+
@repeat = options.delete(:repeat) - 1
|
20
|
+
end
|
21
|
+
|
22
|
+
def each(&block)
|
23
|
+
events.each(&block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def events
|
27
|
+
@events ||= build_events
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def build_events
|
33
|
+
events = [@starts]
|
34
|
+
time = @starts
|
35
|
+
|
36
|
+
@repeat.times do
|
37
|
+
time = time + @interval
|
38
|
+
|
39
|
+
break if time.day != @starts.day && !@options[:overlap_day]
|
40
|
+
|
41
|
+
events << time
|
42
|
+
end
|
43
|
+
|
44
|
+
events
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TimeRecurrence do
|
4
|
+
context "initialize" do
|
5
|
+
it "should require :starts option" do
|
6
|
+
expect {
|
7
|
+
described_class.new({})
|
8
|
+
}.to raise_error(ArgumentError, ":starts option is required")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should require valid :starts option" do
|
12
|
+
expect {
|
13
|
+
described_class.new(:starts => "invalid")
|
14
|
+
}.to raise_error(ArgumentError, "invalid :starts option")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should require :interval option" do
|
18
|
+
expect {
|
19
|
+
described_class.new(:starts => Time.new(2012, 9, 6, 10, 0, 0))
|
20
|
+
}.to raise_error(ArgumentError, ":interval option is required")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should require :interval to be greater than zero when provided" do
|
24
|
+
expect {
|
25
|
+
described_class.new(:starts => Time.new(2012, 9, 6, 10, 0, 0), :interval => 0)
|
26
|
+
}.to raise_error(ArgumentError, "invalid :interval option")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should require :repeat option" do
|
30
|
+
expect {
|
31
|
+
described_class.new(:starts => Time.new(2012, 9, 6, 10, 0, 0), :interval => 60)
|
32
|
+
}.to raise_error(ArgumentError, ":repeat option is required")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should require :repeat to be greater than zero when provided" do
|
36
|
+
expect {
|
37
|
+
described_class.new(:starts => Time.new(2012, 9, 6, 10, 0, 0), :interval => 60, :repeat => 0)
|
38
|
+
}.to raise_error(ArgumentError, "invalid :repeat option")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "#each" do
|
43
|
+
it "should return an enumerator" do
|
44
|
+
described_class.new(:starts => Time.new(2012, 9, 6, 10, 0, 0), :interval => 60, :repeat => 5).each.should be_instance_of(Enumerator)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "overlaping day" do
|
49
|
+
it "repeating 5 times with 10 minutes as interval" do
|
50
|
+
time_recurrence = described_class.new(:starts => Time.new(2012, 9, 6, 23, 30, 0), :interval => 600, :repeat => 5)
|
51
|
+
time_recurrence.events[0].should eq Time.new(2012, 9, 6, 23, 30, 0)
|
52
|
+
time_recurrence.events[1].should eq Time.new(2012, 9, 6, 23, 40, 0)
|
53
|
+
time_recurrence.events[2].should eq Time.new(2012, 9, 6, 23, 50, 0)
|
54
|
+
time_recurrence.events[3].should eq Time.new(2012, 9, 7, 0, 0, 0)
|
55
|
+
time_recurrence.events[4].should eq Time.new(2012, 9, 7, 0, 10, 0)
|
56
|
+
|
57
|
+
time_recurrence.events.should have(5).items
|
58
|
+
end
|
59
|
+
|
60
|
+
it "repeating 5 times with 20 minutes as interval" do
|
61
|
+
time_recurrence = described_class.new(:starts => Time.new(2012, 9, 6, 23, 30, 0), :interval => 1200, :repeat => 5)
|
62
|
+
time_recurrence.events[0].should eq Time.new(2012, 9, 6, 23, 30, 0)
|
63
|
+
time_recurrence.events[1].should eq Time.new(2012, 9, 6, 23, 50, 0)
|
64
|
+
time_recurrence.events[2].should eq Time.new(2012, 9, 7, 0, 10, 0)
|
65
|
+
time_recurrence.events[3].should eq Time.new(2012, 9, 7, 0, 30, 0)
|
66
|
+
time_recurrence.events[4].should eq Time.new(2012, 9, 7, 0, 50, 0)
|
67
|
+
|
68
|
+
time_recurrence.events.should have(5).items
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "not overlaping day" do
|
73
|
+
it "repeating 5 times with 10 minutes as interval" do
|
74
|
+
time_recurrence = described_class.new(:starts => Time.new(2012, 9, 6, 10, 0, 0), :interval => 600, :repeat => 5, :overlap_day => false)
|
75
|
+
time_recurrence.events[0].should eq Time.new(2012, 9, 6, 10, 0, 0)
|
76
|
+
time_recurrence.events[1].should eq Time.new(2012, 9, 6, 10, 10, 0)
|
77
|
+
time_recurrence.events[2].should eq Time.new(2012, 9, 6, 10, 20, 0)
|
78
|
+
time_recurrence.events[3].should eq Time.new(2012, 9, 6, 10, 30, 0)
|
79
|
+
time_recurrence.events[4].should eq Time.new(2012, 9, 6, 10, 40, 0)
|
80
|
+
|
81
|
+
time_recurrence.events.should have(5).items
|
82
|
+
end
|
83
|
+
|
84
|
+
it "repeating 5 times with 20 minutes as interval" do
|
85
|
+
time_recurrence = described_class.new(:starts => Time.new(2012, 9, 6, 23, 30, 0), :interval => 1200, :repeat => 5, :overlap_day => false)
|
86
|
+
time_recurrence.events[0].should eq Time.new(2012, 9, 6, 23, 30, 0)
|
87
|
+
time_recurrence.events[1].should eq Time.new(2012, 9, 6, 23, 50, 0)
|
88
|
+
|
89
|
+
time_recurrence.events.should have(2).items
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "time_recurrence"
|
6
|
+
s.version = "0.0.1"
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Ricardo Henrique"]
|
9
|
+
s.email = ["ricardohsd@gmail.com"]
|
10
|
+
s.homepage = "http://rubygems.org/gems/time_recurrence"
|
11
|
+
s.summary = "A small library to handle time recurring events"
|
12
|
+
s.description = s.summary
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_development_dependency "rspec", "~> 2.11"
|
20
|
+
s.add_development_dependency "rake", "~> 0.9"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_recurrence
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ricardo Henrique
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.11'
|
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: '2.11'
|
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.9'
|
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.9'
|
46
|
+
description: A small library to handle time recurring events
|
47
|
+
email:
|
48
|
+
- ricardohsd@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- Gemfile
|
54
|
+
- Gemfile.lock
|
55
|
+
- Rakefile
|
56
|
+
- lib/time_recurrence.rb
|
57
|
+
- lib/time_recurrence/namespace.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
- spec/time_recurrence_spec.rb
|
60
|
+
- time_recurrence.gemspec
|
61
|
+
homepage: http://rubygems.org/gems/time_recurrence
|
62
|
+
licenses: []
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.8.24
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: A small library to handle time recurring events
|
85
|
+
test_files:
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
- spec/time_recurrence_spec.rb
|