business_time 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -70,8 +70,31 @@ I needed this, but taking into account business hours/days and holidays.
70
70
  friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
71
71
  tuesday_morning = 1.business_hour.after(friday_afternoon)
72
72
 
73
+ == Usage in Rails
74
+ The code above should work on a rails console without any issue. You will want to add a line something like:
73
75
 
76
+ config.gem "business_time"
74
77
 
78
+ to your environment.rb file. Or if you're using bundler, add this line to your Gemfile:
79
+
80
+ gem "business_time"
81
+
82
+ This gem also includes a generator so you can bootstrap some stuff in your environment:
83
+
84
+ ./script/generate business_time_config
85
+
86
+ Or in Rails 3:
87
+
88
+ script/rails generate business_time:config
89
+
90
+ The generator will add a /config/business_time.yml and a /config/initializers/business_time.rb file that will cause the start of business day, the end of business day, and your holidays to be loaded from the yaml file. You might want to programatically load your holidays from a database table, but you will want to pay attention to how the initializer works - you will want to make sure that the initializer sets stuff up appropriately so rails instances on mongrels or passenger will have the appropriate data as they come up and down.
91
+
92
+ == Outside of Rails
93
+ This code does depend on ActiveSupport, but nothing else within rails. Even then, it would be pretty easy to break that dependency as well (but would add some code bloat and remove some clarity). Feel free to use it on any ruby project you'd like!
94
+
95
+ == Contributors
96
+ * David Bock http://github.com/bokmann
97
+ * Enrico Bianco http://github.com/enricob
75
98
  == Note on Patches/Pull Requests
76
99
 
77
100
  * Fork the project.
data/Rakefile CHANGED
@@ -12,6 +12,7 @@ begin
12
12
  gem.authors = ["bokmann"]
13
13
  gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
14
  gem.add_dependency('activesupport','>= 2.0.0')
15
+ gem.files += FileList['lib/generators/**/*.rb']
15
16
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
17
  end
17
18
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -2,16 +2,41 @@ module BusinessTime
2
2
 
3
3
  # controls the behavior of the code. You can change
4
4
  # the beginning_of_workday, end_of_workday, and the list of holidays
5
+ #manually, or with a yaml file and the load method.
5
6
  class Config
6
7
  class << self
7
8
  attr_accessor :beginning_of_workday
8
9
  attr_accessor :end_of_workday
9
10
  attr_accessor :holidays
10
-
11
11
  end
12
- self.holidays = []
13
- self.beginning_of_workday = "9:00 am"
14
- self.end_of_workday = "5:00 pm"
12
+
13
+ def self.reset
14
+ self.holidays = []
15
+ self.beginning_of_workday = "9:00 am"
16
+ self.end_of_workday = "5:00 pm"
17
+ end
18
+
19
+ # loads the config data from a yaml file written as:
20
+ # business_time:
21
+ # beginning_od_workday: 8:30 am
22
+ # end_of_workday: 5:30 pm
23
+ # holidays:
24
+ # - Jan 1st, 2010
25
+ # - July 4th, 2010
26
+ # - Dec 25th, 2010
27
+ def self.load(filename)
28
+ self.reset
29
+ data = YAML::load(File.open(filename))
30
+ self.beginning_of_workday = data["business_time"]["beginning_of_workday"]
31
+ self.end_of_workday = data["business_time"]["end_of_workday"]
32
+ data["business_time"]["holidays"].each do |holiday|
33
+ self.holidays << Date.parse(holiday)
34
+ end
35
+
36
+ end
37
+
38
+ #reset the first time we are loaded.
39
+ self.reset
15
40
  end
16
41
 
17
42
  end
