workpattern 0.5.0 → 0.7.0
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 +5 -5
- data/.gitignore +9 -19
- data/.travis.yml +17 -12
- data/{CHANGELOG → CHANGELOG.md} +27 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +17 -20
- data/Rakefile +0 -0
- data/lib/workpattern/clock.rb +0 -0
- data/lib/workpattern/constants.rb +72 -0
- data/lib/workpattern/day.rb +268 -0
- data/lib/workpattern/version.rb +1 -1
- data/lib/workpattern/week.rb +135 -298
- data/lib/workpattern/week_pattern.rb +142 -0
- data/lib/workpattern/workpattern.rb +124 -132
- data/lib/workpattern.rb +25 -73
- data/script/console +0 -0
- data/script/destroy +0 -0
- data/script/generate +0 -0
- data/script/txt2html +0 -0
- data/workpattern.gemspec +41 -20
- metadata +20 -36
- data/lib/workpattern/utility/base.rb +0 -27
- data/test/test_clock.rb +0 -29
- data/test/test_helper.rb +0 -2
- data/test/test_week.rb +0 -532
- data/test/test_workpattern.rb +0 -307
- data/test/test_workpattern_module.rb +0 -88
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b4784e020f63bed8a312b50553d245f54fe9ebf5c003555f706bc6d4b74ca447
|
|
4
|
+
data.tar.gz: f401a880126a8dea9113037a7b20d50ba123ec66727d8e1d35d74bf5d7539f32
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a2d348d3aa2a2ecfd1174f1446176b26f7f481af8eafcecdcbf68be8d9594d1e1dac253d93c8d31389a5ba62b23503c80b9ae5b9159df19021fd2bd3f41a9f08
|
|
7
|
+
data.tar.gz: b132fd4596abf3df95b7a955a376e34b7b8796fae5c848014145e91e2565aaeab2c146ce8beb4881852b5d1de989bd98724f352c8cd86a0a68c3fb013c8f7133
|
data/.gitignore
CHANGED
|
@@ -1,20 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/.bundle/
|
|
2
|
+
/.yardoc/
|
|
3
|
+
/_yardoc/
|
|
4
|
+
/coverage/
|
|
5
|
+
/doc/
|
|
6
|
+
/pkg/
|
|
7
|
+
/spec/reports/
|
|
8
|
+
/tmp/
|
|
6
9
|
Gemfile.lock
|
|
7
|
-
|
|
8
|
-
_yardoc
|
|
9
|
-
coverage
|
|
10
|
-
doc/
|
|
11
|
-
lib/bundler/man
|
|
12
|
-
pkg
|
|
13
|
-
rdoc
|
|
14
|
-
spec/reports
|
|
15
|
-
test/tmp
|
|
16
|
-
test/version_tmp
|
|
17
|
-
tmp
|
|
18
|
-
\#*
|
|
19
|
-
.#*
|
|
20
|
-
*.swp
|
|
10
|
+
.compound-engineering/*.local.yaml
|
data/.travis.yml
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
language: ruby
|
|
2
2
|
rvm:
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
- 2.1
|
|
4
|
+
- 2.2
|
|
5
|
+
- 2.3
|
|
6
|
+
- 2.4
|
|
7
|
+
- 2.5
|
|
8
|
+
- 2.6
|
|
9
|
+
- ruby-head
|
|
10
|
+
- jruby-19mode
|
|
11
|
+
- jruby-head
|
|
12
|
+
deploy:
|
|
13
|
+
provider: rubygems
|
|
14
|
+
api_key:
|
|
15
|
+
secure: IXKBqbRf+2aDOpH+g0LpTtIzyagDqjHy6Bo/UYtLqR42CHZUnzre8AvCTvobB+BlHF9W+Igdaf3tzzY+A+XrTf38Iut+zsFI6KnkP7pN8pSMfcpuxZrLi0vd24QAHlHLCHIwsl4WfHj/swIRQGtlhvAV5mcTLW2rhE9/M6pGvRo=
|
|
16
|
+
gem: workpattern
|
|
17
|
+
on:
|
|
18
|
+
tags: true
|
|
19
|
+
repo: callenb/workpattern
|
data/{CHANGELOG → CHANGELOG.md}
RENAMED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
+
## Workpattern v0.7.0 (unreleased) ##
|
|
2
|
+
|
|
3
|
+
* Added `Workpattern#to_h` — serialises a workpattern to a plain Ruby hash (JSON-safe; pattern bitmaps are hex-encoded strings).
|
|
4
|
+
* Added `Workpattern.from_h(hash, overwrite: false)` — reconstructs a workpattern from a hash produced by `to_h`. Requires symbol keys; when deserialising from JSON use `JSON.parse(json, symbolize_names: true)`.
|
|
5
|
+
* Removed `Workpattern.persistence_class=` and `Workpattern.persistence?` — both were silently non-functional in all prior releases due to a `@@persist`/`@@persistence` naming bug; no working integration exists.
|
|
6
|
+
* Fixed `DEFAULT_NAME` undefined constant in `Workpattern::Workpattern.initialize` (pre-existing; only triggered when calling the inner class constructor directly with no arguments).
|
|
7
|
+
* Fixed `Array.new(LAST_DAY_OF_WEEK)` → `Array.new(LAST_DAY_OF_WEEK + 1)` in `Week.initialize` for consistency (pre-existing; Ruby auto-extends arrays so no runtime difference).
|
|
8
|
+
|
|
9
|
+
## Workpattern v0.6.0 ( 25 Feb, 2021) ##
|
|
10
|
+
|
|
11
|
+
I stopped keeping this Changelog file update back when v0.5.0 was realeased on 19 Oct 2016 and now it is 10Feb 2021 and I'm playing catch-up.
|
|
12
|
+
I have created the following set of bullet point changes by going through my commit messages, the quality of which varies greatly.
|
|
13
|
+
A lot of the effort has been on making the code easier to read as it was a real mess.
|
|
14
|
+
Here is a chronological take on what I have been doing.
|
|
15
|
+
|
|
16
|
+
* removed test warnings by surrounding ambiguous negatives with parenthesis
|
|
17
|
+
* I think a bug was fixed by changing #wee_total and #total to just an attr_writer
|
|
18
|
+
* removed some duplicated code
|
|
19
|
+
* replaced a number of literals with constants which I put in their own file
|
|
20
|
+
* made a lot of the code easier to read, such as extracting to a method with a sensible name or renaming existing ones
|
|
21
|
+
* Workpattern is now tested on Ruby 1.9.3, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, ruby-head, jruby-19mode & jruby-head
|
|
22
|
+
* Created a Day class to remove the complexity from Week so Week deals with weeks and Day with a day
|
|
23
|
+
* My Clock class was replaced by Time or Date objects in all tests. Clock only used for internal stuff now.
|
|
24
|
+
* Switched to sorted_set rubygem after the SortedSet class was removed from Ruby in v3.x.
|
|
25
|
+
* Workpattern now works with ruby 3.0.1dev
|
|
26
|
+
* added Contributor Covenant Code of Conduct
|
|
27
|
+
|
|
1
28
|
## Workpattern v0.5.0 ( Oct 19, 2016) ##
|
|
2
29
|
|
|
3
30
|
* Workpattern now handles Timezones. It changes the date into UTC, does the calculation and then changes it back * Barrie Callender
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
|
6
|
+
|
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
|
8
|
+
|
|
9
|
+
## Our Standards
|
|
10
|
+
|
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
|
12
|
+
|
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
|
18
|
+
|
|
19
|
+
Examples of unacceptable behavior include:
|
|
20
|
+
|
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
22
|
+
advances of any kind
|
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
24
|
+
* Public or private harassment
|
|
25
|
+
* Publishing others' private information, such as a physical or email
|
|
26
|
+
address, without their explicit permission
|
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
28
|
+
professional setting
|
|
29
|
+
|
|
30
|
+
## Enforcement Responsibilities
|
|
31
|
+
|
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
|
33
|
+
|
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
|
35
|
+
|
|
36
|
+
## Scope
|
|
37
|
+
|
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
|
39
|
+
|
|
40
|
+
## Enforcement
|
|
41
|
+
|
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at barrie@callenb.org. All complaints will be reviewed and investigated promptly and fairly.
|
|
43
|
+
|
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
|
45
|
+
|
|
46
|
+
## Enforcement Guidelines
|
|
47
|
+
|
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
|
49
|
+
|
|
50
|
+
### 1. Correction
|
|
51
|
+
|
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
|
53
|
+
|
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
|
55
|
+
|
|
56
|
+
### 2. Warning
|
|
57
|
+
|
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
|
59
|
+
|
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
|
61
|
+
|
|
62
|
+
### 3. Temporary Ban
|
|
63
|
+
|
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
|
65
|
+
|
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
|
67
|
+
|
|
68
|
+
### 4. Permanent Ban
|
|
69
|
+
|
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
|
71
|
+
|
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
|
73
|
+
|
|
74
|
+
## Attribution
|
|
75
|
+
|
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
78
|
+
|
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
|
80
|
+
|
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
|
82
|
+
|
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
CHANGED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Barrie Callender
|
|
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
CHANGED
|
@@ -9,7 +9,7 @@ Please use [Github Issues] to report bugs. If you have a question about the lib
|
|
|
9
9
|
|
|
10
10
|
## Getting Started
|
|
11
11
|
|
|
12
|
-
Workpattern is a library with no monkey-patching and
|
|
12
|
+
Workpattern is a library with no monkey-patching and was tested using [Travis](https://travis-ci.org) against the following Ruby versions `1.9.3`, `2.1`, `2.2`, `2.3`, `2.4`, `2.5`, `2.6`, `ruby-head (3.1.0dev)`, `jruby-19mode (9.2.9.0 (2.5.7)` and `jruby-head (9.3.0.0-SNAPSHOT (2.6.5)`.
|
|
13
13
|
|
|
14
14
|
You can install it using:
|
|
15
15
|
```sh
|
|
@@ -93,27 +93,24 @@ Workpattern.delete "My Workpattern"
|
|
|
93
93
|
Workpattern.clear
|
|
94
94
|
```
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
### Serialisation
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
A `Workpattern` can be serialised to a plain Ruby hash and restored later. The hash is JSON-safe — pattern bitmaps are stored as hex strings.
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
``` ruby
|
|
101
|
+
# Serialise
|
|
102
|
+
h = mywp.to_h
|
|
103
|
+
|
|
104
|
+
# Persist however you like (file, database, Redis, …)
|
|
105
|
+
json = JSON.generate(h)
|
|
101
106
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
|
107
|
-
permit persons to whom the Software is furnished to do so, subject to
|
|
108
|
-
the following conditions:
|
|
107
|
+
# Restore — JSON.parse must use symbolize_names: true
|
|
108
|
+
h2 = JSON.parse(json, symbolize_names: true)
|
|
109
|
+
mywp2 = Workpattern.from_h(h2)
|
|
110
|
+
```
|
|
109
111
|
|
|
110
|
-
|
|
111
|
-
included in all copies or substantial portions of the Software.
|
|
112
|
+
If a workpattern with the same name already exists, `from_h` raises `NameError`. Pass `overwrite: true` to replace it:
|
|
112
113
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
117
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
118
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
119
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
114
|
+
``` ruby
|
|
115
|
+
Workpattern.from_h(h, overwrite: true)
|
|
116
|
+
```
|
data/Rakefile
CHANGED
|
File without changes
|
data/lib/workpattern/clock.rb
CHANGED
|
File without changes
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
module Workpattern
|
|
2
|
+
|
|
3
|
+
# default name of a new Workpattern
|
|
4
|
+
DEFAULT_WORKPATTERN_NAME = 'default'.freeze
|
|
5
|
+
|
|
6
|
+
# default base year for a new workpattern
|
|
7
|
+
DEFAULT_BASE_YEAR = 2000
|
|
8
|
+
|
|
9
|
+
# default span of years for a new Workpattern
|
|
10
|
+
DEFAULT_SPAN = 100
|
|
11
|
+
|
|
12
|
+
# 60 seconds in a minute
|
|
13
|
+
MINUTE = 60
|
|
14
|
+
|
|
15
|
+
# 60 minutes in an hour
|
|
16
|
+
HOUR = MINUTE * 60
|
|
17
|
+
|
|
18
|
+
# 24 hours in a day
|
|
19
|
+
HOURS_IN_DAY = 24
|
|
20
|
+
|
|
21
|
+
# Seconds in a day
|
|
22
|
+
DAY = HOUR * HOURS_IN_DAY
|
|
23
|
+
|
|
24
|
+
# 60 minutes in a working hour as binary bit per minute
|
|
25
|
+
WORKING_HOUR = 2**MINUTE - 1
|
|
26
|
+
|
|
27
|
+
# 0 minutes in a working hour as binary bits per minute
|
|
28
|
+
RESTING_HOUR = 0
|
|
29
|
+
|
|
30
|
+
# Earliest or first time in the day
|
|
31
|
+
FIRST_TIME_IN_DAY = Clock.new(0, 0)
|
|
32
|
+
|
|
33
|
+
# Latest or last time in the day
|
|
34
|
+
LAST_TIME_IN_DAY = Clock.new(23, 59)
|
|
35
|
+
|
|
36
|
+
# Flags for calculations
|
|
37
|
+
PREVIOUS_DAY = -1
|
|
38
|
+
SAME_DAY = 0
|
|
39
|
+
NEXT_DAY = 1
|
|
40
|
+
|
|
41
|
+
# Specifies a working pattern
|
|
42
|
+
WORK_TYPE = 1
|
|
43
|
+
|
|
44
|
+
# Specifies a resting pattern
|
|
45
|
+
REST_TYPE = 0
|
|
46
|
+
|
|
47
|
+
# All the days of the week
|
|
48
|
+
SUNDAY=0
|
|
49
|
+
MONDAY=1
|
|
50
|
+
TUESDAY=2
|
|
51
|
+
WEDNESDAY=3
|
|
52
|
+
THURSDAY=4
|
|
53
|
+
FRIDAY=5
|
|
54
|
+
SATURDAY=6
|
|
55
|
+
|
|
56
|
+
# first and last day of week
|
|
57
|
+
FIRST_DAY_OF_WEEK = SUNDAY
|
|
58
|
+
LAST_DAY_OF_WEEK = SATURDAY
|
|
59
|
+
|
|
60
|
+
# Represents the days of the week to be used in applying working
|
|
61
|
+
# and resting patterns.
|
|
62
|
+
# Values exist for each day of the week as well as for the weekend
|
|
63
|
+
# (Saturday and Sunday),
|
|
64
|
+
# the week (Monday to Friday) and all days in the week.
|
|
65
|
+
#
|
|
66
|
+
daynames = { sun: [0], mon: [1], tue: [2], wed: [3],
|
|
67
|
+
thu: [4], fri: [5], sat: [6],
|
|
68
|
+
weekday: [1, 2, 3, 4, 5],
|
|
69
|
+
weekend: [0, 6],
|
|
70
|
+
all: [0, 1, 2, 3, 4, 5, 6] }
|
|
71
|
+
DAYNAMES = daynames.freeze
|
|
72
|
+
end
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
module Workpattern
|
|
2
|
+
|
|
3
|
+
class Day
|
|
4
|
+
|
|
5
|
+
attr_accessor :hours_per_day, :first_working_minute, :last_working_minute
|
|
6
|
+
attr_reader :pattern
|
|
7
|
+
|
|
8
|
+
def pattern=(value)
|
|
9
|
+
@pattern = value
|
|
10
|
+
set_first_and_last_minutes
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def to_h
|
|
14
|
+
{ pattern: @pattern.to_s(16), hours_per_day: @hours_per_day }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.from_h(h)
|
|
18
|
+
unless h[:hours_per_day].is_a?(Integer) && h[:hours_per_day] > 0 && h[:hours_per_day] <= HOURS_IN_DAY
|
|
19
|
+
raise ArgumentError, "from_h: hours_per_day must be an Integer between 1 and #{HOURS_IN_DAY}"
|
|
20
|
+
end
|
|
21
|
+
unless h[:pattern].is_a?(String) && h[:pattern].length <= 400
|
|
22
|
+
raise ArgumentError, "from_h: pattern must be a hex String of at most 400 characters"
|
|
23
|
+
end
|
|
24
|
+
day = allocate
|
|
25
|
+
day.hours_per_day = h[:hours_per_day]
|
|
26
|
+
day.pattern = h[:pattern].to_i(16)
|
|
27
|
+
day
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def initialize(hours_per_day = HOURS_IN_DAY, type = WORK_TYPE)
|
|
31
|
+
@hours_per_day = hours_per_day
|
|
32
|
+
@pattern = initial_day(type)
|
|
33
|
+
set_first_and_last_minutes
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def set_resting(start_time, finish_time)
|
|
37
|
+
mask = resting_mask(start_time, finish_time)
|
|
38
|
+
@pattern = @pattern & mask
|
|
39
|
+
set_first_and_last_minutes
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def set_working(from_time, to_time)
|
|
43
|
+
@pattern = @pattern | working_mask(from_time, to_time)
|
|
44
|
+
set_first_and_last_minutes
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def working_minutes(from_time = FIRST_TIME_IN_DAY, to_time = LAST_TIME_IN_DAY)
|
|
48
|
+
section = @pattern & working_mask(from_time, to_time)
|
|
49
|
+
section.to_s(2).count('1')
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def working?(hour, minute)
|
|
53
|
+
mask = (2**((hour * 60) + minute))
|
|
54
|
+
result = mask & @pattern
|
|
55
|
+
mask == result
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def resting?(hour, minute)
|
|
59
|
+
!working?(hour,minute)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def calc(a_date, a_duration)
|
|
63
|
+
if a_duration == 0
|
|
64
|
+
return a_date, a_duration, SAME_DAY
|
|
65
|
+
else
|
|
66
|
+
return a_duration > 0 ? add(a_date, a_duration) : subtract(a_date, a_duration)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def add(a_date, a_duration)
|
|
73
|
+
minutes_left = working_minutes(a_date)
|
|
74
|
+
if a_duration > minutes_left
|
|
75
|
+
return [a_date, a_duration - minutes_left, NEXT_DAY]
|
|
76
|
+
elsif a_duration < minutes_left
|
|
77
|
+
return add_minutes(a_date, a_duration)
|
|
78
|
+
else
|
|
79
|
+
if working?(LAST_TIME_IN_DAY.hour, LAST_TIME_IN_DAY.min)
|
|
80
|
+
return [a_date, 0, NEXT_DAY]
|
|
81
|
+
else
|
|
82
|
+
return_date = Time.gm(a_date.year, a_date.month, a_date.day, @last_working_minute.hour, @last_working_minute.min) + 60
|
|
83
|
+
return [ return_date, 0, SAME_DAY]
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def add_minutes(a_date, a_duration)
|
|
89
|
+
elapsed_date = a_date + (a_duration * 60) - 60
|
|
90
|
+
|
|
91
|
+
if working_minutes(a_date, elapsed_date) == a_duration
|
|
92
|
+
return [elapsed_date += 60, 0, SAME_DAY]
|
|
93
|
+
else
|
|
94
|
+
begin
|
|
95
|
+
elapsed_date += 60
|
|
96
|
+
end while working_minutes(a_date, elapsed_date) != a_duration
|
|
97
|
+
return [elapsed_date += 60, 0, SAME_DAY]
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def subtract(a_date, a_duration)
|
|
102
|
+
minutes_left = working_minutes(FIRST_TIME_IN_DAY,a_date - 60)
|
|
103
|
+
abs_duration = a_duration.abs
|
|
104
|
+
if abs_duration > minutes_left
|
|
105
|
+
return [a_date, a_duration + minutes_left, PREVIOUS_DAY]
|
|
106
|
+
elsif abs_duration < minutes_left
|
|
107
|
+
return subtract_minutes(a_date, abs_duration)
|
|
108
|
+
else
|
|
109
|
+
return [Time.gm(a_date.year,a_date.month,a_date.day,@first_working_minute.hour,@first_working_minute.min), 0, SAME_DAY]
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def subtract_minutes(a_date, abs_duration)
|
|
114
|
+
elapsed_date = a_date - (abs_duration * 60)
|
|
115
|
+
if working_minutes(elapsed_date, a_date - 60) == abs_duration
|
|
116
|
+
return [elapsed_date, 0, SAME_DAY]
|
|
117
|
+
else
|
|
118
|
+
a_date -= 60
|
|
119
|
+
begin
|
|
120
|
+
elapsed_date -= 60
|
|
121
|
+
end while working_minutes(elapsed_date, a_date) != abs_duration
|
|
122
|
+
return [elapsed_date, 0, SAME_DAY]
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def working_day
|
|
127
|
+
2**((60 * @hours_per_day) +1) - 1
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def initial_day(type = WORK_TYPE)
|
|
131
|
+
|
|
132
|
+
pattern = 2**((60 * @hours_per_day) + 1)
|
|
133
|
+
|
|
134
|
+
if type == WORK_TYPE
|
|
135
|
+
pattern = pattern - 1
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
pattern
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def working_mask(start_time, finish_time)
|
|
142
|
+
|
|
143
|
+
start = minutes_in_time(start_time)
|
|
144
|
+
finish = minutes_in_time(finish_time)
|
|
145
|
+
|
|
146
|
+
mask = initial_day
|
|
147
|
+
|
|
148
|
+
mask = mask - ((2**start) - 1)
|
|
149
|
+
mask & ((2**(finish + 1)) -1)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def resting_mask(start_time, finish_time)
|
|
153
|
+
|
|
154
|
+
start = minutes_in_time(start_time)
|
|
155
|
+
finish_clock = Clock.new(finish_time.hour, finish_time.min + 1)
|
|
156
|
+
|
|
157
|
+
mask = initial_day(REST_TYPE)
|
|
158
|
+
if minutes_in_time(finish_time) != LAST_TIME_IN_DAY.minutes
|
|
159
|
+
mask = mask | working_mask(finish_clock,LAST_TIME_IN_DAY)
|
|
160
|
+
end
|
|
161
|
+
mask | ((2**start) - 1)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def minutes_in_time(a_time)
|
|
165
|
+
(a_time.hour * 60) + a_time.min
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def last_minute
|
|
169
|
+
if working?(LAST_TIME_IN_DAY.hour, LAST_TIME_IN_DAY.min)
|
|
170
|
+
return LAST_TIME_IN_DAY
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
top = minutes_in_time(LAST_TIME_IN_DAY)
|
|
174
|
+
bottom = minutes_in_time(FIRST_TIME_IN_DAY)
|
|
175
|
+
mark = top / 2
|
|
176
|
+
|
|
177
|
+
not_done = true
|
|
178
|
+
while not_done
|
|
179
|
+
|
|
180
|
+
minutes = working_minutes(minutes_to_time(mark), minutes_to_time(top))
|
|
181
|
+
|
|
182
|
+
if minutes > 1
|
|
183
|
+
bottom = mark
|
|
184
|
+
mark = mark + ((top - bottom) / 2)
|
|
185
|
+
|
|
186
|
+
elsif minutes == 0
|
|
187
|
+
top = mark
|
|
188
|
+
mark = mark - (( top - bottom) / 2)
|
|
189
|
+
|
|
190
|
+
elsif minutes == 1 && is_resting(mark)
|
|
191
|
+
bottom = mark
|
|
192
|
+
mark = mark + ((top - bottom) / 2)
|
|
193
|
+
|
|
194
|
+
else
|
|
195
|
+
not_done = false
|
|
196
|
+
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
if mark == bottom #& last_mark != mark
|
|
200
|
+
mark = mark + 1
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
if mark == 1 && top == 1
|
|
204
|
+
mark = 0
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
end
|
|
208
|
+
minutes_to_time(mark)
|
|
209
|
+
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def first_minute
|
|
213
|
+
if working?(FIRST_TIME_IN_DAY.hour, FIRST_TIME_IN_DAY.min)
|
|
214
|
+
return FIRST_TIME_IN_DAY
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
top = minutes_in_time(LAST_TIME_IN_DAY)
|
|
218
|
+
bottom = minutes_in_time(FIRST_TIME_IN_DAY)
|
|
219
|
+
mark = top / 2
|
|
220
|
+
|
|
221
|
+
not_done = true
|
|
222
|
+
while not_done
|
|
223
|
+
|
|
224
|
+
minutes = working_minutes(minutes_to_time(bottom), minutes_to_time(mark))
|
|
225
|
+
if minutes > 1
|
|
226
|
+
top = mark
|
|
227
|
+
mark = mark - ((top - bottom) / 2)
|
|
228
|
+
elsif minutes == 0
|
|
229
|
+
bottom = mark
|
|
230
|
+
mark = mark + (( top - bottom) / 2)
|
|
231
|
+
elsif minutes == 1 && is_resting(mark)
|
|
232
|
+
top = mark
|
|
233
|
+
mark = mark - ((top - bottom) / 2)
|
|
234
|
+
else
|
|
235
|
+
not_done = false
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
if mark == 1 && top == 1
|
|
239
|
+
mark = 0
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
minutes_to_time(mark)
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def minutes_to_time(minutes)
|
|
247
|
+
Time.gm(1963,6,10,minutes / 60, minutes - (minutes / 60 * 60))
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def is_resting(minutes)
|
|
251
|
+
a_time =(minutes_to_time(minutes))
|
|
252
|
+
resting?(a_time.hour, a_time.min)
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def set_first_and_last_minutes
|
|
256
|
+
if working_minutes == 0
|
|
257
|
+
@first_working_minute = nil
|
|
258
|
+
@last_working_minute = nil
|
|
259
|
+
else
|
|
260
|
+
@first_working_minute = first_minute
|
|
261
|
+
@last_working_minute = last_minute
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
end
|
data/lib/workpattern/version.rb
CHANGED