time_span 0.0.1.alpha02

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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in time_span.gemspec
4
+ gemspec
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,117 @@
1
+ #require "time_span/version"
2
+ require 'date'
3
+
4
+ module TimeSpan
5
+ class TimeSpan
6
+
7
+ attr_accessor :starts, :ends # RelativeTime objects
8
+
9
+ ##################################
10
+ ## time comparison #
11
+ ##################################
12
+
13
+ def initialize(starting_at, ending_at)
14
+ self.starts = starting_at
15
+ self.ends = ending_at
16
+ starting_at.kind_of?(RelativeTime) && ending_at.kind_of?(RelativeTime)
17
+ end
18
+
19
+
20
+ def starts_before?(b)
21
+ starts < b.starts
22
+ end
23
+
24
+ def starts_after?(b)
25
+ starts > b.starts
26
+ end
27
+
28
+ def starts_on_or_after?(b)
29
+ starts >= b.starts
30
+ end
31
+
32
+
33
+ def starts_with?(b)
34
+ starts == b.starts
35
+ end
36
+
37
+ def starts_before_or_with?(b)
38
+ starts <= b.starts
39
+ end
40
+
41
+ def ends_before?(b)
42
+ ends < b.ends
43
+ end
44
+
45
+ def ends_on_or_before?(b)
46
+ ends <= b.ends
47
+ end
48
+
49
+ def ends_on_or_after?(b)
50
+ ends >= b.ends
51
+ end
52
+
53
+ def ends_after?(b)
54
+ ends > b.ends
55
+ end
56
+
57
+ def ends_with?(b)
58
+ ends == b.ends
59
+ end
60
+
61
+ def ends_before_other_starts?(b)
62
+ ends < b.starts
63
+ end
64
+
65
+ def ends_as_other_starts?(b)
66
+ ends == b.starts
67
+ end
68
+
69
+ def starts_after_other_ends?(b)
70
+ starts > b.ends
71
+ end
72
+
73
+ def starts_as_other_ends?(b)
74
+ starts == b.ends
75
+ end
76
+
77
+ ##################################
78
+ ## span comparison #
79
+ ##################################
80
+
81
+ ## >= and <= intentionally not defined;
82
+
83
+
84
+ def == (b)
85
+ ends_with?(b) && starts_with?(b)
86
+ end
87
+
88
+ def < (b)
89
+ ends_before_other_starts?(b)
90
+ end
91
+
92
+ def > (b)
93
+ starts_after_other_ends?(b)
94
+ end
95
+
96
+ def contained_fully_inside?(b)
97
+ starts_after?(b) && ends_before?(b)
98
+ end
99
+
100
+ def contained_inside?(b)
101
+ starts_on_or_after?(b) && ends_on_or_before?(b)
102
+ end
103
+
104
+ def contains_fully?(b)
105
+ starts_before?(b) && ends_after?(b)
106
+ end
107
+
108
+ def contains?(b)
109
+ starts_before_or_with?(b) && ends_on_or_after?(b)
110
+ end
111
+
112
+ end
113
+
114
+ # first implementation is just as DateTime; later add fuzzy time
115
+ class RelativeTime < ::DateTime
116
+ end
117
+ end
@@ -0,0 +1,3 @@
1
+ module TimeSpan
2
+ VERSION = "0.0.1.alpha02"
3
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ require 'time_span'
6
+
7
+ require 'bundler/setup'
8
+ require 'rspec'
9
+ require 'rspec/autorun'
10
+
11
+ #RSpec.configure do |config|
12
+ #
13
+ #end
14
+
@@ -0,0 +1,156 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '../../spec_helper')
2
+
3
+ describe "TimeSpan" do
4
+
5
+ context "TimeSpan" do
6
+
7
+ context "instance methods" do
8
+
9
+ let (:alpha_start) do
10
+ TimeSpan::RelativeTime.parse("Jan 1, 2007")
11
+ end
12
+
13
+ let (:alpha_end) do
14
+ alpha_start + 2000
15
+ end
16
+
17
+
18
+ let (:alpha) do
19
+ TimeSpan::TimeSpan.new(alpha_start, alpha_end)
20
+ end
21
+
22
+ context "single time point comparators" do
23
+
24
+ context "same ends time, alpha starts before beta" do
25
+
26
+ let (:beta_start) do
27
+ TimeSpan::RelativeTime.parse("Jan 1, 2008")
28
+ end
29
+
30
+ let (:beta) do
31
+ TimeSpan::TimeSpan.new(beta_start, alpha_end)
32
+ end
33
+
34
+ context "alpha" do
35
+
36
+ it "starts before beta" do
37
+ alpha.should be_starts_before(beta)
38
+ end
39
+
40
+
41
+ it "ends with beta" do
42
+ alpha.should be_ends_with(beta)
43
+ end
44
+
45
+ end
46
+
47
+ context "alpha does not" do
48
+
49
+ it "start after beta" do
50
+ alpha.should_not be_starts_after(beta)
51
+ end
52
+
53
+ it "start with beta" do
54
+ alpha.should_not be_starts_with(beta)
55
+ end
56
+
57
+ it "end before beta" do
58
+ alpha.should_not be_ends_before(beta)
59
+ end
60
+
61
+ it "end after beta" do
62
+ alpha.should_not be_ends_after(beta)
63
+ end
64
+
65
+ it "end before beta starts" do
66
+ alpha.should_not be_ends_before_other_starts(beta)
67
+ end
68
+
69
+ it "end as beta starts" do
70
+ alpha.should_not be_ends_as_other_starts(beta)
71
+ end
72
+
73
+ it "start after beta ends" do
74
+ alpha.should_not be_starts_after_other_ends(beta)
75
+ end
76
+
77
+ it "start as beta ends" do
78
+ alpha.should_not be_starts_as_other_ends(beta)
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ end
86
+
87
+ context "time span comparators" do
88
+
89
+ let (:beta_earliest) do
90
+ alpha_start - 1000
91
+ end
92
+
93
+ let (:beta_before_alpha_starts) do
94
+ alpha_start - 500
95
+ end
96
+
97
+ let (:beta_after_alpha_starts) do
98
+ alpha_start + 500
99
+ end
100
+
101
+ let (:beta_before_alpha_ends) do
102
+ alpha_start + 1000
103
+ end
104
+
105
+ let (:beta_right_after_alpha_ends) do
106
+ alpha_end + 500
107
+ end
108
+
109
+ let (:beta_last) do
110
+ alpha_end + 1000
111
+ end
112
+
113
+ it "identical start and end times should return == as true" do
114
+ beta = TimeSpan::TimeSpan.new(alpha_start, alpha_end)
115
+ alpha.should == beta
116
+ end
117
+
118
+ it "alpha ends before beta starts, should return alpha < beta" do
119
+ beta = TimeSpan::TimeSpan.new(beta_right_after_alpha_ends, beta_last)
120
+ alpha.should < beta
121
+ end
122
+
123
+ it "alpha starts after beta ends, should return alpha > beta" do
124
+ beta = TimeSpan::TimeSpan.new(beta_earliest, beta_before_alpha_starts)
125
+ alpha.should > beta
126
+ end
127
+
128
+ it "alpha starting after and ending before beta should be contained inside beta" do
129
+ beta = TimeSpan::TimeSpan.new(beta_earliest, beta_last)
130
+ alpha.should be_contained_fully_inside(beta)
131
+ end
132
+
133
+ it "alpha starting with beta but ending before, is contained inside" do
134
+ beta = TimeSpan::TimeSpan.new(alpha_start, beta_last)
135
+ alpha.should be_contained_inside(beta)
136
+ end
137
+
138
+ it "alpha starting before and ending after full contains beta" do
139
+ beta = TimeSpan::TimeSpan.new(beta_after_alpha_starts, beta_before_alpha_ends)
140
+ alpha.should be_contains_fully(beta)
141
+ end
142
+
143
+ it "alpha starting before but ending with beta should contain beta" do
144
+ beta = TimeSpan::TimeSpan.new(beta_after_alpha_starts, alpha_end)
145
+ alpha.should be_contains(beta)
146
+ end
147
+
148
+ end
149
+
150
+ end
151
+
152
+ end
153
+
154
+
155
+
156
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "time_span/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "time_span"
7
+ s.version = TimeSpan::VERSION
8
+ s.authors = ["Craig A. Cook"]
9
+ s.email = ["craig.a.cook@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Time Span}
12
+ s.description = %q{Time spans, including many comparators}
13
+ s.license = 'MIT'
14
+
15
+ s.rubyforge_project = "time_span"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ # specify any dependencies here; for example:
23
+ s.add_development_dependency "rspec"
24
+ # s.add_runtime_dependency "rest-client"
25
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_span
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha02
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Craig A. Cook
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70298360 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70298360
25
+ description: Time spans, including many comparators
26
+ email:
27
+ - craig.a.cook@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - lib/time_span.rb
36
+ - lib/time_span/version.rb
37
+ - spec/spec_helper.rb
38
+ - spec/time_span/time_span_spec.rb
39
+ - time_span.gemspec
40
+ homepage: ''
41
+ licenses:
42
+ - MIT
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>'
57
+ - !ruby/object:Gem::Version
58
+ version: 1.3.1
59
+ requirements: []
60
+ rubyforge_project: time_span
61
+ rubygems_version: 1.8.12
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Time Span
65
+ test_files: []