business_hours 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +10 -0
- data/business_hours.gemspec +27 -0
- data/lib/business_hours/day.rb +87 -0
- data/lib/business_hours/version.rb +3 -0
- data/lib/business_hours/week.rb +36 -0
- data/lib/business_hours.rb +10 -0
- data/test/test_business_hours.rb +87 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e44aea9efb82d12de3027199cbab0a350208b3d6
|
4
|
+
data.tar.gz: 04d9092b23ef31966f447e945f907a1b14216d6d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 39ac0bf45a06e90e443b3c12e8e47c7b3c53dd82510a0a12ff84981f380e31be7ff682654368f9e8a80f028894f3266f25b0fbf02aeb1c99532248897e4e7564
|
7
|
+
data.tar.gz: 3a93ced6470b42334f2a63a1dc849ecd2b744839b6ff55ccc10bc6d91d23acc224f026dcb71f53add00fe835fbed1040c3e95be1762da8f529fbe71314893953
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ivan Kryak
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# BusinessHours
|
2
|
+
|
3
|
+
This gem provides simple DSL to describe working hours for business place.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'business_hours'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install business_hours
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Create new business week for particular time zone:
|
22
|
+
|
23
|
+
business_hours = BusinessHours::Week.new('Europe/Moscow')
|
24
|
+
|
25
|
+
Then add some working days:
|
26
|
+
|
27
|
+
business_hours.day(1) do
|
28
|
+
open_at '10:00'
|
29
|
+
close_at '20:00'
|
30
|
+
has_break '13:00', '14:00'
|
31
|
+
end
|
32
|
+
|
33
|
+
or you can use abbreviation for day name:
|
34
|
+
|
35
|
+
business_hours.day(:mon)
|
36
|
+
|
37
|
+
If there is no information about a day, then we assume that it's closed. Or you can mark day as closed with:
|
38
|
+
|
39
|
+
business_hours.days(1) do
|
40
|
+
closed!
|
41
|
+
end
|
42
|
+
|
43
|
+
Then you can easily check if business place is opened now:
|
44
|
+
|
45
|
+
business_hours.now_open?
|
46
|
+
|
47
|
+
See test folder for more information
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'business_hours/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "business_hours"
|
8
|
+
spec.version = BusinessHours::VERSION
|
9
|
+
spec.authors = ["Ivan Kryak"]
|
10
|
+
spec.email = ["kryak.iv@gmail.com"]
|
11
|
+
spec.description = %q{Allows easily handle opening hours without take care of time zones}
|
12
|
+
spec.summary = %q{Allows easily handle opening hours without take care of time zones}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
spec.add_development_dependency "timecop"
|
25
|
+
spec.add_runtime_dependency "activesupport"
|
26
|
+
spec.add_runtime_dependency "tzinfo"
|
27
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module BusinessHours
|
4
|
+
class Day
|
5
|
+
class DayDelegator < SimpleDelegator
|
6
|
+
def open_at(time)
|
7
|
+
self.open_at = Time.zone.parse(time)
|
8
|
+
end
|
9
|
+
|
10
|
+
def close_at(time)
|
11
|
+
self.close_at = Time.zone.parse(time)
|
12
|
+
end
|
13
|
+
|
14
|
+
def has_break(from, to)
|
15
|
+
self.break = { from: Time.zone.parse(from), to: Time.zone.parse(to) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def closed!
|
19
|
+
self.closed = true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_accessor :open_at, :close_at, :closed, :break
|
24
|
+
|
25
|
+
class << self
|
26
|
+
def build(&block)
|
27
|
+
day = new
|
28
|
+
day.build(&block)
|
29
|
+
day
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def build(&block)
|
34
|
+
delegator = DayDelegator.new(self)
|
35
|
+
delegator.instance_eval(&block)
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(day_of_week)
|
40
|
+
@day_of_week = day_of_week
|
41
|
+
|
42
|
+
@open_at = nil
|
43
|
+
@close_at = nil
|
44
|
+
@closed = false
|
45
|
+
@break = { from: nil, to: nil }
|
46
|
+
end
|
47
|
+
|
48
|
+
def closed?
|
49
|
+
@closed or [@open_at, @close_at].any?(&:nil?)
|
50
|
+
end
|
51
|
+
|
52
|
+
def today?
|
53
|
+
@day_of_week == Date.today.wday
|
54
|
+
end
|
55
|
+
|
56
|
+
def has_breaks?
|
57
|
+
@break.values_at(:from, :to).compact.any?
|
58
|
+
end
|
59
|
+
|
60
|
+
def closed_at?(time)
|
61
|
+
return true if closed?
|
62
|
+
|
63
|
+
open = @open_at.change(day: time.day, month: time.month, year: time.year)
|
64
|
+
close = @close_at.change(day: time.day, month: time.month, year: time.year)
|
65
|
+
|
66
|
+
result = !time.between?(open, close)
|
67
|
+
|
68
|
+
if has_breaks?
|
69
|
+
break_start = @break[:from].change(day: time.day, month: time.month, year: time.year)
|
70
|
+
break_end = @break[:to].change(day: time.day, month: time.month, year: time.year)
|
71
|
+
|
72
|
+
result = result and time.between?(break_start, break_end)
|
73
|
+
end
|
74
|
+
|
75
|
+
result
|
76
|
+
end
|
77
|
+
|
78
|
+
def to_s(format = :short)
|
79
|
+
case format
|
80
|
+
when :short
|
81
|
+
Date::ABBR_DAYNAMES[@day_of_week]
|
82
|
+
else
|
83
|
+
Date::DAYNAMES[@day_of_week]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module BusinessHours
|
2
|
+
class Week
|
3
|
+
def initialize(time_zone = UTC_TIME_ZONE)
|
4
|
+
@time_zone = time_zone
|
5
|
+
@days = Array.new(7).each_with_index.map { |n, i| Day.new(i) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def day(day_name, &block)
|
9
|
+
Time.use_zone(@time_zone) do
|
10
|
+
day_index = if day_name.is_a? Fixnum
|
11
|
+
day_name
|
12
|
+
elsif day_name.respond_to? :to_s
|
13
|
+
Date::ABBR_DAYNAMES.index(day_name.to_s.capitalize)
|
14
|
+
end
|
15
|
+
@days[day_index].tap { |day| day.build(&block) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def closed?
|
20
|
+
current_day = @days[Date.today.wday]
|
21
|
+
|
22
|
+
return true if current_day.nil? or current_day.closed? # if there is no data for current day, assume that it's closed all day
|
23
|
+
|
24
|
+
current_time = Time.now
|
25
|
+
current_day.closed_at?(current_time)
|
26
|
+
end
|
27
|
+
|
28
|
+
def now_open?
|
29
|
+
!closed?
|
30
|
+
end
|
31
|
+
|
32
|
+
def days
|
33
|
+
@days
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'business_hours'
|
3
|
+
require 'timecop'
|
4
|
+
|
5
|
+
class TestBusinessHours < Test::Unit::TestCase
|
6
|
+
def initialize(*args)
|
7
|
+
super
|
8
|
+
Time.zone = 'Etc/UTC'
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_initialization
|
12
|
+
week = BusinessHours::Week.new
|
13
|
+
|
14
|
+
week.day('mon') do
|
15
|
+
open_at '10:00'
|
16
|
+
close_at '20:00'
|
17
|
+
has_break '13:00', '13:30'
|
18
|
+
end
|
19
|
+
|
20
|
+
week.day('tue') do
|
21
|
+
closed!
|
22
|
+
end
|
23
|
+
|
24
|
+
assert_not_nil week
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_closed_today
|
28
|
+
hours = BusinessHours::Week.new
|
29
|
+
hours.day(Date.today.wday) do
|
30
|
+
closed!
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_equal hours.closed?, true
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_open_today
|
37
|
+
hours = BusinessHours::Week.new
|
38
|
+
|
39
|
+
hours.day(Date.today.wday) do
|
40
|
+
open_at '10:00'
|
41
|
+
close_at '20:00'
|
42
|
+
end
|
43
|
+
|
44
|
+
Timecop.travel(Time.zone.now.change(hour: 11)) { assert_equal hours.closed?, false }
|
45
|
+
Timecop.travel(Time.zone.now.change(hour: 9)) { assert_equal hours.closed?, true }
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_different_timezone
|
49
|
+
Time.zone = 'Europe/Moscow'
|
50
|
+
hours = BusinessHours::Week.new('Europe/Berlin')
|
51
|
+
hours.day(Date.today.wday) do
|
52
|
+
open_at '10:00'
|
53
|
+
close_at '20:00'
|
54
|
+
end
|
55
|
+
|
56
|
+
Timecop.travel(Time.zone.now.change(hour: 11)) { assert_equal hours.closed?, true }
|
57
|
+
Timecop.travel(Time.zone.now.change(hour: 9)) { assert_equal hours.closed?, true }
|
58
|
+
Timecop.travel(Time.zone.now.change(hour: 14)) { assert_equal hours.closed?, false }
|
59
|
+
Timecop.travel(Time.zone.now.change(hour: 15)) { assert_equal hours.closed?, false }
|
60
|
+
Timecop.travel(Time.zone.now.change(hour: 23)) { assert_equal hours.closed?, true }
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_with_breaks
|
64
|
+
hours = BusinessHours::Week.new('Europe/Berlin')
|
65
|
+
day = hours.day(Date.today.wday) do
|
66
|
+
open_at '10:00'
|
67
|
+
close_at '20:00'
|
68
|
+
has_break '13:30', '14:00'
|
69
|
+
end
|
70
|
+
|
71
|
+
assert_equal day.has_breaks?, true
|
72
|
+
Timecop.travel(Time.now.change(hour: 13, minutes: 40)) { assert_equal hours.closed?, true }
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_initialization_with_dayname
|
76
|
+
hours = BusinessHours::Week.new('Europe/Berlin')
|
77
|
+
day = hours.day(Date::ABBR_DAYNAMES[Date.today.wday]) do
|
78
|
+
open_at '10:00'
|
79
|
+
close_at '20:00'
|
80
|
+
end
|
81
|
+
|
82
|
+
assert_equal day.today?, true
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_default_values
|
86
|
+
end
|
87
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: business_hours
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivan Kryak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: timecop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: tzinfo
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Allows easily handle opening hours without take care of time zones
|
98
|
+
email:
|
99
|
+
- kryak.iv@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- business_hours.gemspec
|
110
|
+
- lib/business_hours.rb
|
111
|
+
- lib/business_hours/day.rb
|
112
|
+
- lib/business_hours/version.rb
|
113
|
+
- lib/business_hours/week.rb
|
114
|
+
- test/test_business_hours.rb
|
115
|
+
homepage: ''
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata: {}
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.1.10
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Allows easily handle opening hours without take care of time zones
|
139
|
+
test_files:
|
140
|
+
- test/test_business_hours.rb
|