funtimes 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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Len Smith
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,12 @@
1
+ = funtimes
2
+
3
+ Library for working with months, quarters and date ranges in Ruby.
4
+
5
+ include DateRange in any class with a start_date and end_date and be able to iterate over months and days.
6
+
7
+ Look in the specs for the, uhm, specs.
8
+
9
+ == Copyright
10
+
11
+ Copyright (c) 2010 Len Smith.
12
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "funtimes"
8
+ gem.summary = %Q{Manage date ranges, months and quarters}
9
+ gem.description = %Q{Manage date ranges, months and quarters}
10
+ gem.email = "ignu.smith@gmail.com"
11
+ gem.homepage = "http://github.com/ignu/fun-times"
12
+ gem.authors = ["Len Smith"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "funtimes #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/funtimes.gemspec ADDED
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{funtimes}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Len Smith"]
12
+ s.date = %q{2010-07-02}
13
+ s.description = %q{Manage date ranges, months and quarters}
14
+ s.email = %q{ignu.smith@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "funtimes.gemspec",
27
+ "lib/date_range.rb",
28
+ "lib/funtimes.rb",
29
+ "lib/month.rb",
30
+ "lib/quarter.rb",
31
+ "spec/date_range_spec.rb",
32
+ "spec/month_spec.rb",
33
+ "spec/quarter_spec.rb",
34
+ "spec/spec.opts",
35
+ "spec/spec_helper.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/ignu/fun-times}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.6}
41
+ s.summary = %q{Manage date ranges, months and quarters}
42
+ s.test_files = [
43
+ "spec/date_range_spec.rb",
44
+ "spec/month_spec.rb",
45
+ "spec/quarter_spec.rb",
46
+ "spec/spec_helper.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
60
+ end
61
+ end
62
+
data/lib/date_range.rb ADDED
@@ -0,0 +1,78 @@
1
+ module DateRange
2
+
3
+ def days
4
+ (_start)..(_end)
5
+ end
6
+
7
+ def months
8
+ return [] unless _start && _end
9
+ (Month.from(_start)..Month.from(_end))
10
+ end
11
+
12
+ def quarters
13
+ return [] unless _start && _end
14
+ (Quarter.from(_start)..Quarter.from(_end))
15
+ end
16
+
17
+ def encompasses(other)
18
+ _start <= other.start_date && _end >= other.end_date
19
+ end
20
+
21
+ def intersects(other)
22
+ other.encompasses(self) || encompasses(other) || overlaps(other)
23
+ end
24
+
25
+ def & (other)
26
+ return [] unless intersects other
27
+ return other if encompasses(other)
28
+ return self if other.encompasses(self)
29
+ return SimpleRange.new(other.start_date, _end) if overlaps_left_side(other)
30
+ SimpleRange.new(_start, other.end_date)
31
+ end
32
+
33
+ def same_range_as(other)
34
+ other.start_date == _start && other.end_date == _end
35
+ end
36
+
37
+ def number_of_days_in(other)
38
+ intersection = (self & other)
39
+ return 0 if intersection == []
40
+ intersection.days.to_a.length
41
+ end
42
+
43
+ private
44
+
45
+ def overlaps(other)
46
+ overlaps_left_side(other) || overlaps_right_side(other)
47
+ end
48
+
49
+ def overlaps_right_side(other)
50
+ other.start_date <= _start && other.end_date >= _start
51
+ end
52
+
53
+ def overlaps_left_side(other)
54
+ other.start_date <= _end && other.end_date >= _end
55
+ end
56
+
57
+ def _start
58
+ @start_date ||= start_date
59
+ return @start_date.to_datetime if @start_date.respond_to? :to_datetime
60
+ @start_date
61
+ end
62
+
63
+ def _end
64
+ @end_date ||= end_date
65
+ return @end_date.to_datetime if @end_date.respond_to? :to_datetime
66
+ @end_date
67
+ end
68
+ end
69
+
70
+ class SimpleRange
71
+ include DateRange
72
+
73
+ attr_accessor :start_date, :end_date
74
+
75
+ def initialize(date1, date2)
76
+ @start_date, @end_date = date1, date2
77
+ end
78
+ end
data/lib/funtimes.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'month'
2
+ require 'date_range'
3
+ require 'quarter'
data/lib/month.rb ADDED
@@ -0,0 +1,59 @@
1
+ require 'date'
2
+ require 'date_range'
3
+ class Month
4
+ include Comparable
5
+ include DateRange
6
+ attr_accessor :year, :number, :start_date, :end_date
7
+
8
+ def self.from(date)
9
+ Month.new(date.month, date.year)
10
+ end
11
+
12
+ def initialize(month, year)
13
+ @number, @year = month, year
14
+ @start_date = DateTime.new(@year, @number, 1)
15
+ @end_date = DateTime.new(next_months_start.year, next_months_start.month, 1) - 1
16
+ end
17
+
18
+ def to_s
19
+ "#{name} #{@year}"
20
+ end
21
+
22
+ def name
23
+ @start_date.strftime("%B")
24
+ end
25
+
26
+ def <=> (other)
27
+ return @year <=> other.year unless @year == other.year
28
+ @number <=> other.number
29
+ end
30
+
31
+ def succ
32
+ Month.from next_months_start
33
+ end
34
+
35
+ def prev
36
+ return Month.new(12, @year-1) if (@number == 1)
37
+ Month.new @number-1, @year
38
+ end
39
+
40
+ def + (amount)
41
+ rv = self
42
+ amount.times {rv = rv.succ}
43
+ rv
44
+ end
45
+
46
+ def -(amount)
47
+ rv = self
48
+ amount.times {rv = rv.prev}
49
+ rv
50
+ end
51
+
52
+ private
53
+
54
+ def next_months_start
55
+ return DateTime.new(@year+1, 1, 1) if (@number == 12)
56
+ DateTime.new(@year, @number+1, 1)
57
+ end
58
+ end
59
+
data/lib/quarter.rb ADDED
@@ -0,0 +1,29 @@
1
+ class Quarter
2
+ include DateRange
3
+ attr_accessor :number, :year, :start_date, :end_date
4
+
5
+ def initialize(q,y)
6
+ months = [1, 4, 7, 10]
7
+ @number, @year = q, y
8
+ @start_date = DateTime.new(y, months[q-1], 1)
9
+ @end_date = DateTime.new(y, months[q], 1) - 1 unless q == 4
10
+ @end_date = DateTime.new(y+1, 1, 1) - 1 if q == 4
11
+ end
12
+
13
+ def self.from(date)
14
+ new(date.month/4+1, date.year)
15
+ end
16
+
17
+ def succ
18
+ return Quarter.new(@number+1, @year) unless @number == 4
19
+ Quarter.new(1, @year+1)
20
+ end
21
+
22
+ def <=> (other)
23
+ @start_date <=> other.start_date
24
+ end
25
+
26
+ def to_s
27
+ "Q#{@number}, #{@year}"
28
+ end
29
+ end
@@ -0,0 +1,78 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "DateRange" do
4
+
5
+ let(:firstOfYear) { DateTime.new(2001,1,1) }
6
+ let(:endOfYear) { DateTime.new(2001,12,31) }
7
+ let(:firstOfMarch) { DateTime.new(2001,3,1) }
8
+ let(:endOfMarch) { DateTime.new(2001,3,30) }
9
+ let(:firstOfJuly) { DateTime.new(2001,7,1) }
10
+ let(:endOfJuly) { DateTime.new(2001,7,31) }
11
+ let(:range) { SimpleRange.new(DateTime.new(2001,1,1), DateTime.new(2001,7,5))}
12
+ let(:year) { SimpleRange.new(firstOfYear, endOfYear)}
13
+ let(:march) { SimpleRange.new(firstOfMarch, endOfMarch)}
14
+ let(:july) { SimpleRange.new(firstOfJuly, endOfJuly)}
15
+
16
+ it "should be equal if start and end dates are the same" do
17
+ year.same_range_as(year).should == true
18
+ end
19
+
20
+ it "should have the quarters in a range" do
21
+ year.quarters.first.number.should == 1
22
+ year.quarters.to_a.length.should == 4
23
+ end
24
+
25
+ it "should count the days in the range" do
26
+ range.days.to_a.length.should == 31+28+31+30+31+30+5
27
+ end
28
+
29
+ it "should be able to get the number of months in a range" do
30
+ range.months.to_a.length.should == 7
31
+ end
32
+
33
+ describe DateRange, "intersecting dates" do
34
+
35
+ it "should tell if one date range encompasses another" do
36
+ year.encompasses(march).should == true
37
+ march.encompasses(year).should == false
38
+ march.encompasses(july).should == false
39
+ end
40
+
41
+ it "should be able to tell if one date range intersects another" do
42
+ year.intersects(march).should == true
43
+ year.intersects(year).should == true
44
+ march.intersects(year).should == true
45
+ march.intersects(july).should == false
46
+ range.intersects(july).should == true
47
+ july.intersects(range).should == true
48
+ end
49
+
50
+ it "can return the intersection" do
51
+ (year & march).should == march
52
+ (march & year).should == march
53
+ (march & march).should == march
54
+
55
+ left = (range & july)
56
+ left.start_date.should == firstOfJuly
57
+ left.end_date.should == range.end_date
58
+ left = (july & range)
59
+ left.start_date.should == firstOfJuly
60
+ left.end_date.should == range.end_date
61
+
62
+ (march & july).should == []
63
+ end
64
+
65
+
66
+ it "can figure out the number of days in" do
67
+ year.number_of_days_in(march).should == 30
68
+ march.number_of_days_in(july).should == 0
69
+ end
70
+
71
+ it "returns null collections when start or end date is not set" do
72
+ r = SimpleRange.new(nil, DateTime.new)
73
+ r.months.length.should == 0
74
+ r = SimpleRange.new(DateTime.new, nil)
75
+ r.months.length.should == 0
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Month" do
4
+ let(:july) {Month.from DateTime.new(1977, 7, 31)}
5
+ let(:sept) {Month.new 9, 1977}
6
+
7
+ it "can initialize the month of a date" do
8
+ july.to_s.should == "July 1977"
9
+ july.name.should == "July"
10
+ july.year.should == 1977
11
+ july.number.should == 7
12
+ july.start_date.should == Date.new(1977, 7, 1)
13
+ july.end_date.should == Date.new(1977, 7, 31)
14
+ end
15
+
16
+ it "can use the less than operator" do
17
+ july.should be < sept
18
+ end
19
+
20
+ it "can iterate over months" do
21
+ range = (july..sept)
22
+ range.first.should == july
23
+ range.to_a.length.should == 3
24
+ range.end.should == sept
25
+ end
26
+
27
+ it "can iterate over months when crossing a year barrier" do
28
+ range = (july..(Month.new(1, 1978)))
29
+ range.to_a.length.should == 7
30
+ end
31
+
32
+ it "can add and subtract months" do
33
+ (july+1).to_s.should == "August 1977"
34
+ (july-1).to_s.should == "June 1977"
35
+ end
36
+
37
+ it "is a daterange" do
38
+ july.days.to_a.length.should == 31
39
+ end
40
+
41
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Quarter" do
4
+
5
+ let (:q1) {Quarter.new(1, 2009)}
6
+ let (:q2) {Quarter.new(2, 2009)}
7
+ let (:q3) {Quarter.new(3, 2009)}
8
+ let (:q4) {Quarter.new(4, 2009)}
9
+
10
+ it "can be newed up" do
11
+ quarter = Quarter.new(1, 2009)
12
+ quarter.number.should == 1
13
+ quarter.year.should == 2009
14
+ end
15
+
16
+ it "can initialize from any date" do
17
+ quarter = Quarter.from(DateTime.new(2009, 2, 1))
18
+ quarter.year.should == 2009
19
+ end
20
+
21
+ it "sets the start date correctly" do
22
+ q1.start_date.should == DateTime.new(2009, 1, 1)
23
+ q2.start_date.should == DateTime.new(2009, 4, 1)
24
+ q3.start_date.should == DateTime.new(2009, 7, 1)
25
+ q4.start_date.should == DateTime.new(2009, 10, 1)
26
+ end
27
+
28
+ it "sets the end date correctly" do
29
+ q1.end_date.should == DateTime.new(2009, 3, 31)
30
+ q2.end_date.should == DateTime.new(2009, 6, 30)
31
+ q3.end_date.should == DateTime.new(2009, 9, 30)
32
+ q4.end_date.should == DateTime.new(2009, 12, 31)
33
+ end
34
+
35
+ it "can iterate over quarters" do
36
+ range = (q1..q3)
37
+ range.first.should == q1
38
+ range.to_a.length.should == 3
39
+ range.end.should == q3
40
+ end
41
+
42
+ it "is a daterange" do
43
+ q1.months.to_a.length.should == 3
44
+ end
45
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'funtimes'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: funtimes
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Len Smith
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-02 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Manage date ranges, months and quarters
35
+ email: ignu.smith@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - funtimes.gemspec
51
+ - lib/date_range.rb
52
+ - lib/funtimes.rb
53
+ - lib/month.rb
54
+ - lib/quarter.rb
55
+ - spec/date_range_spec.rb
56
+ - spec/month_spec.rb
57
+ - spec/quarter_spec.rb
58
+ - spec/spec.opts
59
+ - spec/spec_helper.rb
60
+ has_rdoc: true
61
+ homepage: http://github.com/ignu/fun-times
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.6
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Manage date ranges, months and quarters
90
+ test_files:
91
+ - spec/date_range_spec.rb
92
+ - spec/month_spec.rb
93
+ - spec/quarter_spec.rb
94
+ - spec/spec_helper.rb