punch_time 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +30 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.rubocop.yml +29 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +0 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +69 -0
- data/LICENSE.txt +21 -0
- data/README.md +98 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/punch_time.rb +36 -0
- data/lib/punch_time/configuration.rb +94 -0
- data/lib/punch_time/error.rb +7 -0
- data/lib/punch_time/time_record.rb +164 -0
- data/lib/punch_time/version.rb +5 -0
- data/punch_time.gemspec +45 -0
- data/support/body_punch_hand.png +0 -0
- metadata +151 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 378bfd0ca8bb7a6e03456e9514a1a3e1cb4e44ffe3735b95ac4d35321d8e0c96
|
4
|
+
data.tar.gz: d9242ab9ebe24d3a75423ec02518c8ba8b568b7799aad0b8205ec1ba651c9480
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4d0bae899922bb247d3a368cff7cd311931d4077ca4374ccc31746f3eb1be308ce3a85404a57349b6e1e17df1249cb9012a51c3d0d5589877f90815bea3f3568
|
7
|
+
data.tar.gz: f47e1b59a67c5f47e661b4def268e3ea613c18fb89f09f79ba30662ce137e506b3a8bde23577764453e13eae7cf5937044138849475ce74a189b295425a25fca
|
@@ -0,0 +1,30 @@
|
|
1
|
+
---
|
2
|
+
version: 2
|
3
|
+
jobs:
|
4
|
+
build:
|
5
|
+
working_directory: ~/punch_time
|
6
|
+
docker:
|
7
|
+
- image: ruby:2.6.3
|
8
|
+
steps:
|
9
|
+
- run:
|
10
|
+
command: |
|
11
|
+
apt-get update -qq && apt-get install -y build-essential
|
12
|
+
rm -rf /var/lib/apt/lists/*
|
13
|
+
- checkout
|
14
|
+
- restore_cache:
|
15
|
+
keys:
|
16
|
+
- dependency-cache-{{ checksum "Gemfile.lock" }}
|
17
|
+
- run:
|
18
|
+
command: |
|
19
|
+
gem update --system
|
20
|
+
bundle install --path vendor/bundle
|
21
|
+
- save_cache:
|
22
|
+
key: dependency-cache-{{ checksum "Gemfile.lock" }}
|
23
|
+
paths:
|
24
|
+
- vendor/bundle
|
25
|
+
- run:
|
26
|
+
name: Run Test
|
27
|
+
command: bundle exec rake spec
|
28
|
+
- run:
|
29
|
+
name: Run Lint
|
30
|
+
command: bundle exec rubocop -c .rubocop.yml
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'vendor/**/*'
|
4
|
+
- 'bin/**/*'
|
5
|
+
- !ruby/regexp /old_and_unused\.rb$/
|
6
|
+
TargetRubyVersion: 2.6
|
7
|
+
Style/Documentation:
|
8
|
+
Enabled: false
|
9
|
+
Layout/LineLength:
|
10
|
+
AutoCorrect: true
|
11
|
+
Max: 100
|
12
|
+
Metrics/BlockLength:
|
13
|
+
AutoCorrect: true
|
14
|
+
Max: 120
|
15
|
+
Metrics/MethodLength:
|
16
|
+
AutoCorrect: true
|
17
|
+
Max: 20
|
18
|
+
Metrics/ModuleLength:
|
19
|
+
Max: 180
|
20
|
+
Metrics/AbcSize:
|
21
|
+
Max: 35
|
22
|
+
Style/MultilineBlockChain:
|
23
|
+
Enabled: false
|
24
|
+
Metrics/ClassLength:
|
25
|
+
Max: 120
|
26
|
+
Metrics/PerceivedComplexity:
|
27
|
+
Max: 9
|
28
|
+
Metrics/CyclomaticComplexity:
|
29
|
+
Max: 9
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.3
|
data/CHANGELOG.md
ADDED
File without changes
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at smapira@routeflags.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
punch_time (0.1.3)
|
5
|
+
activemodel
|
6
|
+
activesupport
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (6.0.2.1)
|
12
|
+
activesupport (= 6.0.2.1)
|
13
|
+
activesupport (6.0.2.1)
|
14
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
15
|
+
i18n (>= 0.7, < 2)
|
16
|
+
minitest (~> 5.1)
|
17
|
+
tzinfo (~> 1.1)
|
18
|
+
zeitwerk (~> 2.2)
|
19
|
+
ast (2.4.0)
|
20
|
+
concurrent-ruby (1.1.5)
|
21
|
+
diff-lcs (1.3)
|
22
|
+
i18n (1.8.2)
|
23
|
+
concurrent-ruby (~> 1.0)
|
24
|
+
jaro_winkler (1.5.4)
|
25
|
+
minitest (5.14.0)
|
26
|
+
parallel (1.19.1)
|
27
|
+
parser (2.7.0.2)
|
28
|
+
ast (~> 2.4.0)
|
29
|
+
rainbow (3.0.0)
|
30
|
+
rake (10.5.0)
|
31
|
+
rspec (3.9.0)
|
32
|
+
rspec-core (~> 3.9.0)
|
33
|
+
rspec-expectations (~> 3.9.0)
|
34
|
+
rspec-mocks (~> 3.9.0)
|
35
|
+
rspec-core (3.9.1)
|
36
|
+
rspec-support (~> 3.9.1)
|
37
|
+
rspec-expectations (3.9.0)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.9.0)
|
40
|
+
rspec-mocks (3.9.1)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.9.0)
|
43
|
+
rspec-support (3.9.2)
|
44
|
+
rubocop (0.79.0)
|
45
|
+
jaro_winkler (~> 1.5.1)
|
46
|
+
parallel (~> 1.10)
|
47
|
+
parser (>= 2.7.0.1)
|
48
|
+
rainbow (>= 2.2.2, < 4.0)
|
49
|
+
ruby-progressbar (~> 1.7)
|
50
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
51
|
+
ruby-progressbar (1.10.1)
|
52
|
+
thread_safe (0.3.6)
|
53
|
+
tzinfo (1.2.6)
|
54
|
+
thread_safe (~> 0.1)
|
55
|
+
unicode-display_width (1.6.1)
|
56
|
+
zeitwerk (2.2.2)
|
57
|
+
|
58
|
+
PLATFORMS
|
59
|
+
ruby
|
60
|
+
|
61
|
+
DEPENDENCIES
|
62
|
+
bundler
|
63
|
+
punch_time!
|
64
|
+
rake (~> 10.0)
|
65
|
+
rspec (~> 3.0)
|
66
|
+
rubocop
|
67
|
+
|
68
|
+
BUNDLED WITH
|
69
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 smapira
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# punch_time
|
2
|
+
|
3
|
+
[![CircleCI](https://circleci.com/bb/routeflagsinc/punch_time.svg?style=svg)](https://circleci.com/bb/routeflagsinc/punch_time)
|
4
|
+
|
5
|
+
|
6
|
+
[logo]: https://bitbucket.org/routeflagsinc/punch_time/raw/c0df4f773da9bd66822186350f6516efe3be5a72/support/body_punch_hand.png "punch"
|
7
|
+
|
8
|
+
Work time calculations using punch clock.
|
9
|
+
|
10
|
+
## Features
|
11
|
+
|
12
|
+
Calculates a work, a tardy, a overtime work and a night work times.
|
13
|
+
|
14
|
+
## Motivation
|
15
|
+
|
16
|
+
Proud to present our newest line of gem, the human resource calculations series. As you know, the modern human resource calculations has been needing a large variety of case. This gem include simplest calculator for overtime work, night work and tardy time.
|
17
|
+
If you would kindly accept our request, this gem willing to calculation.
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'punch_time'
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install punch_time
|
34
|
+
|
35
|
+
|
36
|
+
## Configuration
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
PunchTime.configure do |config|
|
40
|
+
config.shift_in_time = Time.parse('10:00')
|
41
|
+
config.shift_out_time = Time.parse('19:00')
|
42
|
+
config.breaks = [
|
43
|
+
{
|
44
|
+
start_time: Time.parse('12:00'),
|
45
|
+
end_time: Time.parse('13:00')
|
46
|
+
}
|
47
|
+
]
|
48
|
+
config.night = {
|
49
|
+
start_time: Time.parse('22:00'),
|
50
|
+
end_time: Time.parse('05:00')
|
51
|
+
}
|
52
|
+
config.offset = '+0000'
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
## Usage
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# Set the your time record
|
60
|
+
PunchTime.punch(DateTime.parse('20200101 10:10'), DateTime.parse('20200101 19:00'))
|
61
|
+
|
62
|
+
# Calculations can be performed in seconds, minutes, hours, or days
|
63
|
+
p PunchTime.sum_work.hours
|
64
|
+
|
65
|
+
# When need calculate holiday work time (use with business_time)
|
66
|
+
sum_works = []
|
67
|
+
Date.parse('20200101').upto(Date.parse('20200105')) do |x|
|
68
|
+
PunchTime.punch(DateTime.parse(x.to_s + ' 10:10'), DateTime.parse(x.to_s + ' 19:00'))
|
69
|
+
sum_works.append(PunchTime.sum_work.hours) unless x.workday?
|
70
|
+
end
|
71
|
+
p sum_works.inject(:+)
|
72
|
+
|
73
|
+
# Other methods
|
74
|
+
PunchTime.sum_work
|
75
|
+
PunchTime.sum_tardy
|
76
|
+
PunchTime.sum_over_work
|
77
|
+
PunchTime.sum_night_work
|
78
|
+
PunchTime.tardy?
|
79
|
+
PunchTime.overtime_work?
|
80
|
+
PunchTime.night_overtime_work?
|
81
|
+
```
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
Bug reports and pull requests are welcome on GitHub at https://bitbucket.org/routeflagsinc/punch_time. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
90
|
+
|
91
|
+
## Code of Conduct
|
92
|
+
|
93
|
+
Everyone interacting in the PunchClock project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://bitbucket.org/routeflagsinc/punch_time/blob/master/CODE_OF_CONDUCT.md).
|
94
|
+
|
95
|
+
## Good stuffs
|
96
|
+
|
97
|
+
- [biz](https://github.com/zendesk/biz)
|
98
|
+
- [business_time](https://github.com/bokmann/business_time)
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "punch/clock"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/punch_time.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'punch_time/version'
|
4
|
+
require 'punch_time/configuration'
|
5
|
+
require 'punch_time/time_record'
|
6
|
+
require 'punch_time/error'
|
7
|
+
require 'active_support/core_ext/module'
|
8
|
+
require 'forwardable'
|
9
|
+
|
10
|
+
module PunchTime
|
11
|
+
class << self
|
12
|
+
extend Forwardable
|
13
|
+
|
14
|
+
def configure(&config)
|
15
|
+
Thread.current[:time_record] = TimeRecord.new(&config)
|
16
|
+
end
|
17
|
+
|
18
|
+
delegate %i[
|
19
|
+
punch
|
20
|
+
sum_tardy
|
21
|
+
sum_work
|
22
|
+
sum_over_work
|
23
|
+
sum_night_work
|
24
|
+
tardy?
|
25
|
+
overtime_work?
|
26
|
+
night_overtime_work?
|
27
|
+
] => :time_record
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def time_record
|
32
|
+
Thread.current[:time_record] ||
|
33
|
+
raise(Error::Configuration, "#{name} not configured")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'time'
|
5
|
+
require 'active_model'
|
6
|
+
|
7
|
+
module PunchTime
|
8
|
+
class Configuration
|
9
|
+
include ActiveModel::Validations
|
10
|
+
|
11
|
+
attr_accessor :raw
|
12
|
+
attr_accessor :shift_in_time
|
13
|
+
attr_accessor :shift_out_time
|
14
|
+
attr_accessor :night
|
15
|
+
attr_accessor :breaks
|
16
|
+
attr_accessor :offset
|
17
|
+
|
18
|
+
validates :shift_in_time, presence: true
|
19
|
+
validates :shift_out_time, presence: true
|
20
|
+
validates :night, presence: true
|
21
|
+
validate :no_format
|
22
|
+
|
23
|
+
def initialize
|
24
|
+
self.raw = Raw.new
|
25
|
+
|
26
|
+
yield raw if block_given?
|
27
|
+
|
28
|
+
self.shift_in_time ||= raw.shift_in_time
|
29
|
+
self.shift_out_time ||= raw.shift_out_time
|
30
|
+
self.night ||= raw.night
|
31
|
+
self.breaks ||= raw.breaks
|
32
|
+
self.offset ||= raw.offset
|
33
|
+
|
34
|
+
raise ArgumentError, errors.messages unless valid?
|
35
|
+
|
36
|
+
raw.freeze
|
37
|
+
end
|
38
|
+
|
39
|
+
Raw = Struct.new(:shift_in_time, :shift_out_time, :night,
|
40
|
+
:breaks, :offset) do
|
41
|
+
module Default
|
42
|
+
BREAKS = [
|
43
|
+
{
|
44
|
+
start_time: Time.parse('12:00'),
|
45
|
+
end_time: Time.parse('13:00')
|
46
|
+
}
|
47
|
+
].freeze
|
48
|
+
|
49
|
+
NIGHT = {
|
50
|
+
start_time: Time.parse('22:00'),
|
51
|
+
end_time: Time.parse('05:00')
|
52
|
+
}.freeze
|
53
|
+
|
54
|
+
START_TIME = Time.parse('10:00').freeze
|
55
|
+
END_TIME = Time.parse('19:00').freeze
|
56
|
+
OFFSET = '+0000'
|
57
|
+
end
|
58
|
+
|
59
|
+
def initialize(*)
|
60
|
+
super
|
61
|
+
|
62
|
+
self.shift_in_time ||= Default::START_TIME
|
63
|
+
self.shift_out_time ||= Default::END_TIME
|
64
|
+
self.night ||= Default::NIGHT
|
65
|
+
self.breaks ||= Default::BREAKS
|
66
|
+
self.offset ||= Default::OFFSET
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private_constant :Raw,
|
71
|
+
:Default
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def no_format
|
76
|
+
%i[shift_in_time shift_out_time].map do |x|
|
77
|
+
errors.add(x, 'Allow only Time format') unless public_send(x).is_a?(Time)
|
78
|
+
end
|
79
|
+
errors.add(:night, 'Allow only Time format') unless night[:start_time].is_a?(Time)
|
80
|
+
errors.add(:night, 'Allow only Time format') unless night[:end_time].is_a?(Time)
|
81
|
+
errors.add(:offset, 'Allow only String format') unless offset.is_a?(String)
|
82
|
+
breaks.each do |x|
|
83
|
+
unless x[:start_time].blank? || x[:start_time].is_a?(Time)
|
84
|
+
errors.add(:breaks,
|
85
|
+
'Allow only Time format')
|
86
|
+
end
|
87
|
+
unless x[:end_time].blank? || x[:end_time].is_a?(Time)
|
88
|
+
errors.add(:breaks,
|
89
|
+
'Allow only Time format')
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model'
|
4
|
+
require 'active_support/all'
|
5
|
+
require 'forwardable'
|
6
|
+
require 'date'
|
7
|
+
require 'time'
|
8
|
+
|
9
|
+
module PunchTime
|
10
|
+
class TimeRecord
|
11
|
+
include ActiveModel::Validations
|
12
|
+
extend Forwardable
|
13
|
+
|
14
|
+
attr_accessor :start_time
|
15
|
+
attr_accessor :end_time
|
16
|
+
|
17
|
+
validates :start_time, presence: true
|
18
|
+
validates :end_time, presence: true
|
19
|
+
validate :no_format
|
20
|
+
|
21
|
+
MINUTE_SECONDS = 60
|
22
|
+
HOUR_MINUTES = 60
|
23
|
+
DAY_HOURS = 24
|
24
|
+
|
25
|
+
Scale = Struct.new(:rational, :days, :hours, :minutes, :seconds)
|
26
|
+
|
27
|
+
# @param [Hash] config
|
28
|
+
def initialize(&config)
|
29
|
+
@configuration = Configuration.new(&config)
|
30
|
+
end
|
31
|
+
|
32
|
+
# @param [DateTime] start_time
|
33
|
+
# @param [DateTime] end_time
|
34
|
+
def punch(start_time, end_time)
|
35
|
+
self.start_time = start_time
|
36
|
+
self.end_time = end_time
|
37
|
+
raise ArgumentError, errors.messages unless valid?
|
38
|
+
end
|
39
|
+
|
40
|
+
# @return [Scale]
|
41
|
+
def sum_night_work
|
42
|
+
night_works = []
|
43
|
+
night_start_time = @configuration.night[:start_time].strftime('%H:%M:%S')
|
44
|
+
night_end_time = @configuration.night[:end_time].strftime('%H:%M:%S')
|
45
|
+
start_time.to_date.upto(end_time.to_date) do |x|
|
46
|
+
night_start = DateTime.parse("#{x.strftime('%Y-%m-%d')} #{night_start_time}")
|
47
|
+
.change(offset: @configuration.offset)
|
48
|
+
night_end = DateTime.parse("#{x.next_day.strftime('%Y-%m-%d')} #{night_end_time}")
|
49
|
+
.change(offset: @configuration.offset)
|
50
|
+
if end_time > night_end
|
51
|
+
night_works.append(night_end - night_start)
|
52
|
+
elsif (end_time - night_start).positive?
|
53
|
+
night_works.append(end_time - night_start)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
night_work = night_works.inject(:+)
|
57
|
+
convert_humanize(night_work)
|
58
|
+
end
|
59
|
+
|
60
|
+
# @return [Scale]
|
61
|
+
def sum_over_work
|
62
|
+
shift_out_time = @configuration.shift_out_time.strftime('%H:%M:%S')
|
63
|
+
over_work = end_time -
|
64
|
+
DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_out_time}")
|
65
|
+
.change(offset: @configuration.offset) -
|
66
|
+
sum_night_work.rational
|
67
|
+
|
68
|
+
convert_humanize(over_work)
|
69
|
+
end
|
70
|
+
|
71
|
+
# @return [Scale]
|
72
|
+
def sum_work
|
73
|
+
shift_in_time = @configuration.shift_in_time.strftime('%H:%M:%S')
|
74
|
+
shift_in = DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_in_time}")
|
75
|
+
.change(offset: @configuration.offset)
|
76
|
+
work = end_time - shift_in - sum_break
|
77
|
+
convert_humanize(work)
|
78
|
+
end
|
79
|
+
|
80
|
+
# @return [Scale]
|
81
|
+
def sum_tardy
|
82
|
+
shift_in_time = @configuration.shift_in_time.strftime('%H:%M:%S')
|
83
|
+
tardy = start_time -
|
84
|
+
DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_in_time}")
|
85
|
+
.change(offset: @configuration.offset)
|
86
|
+
convert_humanize(tardy)
|
87
|
+
end
|
88
|
+
|
89
|
+
# @return [Boolean]
|
90
|
+
def tardy?
|
91
|
+
shift_in_time = @configuration.shift_in_time.strftime('%H:%M:%S')
|
92
|
+
start_time >
|
93
|
+
DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_in_time}")
|
94
|
+
.change(offset: @configuration.offset)
|
95
|
+
end
|
96
|
+
|
97
|
+
# @return [Boolean]
|
98
|
+
def overtime_work?
|
99
|
+
shift_out_time = @configuration.shift_out_time.strftime('%H:%M:%S')
|
100
|
+
end_time > DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_out_time}")
|
101
|
+
.change(offset: @configuration.offset)
|
102
|
+
end
|
103
|
+
|
104
|
+
# @return [Boolean]
|
105
|
+
def night_overtime_work?
|
106
|
+
night_start_time = @configuration.night[:start_time].strftime('%H:%M:%S')
|
107
|
+
end_time >
|
108
|
+
DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{night_start_time}")
|
109
|
+
.change(offset: @configuration.offset)
|
110
|
+
end
|
111
|
+
|
112
|
+
protected
|
113
|
+
|
114
|
+
attr_reader :configuration
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
# @return [Rational]
|
119
|
+
def sum_break
|
120
|
+
start_day = start_time.strftime('%Y-%m-%d')
|
121
|
+
breaks = @configuration
|
122
|
+
.breaks
|
123
|
+
.select do |x|
|
124
|
+
end_time >
|
125
|
+
DateTime.parse("#{start_day} #{x[:end_time].strftime('%H:%M:%S')}")
|
126
|
+
.change(offset: @configuration.offset)
|
127
|
+
end.select do |x|
|
128
|
+
DateTime.parse("#{start_day} #{x[:start_time].strftime('%H:%M:%S')}")
|
129
|
+
.change(offset: @configuration.offset) >
|
130
|
+
start_time
|
131
|
+
end
|
132
|
+
return Rational(0) if breaks.blank?
|
133
|
+
|
134
|
+
breaks.map { |x| to_datetime(x[:end_time]) - to_datetime(x[:start_time]) }
|
135
|
+
.inject { :+ }
|
136
|
+
end
|
137
|
+
|
138
|
+
def no_format
|
139
|
+
errors.add(:start_time, 'Allow only DateTime format') unless start_time.is_a?(DateTime)
|
140
|
+
errors.add(:end_time, 'Allow only DateTime format') unless end_time.is_a?(DateTime)
|
141
|
+
end
|
142
|
+
|
143
|
+
# @return [Struct]
|
144
|
+
def convert_humanize(value)
|
145
|
+
value ||= Rational(0)
|
146
|
+
Scale.new(value,
|
147
|
+
value.to_i,
|
148
|
+
(value * DAY_HOURS).to_i,
|
149
|
+
(value * DAY_HOURS * HOUR_MINUTES).to_i,
|
150
|
+
(value * DAY_HOURS * HOUR_MINUTES * MINUTE_SECONDS).to_i)
|
151
|
+
end
|
152
|
+
|
153
|
+
# @see https://stackoverflow.com/questions/279769/convert-to-from-datetime-and-time-in-ruby
|
154
|
+
def to_datetime(value)
|
155
|
+
# Convert seconds + microseconds into a fractional number of seconds
|
156
|
+
seconds = value.sec + Rational(value.usec, 10**6)
|
157
|
+
|
158
|
+
# Convert a UTC offset measured in minutes to one measured in a
|
159
|
+
# fraction of a day.
|
160
|
+
offset = Rational(value.utc_offset, 60 * 60 * 24)
|
161
|
+
DateTime.new(value.year, value.month, value.day, value.hour, value.min, seconds, offset)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
data/punch_time.gemspec
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'punch_time/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'punch_time'
|
9
|
+
spec.version = PunchTime::VERSION
|
10
|
+
spec.authors = ['smapira']
|
11
|
+
spec.email = ['smapira@routeflags.com']
|
12
|
+
|
13
|
+
spec.summary = 'Work time calculations using punch clock.'
|
14
|
+
spec.description = 'Work time calculations using punch clock.'
|
15
|
+
spec.homepage = 'https://bitbucket.org/routeflagsinc/punch_time'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
|
22
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
23
|
+
spec.metadata['source_code_uri'] = 'https://bitbucket.org/routeflagsinc/punch_time'
|
24
|
+
spec.metadata['changelog_uri'] = 'https://bitbucket.org/routeflagsinc/punch_time/blob/master/CHANGELOG.md'
|
25
|
+
else
|
26
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
27
|
+
'public gem pushes.'
|
28
|
+
end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = 'exe'
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ['lib']
|
38
|
+
|
39
|
+
spec.add_dependency 'activemodel'
|
40
|
+
spec.add_dependency 'activesupport'
|
41
|
+
spec.add_development_dependency 'bundler'
|
42
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
43
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
44
|
+
spec.add_development_dependency 'rubocop'
|
45
|
+
end
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: punch_time
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- smapira
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: bundler
|
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: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Work time calculations using punch clock.
|
98
|
+
email:
|
99
|
+
- smapira@routeflags.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".circleci/config.yml"
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".rubocop.yml"
|
108
|
+
- ".ruby-version"
|
109
|
+
- CHANGELOG.md
|
110
|
+
- CODE_OF_CONDUCT.md
|
111
|
+
- Gemfile
|
112
|
+
- Gemfile.lock
|
113
|
+
- LICENSE.txt
|
114
|
+
- README.md
|
115
|
+
- Rakefile
|
116
|
+
- bin/console
|
117
|
+
- bin/setup
|
118
|
+
- lib/punch_time.rb
|
119
|
+
- lib/punch_time/configuration.rb
|
120
|
+
- lib/punch_time/error.rb
|
121
|
+
- lib/punch_time/time_record.rb
|
122
|
+
- lib/punch_time/version.rb
|
123
|
+
- punch_time.gemspec
|
124
|
+
- support/body_punch_hand.png
|
125
|
+
homepage: https://bitbucket.org/routeflagsinc/punch_time
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata:
|
129
|
+
homepage_uri: https://bitbucket.org/routeflagsinc/punch_time
|
130
|
+
source_code_uri: https://bitbucket.org/routeflagsinc/punch_time
|
131
|
+
changelog_uri: https://bitbucket.org/routeflagsinc/punch_time/blob/master/CHANGELOG.md
|
132
|
+
post_install_message:
|
133
|
+
rdoc_options: []
|
134
|
+
require_paths:
|
135
|
+
- lib
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
requirements: []
|
147
|
+
rubygems_version: 3.0.3
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: Work time calculations using punch clock.
|
151
|
+
test_files: []
|