@@ -0,0 +1,21 @@
1
+ module BusinessTime
2
+ module Generators
3
+ class ConfigGenerator < Rails::Generators::Base # :nodoc:
4
+
5
+ def self.gem_root
6
+ File.expand_path("../../../..", __FILE__)
7
+ end
8
+
9
+ def self.source_root
10
+ # Use the templates from the 2.3.x generator
11
+ File.join(gem_root, 'rails_generators', 'business_time_config', 'templates')
12
+ end
13
+
14
+ def generate
15
+ template 'business_time.rb', File.join('config', 'initializers', 'business_time.rb')
16
+ template 'business_time.yml', File.join('config', 'business_time.yml')
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ # This generator simply drops the business_time.rb and business_time.yml file
2
+ # into the appropate places in a rails app to configure and initialize the
3
+ # data. Once generated, these files are yours to modify.
4
+ class BusinessTimeConfigGenerator < Rails::Generator::Base
5
+ def manifest
6
+ record do |m|
7
+ m.template('business_time.rb', "config/initializers/business_time.rb")
8
+ m.template('business_time.yml', "config/business_time.yml")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ BusinessTime::Config.load("#{RAILS_ROOT}/config/business_time.yml")
2
+
3
+ # or you can configure it manually: look at me! I'm Tim Ferris!
4
+ # BusinessTime.Config.beginning_of_workday = "10:00 am"
5
+ # BusinessTime.Comfig.end_of_workday = "11:30 am"
6
+ # BusinessTime.config.holidays << Date.parse("August 4th, 2010")
@@ -0,0 +1,7 @@
1
+ business_time:
2
+ beginning_of_workday: 9:00 am
3
+ end_of_workday: 5:00 pm
4
+ holidays:
5
+ - Jan 01, 2010
6
+ - July 4th, 2010
7
+ - December 25th, 2010
@@ -1,5 +1,13 @@
1
1
  require 'rubygems'
2
- require 'active_support'
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
+ end
10
+
3
11
  require 'test/unit'
4
12
  require 'shoulda'
5
13
 
@@ -9,18 +9,59 @@ class TestBusinessDays < Test::Unit::TestCase
9
9
  assert expected == later
10
10
  end
11
11
 
12
- should "move to yesterday is we subtract a business day"
12
+ should "move to yesterday is we subtract a business day" do
13
+ first = Time.parse("April 13th, 2010, 11:00 am")
14
+ before = 1.business_day.before(first)
15
+ expected = Time.parse("April 12th, 2010, 11:00 am")
16
+ assert expected == before
17
+ end
13
18
 
14
- should "take into account the weekend when adding a day"
19
+ should "take into account the weekend when adding a day" do
20
+ first = Time.parse("April 9th, 2010, 12:33 pm")
21
+ after = 1.business_day.after(first)
22
+ expected = Time.parse("April 12th, 2010, 12:33 pm")
23
+ assert expected == after
24
+ end
15
25
 
16
- should "move forward one week when adding 5 business days"
26
+ should "take into account the weekend when subtracting a day" do
27
+ first = Time.parse("April 12th, 2010, 12:33 pm")
28
+ before = 1.business_day.before(first)
29
+ expected = Time.parse("April 9th, 2010, 12:33 pm")
30
+ assert expected == before
31
+ end
17
32
 
18
- should "take into account a holiday when adding a day"
33
+ should "move forward one week when adding 5 business days" do
34
+ first = Time.parse("April 9th, 2010, 12:33 pm")
35
+ after = 5.business_days.after(first)
36
+ expected = Time.parse("April 16th, 2010, 12:33 pm")
37
+ assert expected == after
38
+ end
19
39
 
20
- should "take into account a holiday on a weekend"
40
+ should "move backward one week when subtracting 5 business days" do
41
+ first = Time.parse("April 16th, 2010, 12:33 pm")
42
+ before = 5.business_days.before(first)
43
+ expected = Time.parse("April 9th, 2010, 12:33 pm")
44
+ assert expected == before
45
+ end
46
+
47
+ should "take into account a holiday when adding a day" do
48
+ three_day_weekend = Date.parse("July 5th, 2010")
49
+ BusinessTime::Config.holidays << three_day_weekend
50
+ friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
51
+ tuesday_afternoon = 1.business_day.after(friday_afternoon)
52
+ expected = Time.parse("July 6th, 2010, 4:50 pm")
53
+ assert expected == tuesday_afternoon
54
+ end
21
55
 
