shiftly 0.1.1 → 0.1.3
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 +4 -4
- data/README.md +58 -0
- data/Rakefile +1 -0
- data/lib/generators/shiftly/config_generator.rb +21 -0
- data/lib/shiftly.rb +9 -4
- data/lib/shiftly/config.rb +67 -0
- data/lib/shiftly/core_ext/fixnum.rb +10 -0
- data/lib/shiftly/core_ext/time.rb +25 -0
- data/lib/shiftly/version.rb +1 -1
- data/rails_generators/shiftly_config_generator.rb +8 -0
- data/rails_generators/templates/shiftly.rb +1 -0
- data/rails_generators/templates/shiftly.yml +4 -0
- metadata +9 -3
- data/lib/shiftly/fixnum_ext.rb +0 -10
- data/lib/shiftly/time_ext.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a282b0ea4c0404881bfe8481176414c5b32f6479
|
4
|
+
data.tar.gz: af8be5e5883e58e421c585939550cf56e03c6eee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 914524557cd4dd32c128871d8b4adde210fd44bfb79763445ed93af6b0065dbd3bf05d60e473151ecf908ded5a7239d736da230519b922572c972b2ce5c069e6
|
7
|
+
data.tar.gz: 2e5f55cc8fe3069f66a027eff0366ac8ab00d20fafa046ffb2c4975e16358a219f043c69b85f25683cc0b339030375893a1a9df676390cbb03f30269e3d761d9
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
## Shiftly
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/shiftly)
|
4
|
+
[](https://codeclimate.com/github/logicorg/shiftly)
|
5
|
+
|
6
|
+
Factory shift methods collection by [RubyLogic, PL](http://rubylogic.eu)
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
Add it to your Gemfile with:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'shiftly'
|
14
|
+
```
|
15
|
+
|
16
|
+
Run the bundle command to install it.
|
17
|
+
|
18
|
+
Run generator if needed with:
|
19
|
+
|
20
|
+
```console
|
21
|
+
rails generate shiftly:config
|
22
|
+
```
|
23
|
+
|
24
|
+
Then you have access to the following extensions.
|
25
|
+
|
26
|
+
### Time class exts
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
Time.now.to_factory_date
|
30
|
+
# returns yesterday when it is before 6am and today otherwise
|
31
|
+
```
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Time.now.shift
|
35
|
+
# returns shift number for datetime
|
36
|
+
```
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
Time.now.shift_beg
|
40
|
+
# returns shift beginning datetime
|
41
|
+
```
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
Time.now.shift_end
|
45
|
+
# returns shift end datetime
|
46
|
+
```
|
47
|
+
|
48
|
+
### Fixnum class exts
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
1.next_shift
|
52
|
+
# returns next shift for 1, 2 or 3
|
53
|
+
```
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
1.prev_shift
|
57
|
+
# returns previous shift for 1, 2 or 3
|
58
|
+
```
|
data/Rakefile
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Shiftly
|
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', 'templates')
|
12
|
+
end
|
13
|
+
|
14
|
+
def generate
|
15
|
+
template 'shiftly.rb', File.join('config', 'initializers', 'shiftly.rb')
|
16
|
+
template 'shiftly.yml', File.join('config', 'shiftly.yml')
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/shiftly.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
1
|
+
require 'thread'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/time'
|
4
|
+
require 'time'
|
5
|
+
require 'yaml'
|
6
|
+
require 'business_time'
|
3
7
|
|
4
|
-
|
5
|
-
|
8
|
+
require 'shiftly/config'
|
9
|
+
require 'shiftly/core_ext/time'
|
10
|
+
require 'shiftly/core_ext/fixnum'
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
|
3
|
+
module Shiftly
|
4
|
+
class Config
|
5
|
+
DEFAULT_CONFIG = {
|
6
|
+
day_start_hour: 6,
|
7
|
+
shift_hours: 8,
|
8
|
+
number_of_shifts: 3
|
9
|
+
}
|
10
|
+
|
11
|
+
class << self
|
12
|
+
private
|
13
|
+
|
14
|
+
def config
|
15
|
+
Thread.main[:shiftly_config] ||= default_config
|
16
|
+
end
|
17
|
+
|
18
|
+
def config=(config)
|
19
|
+
Thread.main[:shiftly_config] = config
|
20
|
+
end
|
21
|
+
|
22
|
+
def threadsafe_cattr_accessor(name)
|
23
|
+
define_singleton_method name do
|
24
|
+
config[name]
|
25
|
+
end
|
26
|
+
define_singleton_method "#{name}=" do |value|
|
27
|
+
config[name] = value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
threadsafe_cattr_accessor :day_start_hour
|
33
|
+
|
34
|
+
threadsafe_cattr_accessor :shift_hours
|
35
|
+
|
36
|
+
threadsafe_cattr_accessor :number_of_shifts
|
37
|
+
|
38
|
+
class << self
|
39
|
+
def load(file)
|
40
|
+
reset
|
41
|
+
data = YAML::load(file.respond_to?(:read) ? file : File.open(file))
|
42
|
+
config = (data['shiftly'] || {})
|
43
|
+
|
44
|
+
config_vars = %w(beginning_of_factory_day shift_length number_of_shifts)
|
45
|
+
config_vars.each do |var|
|
46
|
+
send("#{var}=", config[var]) if config[var] && respond_to?("#{var}=")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def default_config
|
51
|
+
deep_dup(DEFAULT_CONFIG)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def reset
|
57
|
+
self.config = default_config
|
58
|
+
end
|
59
|
+
|
60
|
+
def deep_dup(object)
|
61
|
+
Marshal.load(Marshal.dump(object))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
reset
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Time
|
2
|
+
def shift
|
3
|
+
if self.hour < Shiftly::Config.day_start_hour
|
4
|
+
3
|
5
|
+
elsif self.hour < Shiftly::Config.day_start_hour + Shiftly::Config.shift_hours
|
6
|
+
1
|
7
|
+
elsif self.hour < Shiftly::Config.day_start_hour + 2 * Shiftly::Config.shift_hours
|
8
|
+
2
|
9
|
+
else
|
10
|
+
3
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def shift_beg
|
15
|
+
Time.new(self.year, self.month, self.day, Shiftly::Config.day_start_hour + ((shift - 1) * Shiftly::Config.shift_hours), 0, 0)
|
16
|
+
end
|
17
|
+
|
18
|
+
def shift_end
|
19
|
+
shift_beg + Shiftly::Config.shift_hours.hours - 1.second
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_factory_date
|
23
|
+
self.hour < Shiftly::Config.day_start_hour ? 1.business_day.before(self.to_date) : self.to_date
|
24
|
+
end
|
25
|
+
end
|
data/lib/shiftly/version.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
Shiftly::Config.load("#{Rails.root}/config/shiftly.yml")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shiftly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alek Niemczyk
|
@@ -73,13 +73,19 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
+
- lib/generators/shiftly/config_generator.rb
|
76
77
|
- lib/tasks/shiftly_tasks.rake
|
77
78
|
- lib/shiftly.rb
|
78
|
-
- lib/shiftly/
|
79
|
-
- lib/shiftly/
|
79
|
+
- lib/shiftly/core_ext/fixnum.rb
|
80
|
+
- lib/shiftly/core_ext/time.rb
|
81
|
+
- lib/shiftly/config.rb
|
80
82
|
- lib/shiftly/version.rb
|
83
|
+
- rails_generators/templates/shiftly.rb
|
84
|
+
- rails_generators/templates/shiftly.yml
|
85
|
+
- rails_generators/shiftly_config_generator.rb
|
81
86
|
- MIT-LICENSE
|
82
87
|
- Rakefile
|
88
|
+
- README.md
|
83
89
|
homepage: https://github.com/logicorg/shiftly
|
84
90
|
licenses:
|
85
91
|
- MIT
|
data/lib/shiftly/fixnum_ext.rb
DELETED
data/lib/shiftly/time_ext.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'business_time'
|
2
|
-
|
3
|
-
Time.class_eval do
|
4
|
-
|
5
|
-
def shift
|
6
|
-
if self.hour < 6
|
7
|
-
3
|
8
|
-
elsif self.hour < 14
|
9
|
-
1
|
10
|
-
elsif self.hour < 22
|
11
|
-
2
|
12
|
-
else
|
13
|
-
3
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def shift_beg
|
18
|
-
Time.new(self.year, self.month, self.day, (shift * 8) - 2, 0, 0)
|
19
|
-
end
|
20
|
-
|
21
|
-
def shift_end
|
22
|
-
shift_beg + 8.hours - 1.second
|
23
|
-
end
|
24
|
-
|
25
|
-
def to_factory_date
|
26
|
-
self.hour < 6 ? 1.business_day.before(self.to_date) : self.to_date
|
27
|
-
end
|
28
|
-
end
|