business_days 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Burke Libbey, Thorkelson Consulting Ltd.
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.
@@ -0,0 +1,31 @@
1
+ = business_days
2
+
3
+ Business Days is a sort-of extension to ActiveSupport to allow adding business days to time objects.
4
+
5
+ == Installation
6
+
7
+ Add a config.gem line (rails 2.3)
8
+
9
+ config.gem 'business_days'
10
+
11
+ Or add it to your Gemfile.
12
+
13
+ gem 'business_days'
14
+
15
+ == Examples
16
+
17
+ (Time.parse("Friday") + 1.business_day).strftime("%A")
18
+ # => "Monday"
19
+
20
+ (Time.parse("Friday") + 2.business_days).strftime("%A")
21
+ # => "Tuesday"
22
+
23
+ (Time.parse("Saturday") + 1.business_day).strftime("%A")
24
+ # => "Tuesday"
25
+
26
+ (Time.parse("Saturday") + 0.business_days).strftime("%A")
27
+ # => "Monday"
28
+
29
+ (Time.parse("Friday") - 1.business_day).strftime("%A")
30
+ # => "Thursday"
31
+
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = 'business_days'
3
+ gem.version = "0.0.1"
4
+
5
+ gem.author, gem.email = 'Burke Libbey', "burke@burkelibbey.org"
6
+ gem.homepage = "http://github.com/burke/business_days"
7
+
8
+ gem.summary = "Write me."
9
+ gem.description = "Write me..."
10
+
11
+ gem.required_ruby_version = '>= 1.8.7'
12
+
13
+ gem.add_dependency "activesupport"
14
+
15
+ gem.has_rdoc = false
16
+
17
+ gem.files = Dir.glob("{lib,spec}/**/*") + %w(LICENSE README.rdoc Rakefile business_days.gemspec)
18
+
19
+ end
20
+
21
+
@@ -0,0 +1,36 @@
1
+ require 'active_support/all'
2
+ require File.join(File.dirname(__FILE__),'business_days','time')
3
+
4
+ module BusinessDays
5
+
6
+ class BusinessDayDuration
7
+ def initialize(days)
8
+ @days = days
9
+ end
10
+
11
+ def calculate_duration(time, operation)
12
+ business_days = time.is_business_day? ? 1 : 0
13
+ while business_days <= @days
14
+ if (time = time.send(operation, 1.day)).is_business_day?
15
+ business_days += 1
16
+ end
17
+ end
18
+ time
19
+ end
20
+
21
+ def +(time)
22
+ calculate_duration(time, :+)
23
+ end
24
+
25
+ end
26
+
27
+ def business_days
28
+ BusinessDayDuration.new(self)
29
+ end
30
+ alias :business_day :business_days
31
+
32
+ end
33
+
34
+ class Numeric
35
+ include BusinessDays
36
+ end
@@ -0,0 +1,42 @@
1
+ module BusinessDays
2
+ module Time
3
+ module Calculations
4
+ def self.included(base) #:nodoc:
5
+
6
+ base.class_eval do
7
+ alias_method :plus_without_business_days, :+
8
+ alias_method :+, :plus_with_business_days
9
+
10
+ alias_method :minus_without_business_days, :-
11
+ alias_method :-, :minus_with_business_days
12
+ end
13
+ end
14
+
15
+ def is_business_day?
16
+ # TODO: Holidays.
17
+ ! [0,6].include?(self.wday)
18
+ end
19
+
20
+ def plus_with_business_days(other)
21
+ if BusinessDays::BusinessDayDuration === other
22
+ other.calculate_duration(self, :+)
23
+ else
24
+ plus_without_business_days(other)
25
+ end
26
+ end
27
+
28
+ def minus_with_business_days(other)
29
+ if BusinessDays::BusinessDayDuration === other
30
+ other.calculate_duration(self, :-)
31
+ else
32
+ minus_without_business_days(other)
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+
40
+ class Time
41
+ include BusinessDays::Time::Calculations
42
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require File.join(File.dirname(__FILE__),'..','lib','business_days')
3
+
4
+ describe BusinessDays do
5
+
6
+ before do
7
+ @friday = Time.parse("Friday, September 10th, 2010")
8
+ @thursday = @friday - 1.day
9
+ @saturday = @friday + 1.day
10
+ @monday = @friday + 3.days
11
+ @tuesday = @friday + 4.days
12
+ end
13
+
14
+ it "should add properly" do
15
+ (@friday + 1.business_day).should == @monday
16
+ (@friday + 2.business_days).should == @tuesday
17
+ (@saturday + 1.business_days).should == @tuesday
18
+ (@saturday + 0.business_days).should == @monday
19
+ end
20
+
21
+ it "should subtract properly" do
22
+ (@saturday - 0.business_days).should == @friday
23
+ (@saturday - 1.business_day).should == @thursday
24
+ (@monday - 1.business_days).should == @friday
25
+ (@monday - 0.business_days).should == @monday
26
+ end
27
+
28
+ it "should add with the params reversed" do
29
+ (1.business_day + @friday).should == @monday
30
+ end
31
+
32
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+
3
+ # require something.
4
+
5
+ gem "rspec", ">=1.2.5"
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
10
+
11
+
@@ -0,0 +1 @@
1
+ --colour
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: business_days
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Burke Libbey
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-10 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
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: :runtime
34
+ version_requirements: *id001
35
+ description: Write me...
36
+ email: burke@burkelibbey.org
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib/business_days/time.rb
45
+ - lib/business_days.rb
46
+ - spec/business_days_spec.rb
47
+ - spec/spec_helper.rb
48
+ - spec/spec_opts.rb
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - business_days.gemspec
53
+ has_rdoc: false
54
+ homepage: http://github.com/burke/business_days
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 57
68
+ segments:
69
+ - 1
70
+ - 8
71
+ - 7
72
+ version: 1.8.7
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.3.7
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Write me.
89
+ test_files: []
90
+