22
- should "take into account a holiday on a Monday"
23
56
 
24
- should "take into account a holiday on a Friday"
57
+ should "take into account a holiday on a weekend" do
58
+ BusinessTime::Config.reset
59
+ july_4 = Date.parse("July 4th, 2010")
60
+ BusinessTime::Config.holidays << july_4
61
+ friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
62
+ monday_afternoon = 1.business_day.after(friday_afternoon)
63
+ expected = Time.parse("July 5th, 2010, 4:50 pm")
64
+ assert expected == monday_afternoon
65
+ end
25
66
 
26
67
  end
@@ -5,18 +5,43 @@ class TestBusinessHours < Test::Unit::TestCase
5
5
  first = Time.parse("Aug 4 2010, 9:35 am")
6
6
  later = 8.business_hours.after(first)
7
7
  expected = Time.parse("Aug 5 2010, 9:35 am")
8
+ assert expected == later
8
9
  end
9
10
 
10
- should "move to yesterday is we subtract 8 business hours"
11
-
12
- should "take into account a weekend"
11
+ should "move to yesterday if we subtract 8 business hours" do
12
+ first = Time.parse("Aug 4 2010, 9:35 am")
13
+ later = 8.business_hours.before(first)
14
+ expected = Time.parse("Aug 3 2010, 9:35 am")
15
+ assert expected == later
16
+ end
13
17
 
14
- should "take into account a holiday"
18
+ should "take into account a weekend when adding an hour" do
19
+ friday_afternoon = Time.parse("April 9th, 4:50 pm")
20
+ monday_morning = 1.business_hour.after(friday_afternoon)
21
+ expected = Time.parse("April 12th 2010, 9:50 am")
22
+ assert expected == monday_morning
23
+ end
15
24
 
16
- should "add hours in the middle of the workday"
25
+ should "take into account a weekend when subtracting an hour" do
26
+ monday_morning = Time.parse("April 12th 2010, 9:50 am")
27
+ friday_afternoon = 1.business_hour.before(monday_morning)
28
+ expected = Time.parse("April 9th 2010, 4:50 pm")
29
+ assert expected == friday_afternoon
30
+ end
17
31
 
18
- should "wrap to tomorrow if past the end of the workday"
32
+ should "take into account a holiday" do
33
+ BusinessTime::Config.holidays << Date.parse("July 5th, 2010")
34
+ friday_afternoon = Time.parse("July 2nd 2010, 4:50pm")
35
+ tuesday_morning = 1.business_hour.after(friday_afternoon)
36
+ expected = Time.parse("July 6th 2010, 9:50 am")
37
+ assert expected == tuesday_morning
38
+ end
19
39
 
20
- should "wrap to yesterday if past the beginning of the workday"
40
+ should "add hours in the middle of the workday" do
41
+ monday_morning = Time.parse("April 12th 2010, 9:50 am")
42
+ later = 3.business_hours.after(monday_morning)
43
+ expected = Time.parse("April 12th 2010, 12:50 pm")
44
+ assert expected == later
45
+ end
21
46
 
22
47
  end
@@ -15,6 +15,7 @@ class TestConfig < Test::Unit::TestCase
15
15
  end
16
16
 
17
17
  should "keep track of holidays" do
18
+ BusinessTime::Config.reset
18
19
  assert BusinessTime::Config.holidays.empty?
19
20
  daves_birthday = Date.parse("August 4th, 1969")
20
21
  BusinessTime::Config.holidays << daves_birthday
@@ -3,14 +3,31 @@ require 'helper'
3
3
  class TestDateExtensions < Test::Unit::TestCase
4
4
 
5
5
  should "know what a weekend day is" do
