quarter_time 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
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 evizitei
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,79 @@
1
+ = quarter_time
2
+
3
+ A simple gem for dealing with quarter logic. I happen to have a project where half the models in the database recur every three months as part of a "quarter" of the year. Within the code, we constantly are asking "what quarter is this for?", or "show me all the records for this quarter". Well, now I need the same power on another application, so say hello to "quarter_time".
4
+
5
+ (etymology: it's colloquial in some musical circles to use the phrase "quarter time" to represent the 4/4 time signature, or "three-quarter time" to mean 3/4. Also, it's a short name for a gem that no one has used yet, I have to admit that did factor into the equation)
6
+
7
+ == Usage
8
+
9
+ 1) Install! (or require with whatever dependancy manager you use)
10
+
11
+ sudo gem install quarter_time
12
+
13
+ 2) Require!
14
+
15
+ require 'quarter_time'
16
+
17
+ 3) Use!
18
+
19
+ * find out what quarter a day belongs to:
20
+
21
+ Date.parse("3/15/2010").quarter
22
+ # => 1
23
+ Date.parse("12/12/2010").quarter
24
+ # => 4
25
+
26
+ * Use an object to represent quarters
27
+
28
+ >> quarter = Quarter.new(2010,3)
29
+ => #<Quarter: @quarter=3, @year=2010>
30
+
31
+ *calculate the next or previous quarter in the logical sequence
32
+
33
+ >> quarter.next
34
+ => #<Quarter: @quarter=4, @year=2010>
35
+ >> quarter.next.next
36
+ => #<Quarter: @quarter=1, @year=2011>
37
+ >> quarter.previous
38
+ => #<Quarter: @quarter=2, @year=2010>
39
+
40
+ *get the boundary dates of any given quarter
41
+
42
+ >> quarter.start_date.to_s
43
+ => "2010-07-01"
44
+ >> quarter.end_date.to_s
45
+ => "2010-09-30"
46
+
47
+ *get a string representation of the quarter, good for interpolating into emails and such
48
+
49
+ >> quarter.quarter_stamp
50
+ => "Q3, 2010"
51
+
52
+ *include a scope for easy finding, plus several quarter methods into your ActiveRecord models that have year and quarter fields!
53
+
54
+ class SomeModel < ActiveRecord::Base
55
+ include QuarterTime::QuarterDriven
56
+ end
57
+
58
+ q = Quarter.new(2010,2)
59
+ model = SomeModel.for_quarter(q).first
60
+ model.quarter_obj
61
+ => #<Quarter: @quarter=2, @year=2010>
62
+ model.start_date.to_s
63
+ => "2010-03-01"
64
+ model.quarter_stamp
65
+ => "Q2, 2010"
66
+
67
+ == Note on Patches/Pull Requests
68
+
69
+ * Fork the project.
70
+ * Make your feature addition or bug fix.
71
+ * Add tests for it. This is important so I don't break it in a
72
+ future version unintentionally.
73
+ * Commit, do not mess with rakefile, version, or history.
74
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
75
+ * Send me a pull request. Bonus points for topic branches.
76
+
77
+ == Copyright
78
+
79
+ Copyright (c) 2010 evizitei. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "quarter_time"
8
+ gem.summary = %Q{ library for measuring time in quarters (three month periods) and interacting with models that are tied to a specific quarter.}
9
+ gem.description = %Q{A simple gem for dealing with quarter logic. I happen to have a project where half the models in the database recur every three months as part of a "quarter" of the year. Within the code, we constantly are asking "what quarter is this for?", or "show me all the records for this quarter". Well, now I need the same power on another application, so say hello to "quarter_time".}
10
+ gem.email = "ethan.vizitei@gmail.com"
11
+ gem.homepage = "http://github.com/evizitei/quarter_time"
12
+ gem.authors = ["evizitei"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
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 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "quarter_time #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.3
@@ -0,0 +1,3 @@
1
+ class Date
2
+ include QuarterTime::QuarterKnowledge
3
+ end
@@ -0,0 +1,3 @@
1
+ class Time
2
+ include QuarterTime::QuarterKnowledge
3
+ end
@@ -0,0 +1,64 @@
1
+ class Quarter
2
+ attr_accessor :year
3
+ attr_accessor :quarter
4
+
5
+ def initialize(year,quarter)
6
+ @year = year
7
+ @quarter = quarter
8
+ end
9
+
10
+ def next
11
+ @next ||= build_next
12
+ end
13
+
14
+ def previous
15
+ @previous ||= build_previous
16
+ end
17
+
18
+ def start_date
19
+ Date.parse("#{(@quarter * 3) - 2}/01/#{@year}")
20
+ end
21
+
22
+ def end_date
23
+ start_date + 3.months - 1.day
24
+ end
25
+
26
+ def yq_hash
27
+ {:year=>self.year,:quarter=>self.quarter}
28
+ end
29
+
30
+ def date_hash
31
+ {:start_date=>self.start_date,:end_date=>self.end_date}
32
+ end
33
+
34
+ def date_strings_hash
35
+ {:start_date=>self.start_date.strftime("%m/%d/%Y"),:end_date=>self.end_date.strftime("%m/%d/%Y")}
36
+ end
37
+
38
+ def quarter_stamp
39
+ "Q#{self.quarter}, #{self.year}"
40
+ end
41
+
42
+ def eql?(other_quarter)
43
+ other_quarter.class.eql?(self.class) and other_quarter.year == year and other_quarter.quarter == self.quarter
44
+ end
45
+
46
+ alias_method :==,:eql?
47
+
48
+ protected
49
+ def build_next
50
+ if @quarter < 4
51
+ Quarter.new(@year,@quarter + 1)
52
+ else
53
+ Quarter.new(@year + 1,1)
54
+ end
55
+ end
56
+
57
+ def build_previous
58
+ if @quarter > 1
59
+ Quarter.new(@year,@quarter - 1)
60
+ else
61
+ Quarter.new(@year - 1,4)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,17 @@
1
+ module QuarterTime
2
+ module QuarterDriven
3
+ def self.included(base)
4
+ base.class_eval do
5
+ named_scope :for_quarter, lambda{|quarter_obj|{:conditions=>{:year=>quarter_obj.year, :quarter=>quarter_obj.quarter}}}
6
+
7
+ [:start_date,:end_date,:quarter_stamp].each do |delegated_method|
8
+ define_method(delegated_method){ self.quarter_obj.send(delegated_method) }
9
+ end
10
+ end
11
+ end
12
+ #
13
+ def quarter_obj
14
+ @quarter_obj ||= Quarter.new(self.year,self.quarter)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module QuarterTime
2
+ module QuarterKnowledge
3
+ def quarter
4
+ month_number = self.month
5
+ while((month_number % 3) != 0) do
6
+ month_number += 1
7
+ end
8
+ quarter = month_number/3
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ if RUBY_VERSION >= '1.9'
2
+ require 'time'
3
+ require 'date'
4
+ require 'active_support/time'
5
+ else
6
+ require 'active_support'
7
+ require 'active_support/core_ext'
8
+ end
9
+
10
+ require 'quarter_time/quarter_knowledge'
11
+ require 'extensions/date_extension'
12
+ require 'extensions/time_extension'
13
+ require 'quarter_time/quarter'
14
+ require 'quarter_time/quarter_driven'
@@ -0,0 +1,65 @@
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{quarter_time}
8
+ s.version = "0.2.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["evizitei"]
12
+ s.date = %q{2010-06-09}
13
+ s.description = %q{A simple gem for dealing with quarter logic. I happen to have a project where half the models in the database recur every three months as part of a "quarter" of the year. Within the code, we constantly are asking "what quarter is this for?", or "show me all the records for this quarter". Well, now I need the same power on another application, so say hello to "quarter_time".}
14
+ s.email = %q{ethan.vizitei@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
+ "lib/extensions/date_extension.rb",
27
+ "lib/extensions/time_extension.rb",
28
+ "lib/quarter_time.rb",
29
+ "lib/quarter_time/quarter.rb",
30
+ "lib/quarter_time/quarter_driven.rb",
31
+ "lib/quarter_time/quarter_knowledge.rb",
32
+ "quarter_time.gemspec",
33
+ "test/helper.rb",
34
+ "test/test_date_extensions.rb",
35
+ "test/test_quarter.rb",
36
+ "test/test_quarter_driven.rb",
37
+ "test/test_time_extensions.rb"
38
+ ]
39
+ s.homepage = %q{http://github.com/evizitei/quarter_time}
40
+ s.rdoc_options = ["--charset=UTF-8"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.3.7}
43
+ s.summary = %q{library for measuring time in quarters (three month periods) and interacting with models that are tied to a specific quarter.}
44
+ s.test_files = [
45
+ "test/helper.rb",
46
+ "test/test_date_extensions.rb",
47
+ "test/test_quarter.rb",
48
+ "test/test_quarter_driven.rb",
49
+ "test/test_time_extensions.rb"
50
+ ]
51
+
52
+ if s.respond_to? :specification_version then
53
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
57
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
58
+ else
59
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
63
+ end
64
+ end
65
+
data/test/helper.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+
3
+ if RUBY_VERSION >= '1.9'
4
+ require 'time'
5
+ require 'date'
6
+ require 'active_support/time'
7
+ else
8
+ require 'active_support'
9
+ require 'active_support/core_ext'
10
+ end
11
+
12
+ require 'test/unit'
13
+ require 'shoulda'
14
+
15
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+ require 'quarter_time'
18
+
19
+ class Test::Unit::TestCase
20
+ end
@@ -0,0 +1,14 @@
1
+ require 'helper'
2
+
3
+ class TestDateExtension < Test::Unit::TestCase
4
+ context "Date" do
5
+ should "know what quarter it is in March" do
6
+ assert_equal 1,Date.parse("03/15/2010").quarter
7
+ end
8
+
9
+ should "know what quarter it is in November" do
10
+ assert_equal 4,Date.parse("11/15/2010").quarter
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,61 @@
1
+ require 'helper'
2
+
3
+ class TestQuarter < Test::Unit::TestCase
4
+ context "Quarter" do
5
+ should "calculate start date for first quarter" do
6
+ assert_equal Date.parse("01/01/2009"),Quarter.new(2009,1).start_date
7
+ end
8
+
9
+ should "calculate start date for second quarter" do
10
+ assert_equal Date.parse("04/01/2009"),Quarter.new(2009,2).start_date
11
+ end
12
+
13
+ should "calculate start date for third quarter" do
14
+ assert_equal Date.parse("07/01/2009"),Quarter.new(2009,3).start_date
15
+ end
16
+
17
+ should "calculate start date for fourth quarter" do
18
+ assert_equal Date.parse("10/01/2009"),Quarter.new(2009,4).start_date
19
+ end
20
+
21
+ should "calculate end date for first quarter" do
22
+ assert_equal Date.parse("03/31/2009"),Quarter.new(2009,1).end_date
23
+ end
24
+
25
+ should "calculate end date for second quarter" do
26
+ assert_equal Date.parse("06/30/2009"),Quarter.new(2009,2).end_date
27
+ end
28
+
29
+ should "calculate end date for third quarter" do
30
+ assert_equal Date.parse("09/30/2009"),Quarter.new(2009,3).end_date
31
+ end
32
+
33
+ should "calculate end date for fourth quarter" do
34
+ assert_equal Date.parse("12/31/2009"),Quarter.new(2009,4).end_date
35
+ end
36
+
37
+ should "be equal to other quarter made from same year and quarter" do
38
+ assert Quarter.new(2010,3).eql?(Quarter.new(2010,3))
39
+ end
40
+
41
+ should "be equal to other quarter made from same year and quarter with double equals as well" do
42
+ assert Quarter.new(2010,3) == Quarter.new(2010,3)
43
+ end
44
+ end
45
+
46
+ context "Prebuilt Quarter" do
47
+ setup { @quarter = Quarter.new(2009,4)}
48
+
49
+ should "produce a hash for parameters with year and month" do
50
+ assert_equal({:year=>2009,:quarter=>4},@quarter.yq_hash)
51
+ end
52
+
53
+ should "produce a hash for parameters with start_date and end_date" do
54
+ assert_equal({:start_date=>Date.parse("10/01/2009"),:end_date=>Date.parse("12/31/2009")},@quarter.date_hash)
55
+ end
56
+
57
+ should "produce a params hash with strings for dates" do
58
+ assert_equal({:start_date=>"10/01/2009",:end_date=>"12/31/2009"},@quarter.date_strings_hash)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,51 @@
1
+ require 'helper'
2
+
3
+ class TestQuarterDriven < Test::Unit::TestCase
4
+ context "When Included in a class" do
5
+ setup do
6
+ @model = QuarterDrivenModel.new
7
+ end
8
+
9
+ should "produce quarter object from year and quarter attributes" do
10
+ assert_equal Quarter.new(2010,2),@model.quarter_obj
11
+ end
12
+
13
+ should "delegate start date to quarter object" do
14
+ assert_equal Date.parse("4/1/2010"),@model.start_date
15
+ end
16
+
17
+ should "delegate end date to quarter object" do
18
+ assert_equal Date.parse("6/30/2010"),@model.end_date
19
+ end
20
+
21
+ should "delegate quarter stamp to quarter object" do
22
+ assert_equal "Q2, 2010",@model.quarter_stamp
23
+ end
24
+
25
+ should "create a named scope to find by a quarter object" do
26
+ expected_map = {:conditions=>{:year=>2010,:quarter=>4}}
27
+ assert_equal expected_map,QuarterDrivenModel.scopes[:for_quarter].call(Quarter.new(2010,4))
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ class QuarterDrivenModel
34
+ attr_accessor :year,:quarter
35
+
36
+ def self.scopes
37
+ @@scopes
38
+ end
39
+
40
+ def self.named_scope(name,func = nil)
41
+ @@scopes ||= {}
42
+ @@scopes[name] = func
43
+ end
44
+
45
+ def initialize(year = 2010,quarter = 2)
46
+ @year = year
47
+ @quarter = quarter
48
+ end
49
+
50
+ include QuarterTime::QuarterDriven
51
+ end
@@ -0,0 +1,14 @@
1
+ require 'helper'
2
+
3
+ class TestTimeExtension < Test::Unit::TestCase
4
+ context "Time" do
5
+ should "know what quarter it is in March" do
6
+ assert_equal 1,Time.parse("03/15/2010 9:30 AM").quarter
7
+ end
8
+
9
+ should "know what quarter it is in November" do
10
+ assert_equal 4,Time.parse("11/15/2010 4:45 PM").quarter
11
+ end
12
+ end
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quarter_time
3
+ version: !ruby/object:Gem::Version
4
+ hash: 17
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 3
10
+ version: 0.2.3
11
+ platform: ruby
12
+ authors:
13
+ - evizitei
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-09 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: A simple gem for dealing with quarter logic. I happen to have a project where half the models in the database recur every three months as part of a "quarter" of the year. Within the code, we constantly are asking "what quarter is this for?", or "show me all the records for this quarter". Well, now I need the same power on another application, so say hello to "quarter_time".
36
+ email: ethan.vizitei@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/extensions/date_extension.rb
52
+ - lib/extensions/time_extension.rb
53
+ - lib/quarter_time.rb
54
+ - lib/quarter_time/quarter.rb
55
+ - lib/quarter_time/quarter_driven.rb
56
+ - lib/quarter_time/quarter_knowledge.rb
57
+ - quarter_time.gemspec
58
+ - test/helper.rb
59
+ - test/test_date_extensions.rb
60
+ - test/test_quarter.rb
61
+ - test/test_quarter_driven.rb
62
+ - test/test_time_extensions.rb
63
+ has_rdoc: true
64
+ homepage: http://github.com/evizitei/quarter_time
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --charset=UTF-8
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.3.7
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: library for measuring time in quarters (three month periods) and interacting with models that are tied to a specific quarter.
97
+ test_files:
98
+ - test/helper.rb
99
+ - test/test_date_extensions.rb
100
+ - test/test_quarter.rb
101
+ - test/test_quarter_driven.rb
102
+ - test/test_time_extensions.rb