6
- assert(Time.parse("April 9, 2010").weekday?)
7
- assert(!Time.parse("April 10, 2010").weekday?)
8
- assert(!Time.parse("April 11, 2010").weekday?)
9
- assert(Time.parse("April 12, 2010").weekday?)
6
+ assert(Date.parse("April 9, 2010").weekday?)
7
+ assert(!Date.parse("April 10, 2010").weekday?)
8
+ assert(!Date.parse("April 11, 2010").weekday?)
9
+ assert(Date.parse("April 12, 2010").weekday?)
10
10
  end
11
11
 
12
- should "know a weekend day is not a workday"
12
+ should "know a weekend day is not a workday" do
13
+ assert(Date.parse("April 9, 2010").workday?)
14
+ assert(!Date.parse("April 10, 2010").workday?)
15
+ assert(!Date.parse("April 11, 2010").workday?)
16
+ assert(Date.parse("April 12, 2010").workday?)
17
+ end
13
18
 
14
- should "know a holiday is not a workday"
19
+ should "know a holiday is not a workday" do
20
+ july_4 = Date.parse("July 4, 2010")
21
+ july_5 = Date.parse("July 5, 2010")
22
+
23
+ assert(!july_4.workday?)
24
+ assert(july_5.workday?)
25
+
26
+ BusinessTime::Config.holidays << july_4
27
+ BusinessTime::Config.holidays << july_5
28
+
29
+ assert(!july_4.workday?)
30
+ assert(!july_5.workday?)
31
+ end
15
32
 
16
33
  end
@@ -9,12 +9,34 @@ class TestTimeExtensions < Test::Unit::TestCase
9
9
  assert(Time.parse("April 12, 2010 10:30am").weekday?)
10
10
  end
11
11
 
12
- should "know a weekend day is not a workday"
12
+ should "know a weekend day is not a workday" do
13
+ assert(Time.parse("April 9, 2010 10:45 am").workday?)
14
+ assert(!Time.parse("April 10, 2010 10:45 am").workday?)
15
+ assert(!Time.parse("April 11, 2010 10:45 am").workday?)
16
+ assert(Time.parse("April 12, 2010 10:45 am").workday?)
17
+ end
13
18
 
14
- should "know a holiday is not a workday"
19
+ should "know a holiday is not a workday" do
20
+ BusinessTime::Config.reset
21
+
22
+ BusinessTime::Config.holidays << Date.parse("July 4, 2010")
23
+ BusinessTime::Config.holidays << Date.parse("July 5, 2010")
24
+
25
+ assert(!Time.parse("July 4th, 2010 1:15 pm").workday?)
26
+ assert(!Time.parse("July 5th, 2010 2:37 pm").workday?)
27
+ end
15
28
 
16
- should "know the beginning of the day for an instance"
17
29
 
18
- should "know the end of the day for an instance"
30
+ should "know the beginning of the day for an instance" do
31
+ first = Time.parse("August 17th, 2010, 11:50 am")
32
+ expecting = Time.parse("August 17th, 2010, 9:00 am")
33
+ assert expecting == first.beginning_of_workday
34
+ end
35
+
36
+ should "know the end of the day for an instance" do
37
+ first = Time.parse("August 17th, 2010, 11:50 am")
38
+ expecting = Time.parse("August 17th, 2010, 5:00 pm")
39
+ assert expecting == first.end_of_workday
40
+ end
19
41
 
20
42
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - bokmann
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-13 00:00:00 -04:00
17
+ date: 2010-04-16 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -66,6 +66,10 @@ files:
66
66
  - lib/extensions/date.rb
67
67
  - lib/extensions/fixnum.rb
68
68
  - lib/extensions/time.rb
69
+ - lib/generators/business_time/config_generator.rb
70
+ - rails_generators/business_time_config/business_time_config_generator.rb
71
+ - rails_generators/business_time_config/templates/business_time.rb
72
+ - rails_generators/business_time_config/templates/business_time.yml
69
73
  - test/helper.rb
70
74
  - test/test_business_days.rb
71
75
  - test/test_business_hours.rb