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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 69bba3c12f3134fd781d150495e5676de21e992f
4
- data.tar.gz: 1e25adb9b0b968faaefde24416feaf04d7a5b2a3
3
+ metadata.gz: a282b0ea4c0404881bfe8481176414c5b32f6479
4
+ data.tar.gz: af8be5e5883e58e421c585939550cf56e03c6eee
5
5
  SHA512:
6
- metadata.gz: c2a103b68aa547c25e605408538c927fa8ec5d97f2992a1ee4bccbc843d37d0e925dbae8e76a97cfd891087ee431f3cc923a7e64237f3056af7db21ac73a25ca
7
- data.tar.gz: d94be01731188a4bb2ab432dd4425faa1a6ac76cbe58026509de232139d411489301b73c75dd03fedf5cb800140040a81f6021be81cc7cb833cbfd2df5456029
6
+ metadata.gz: 914524557cd4dd32c128871d8b4adde210fd44bfb79763445ed93af6b0065dbd3bf05d60e473151ecf908ded5a7239d736da230519b922572c972b2ce5c069e6
7
+ data.tar.gz: 2e5f55cc8fe3069f66a027eff0366ac8ab00d20fafa046ffb2c4975e16358a219f043c69b85f25683cc0b339030375893a1a9df676390cbb03f30269e3d761d9
@@ -0,0 +1,58 @@
1
+ ## Shiftly
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/shiftly.svg)](http://badge.fury.io/rb/shiftly)
4
+ [![Code Climate](https://codeclimate.com/github/logicorg/shiftly/badges/gpa.svg)](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
@@ -15,6 +15,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
15
15
  end
16
16
 
17
17
 
18
+ task :default => :spec
18
19
 
19
20
 
20
21
  Bundler::GemHelper.install_tasks
@@ -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
@@ -1,5 +1,10 @@
1
- require 'shiftly/time_ext'
2
- require 'shiftly/fixnum_ext'
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
- module Shiftly
5
- end
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,10 @@
1
+ class Fixnum
2
+
3
+ def next_shift
4
+ self == Shiftly::Config.number_of_shifts ? 1 : self + 1
5
+ end
6
+
7
+ def prev_shift
8
+ self == 1 ? Shiftly::Config.number_of_shifts : self - 1
9
+ end
10
+ 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
@@ -1,3 +1,3 @@
1
1
  module Shiftly
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,8 @@
1
+ class ShiftlyConfigGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.template('shiftly.rb', "config/initializers/shiftly.rb")
5
+ m.template('shiftly.yml', "config/shiftly.yml")
6
+ end
7
+ end
8
+ end
@@ -0,0 +1 @@
1
+ Shiftly::Config.load("#{Rails.root}/config/shiftly.yml")
@@ -0,0 +1,4 @@
1
+ shiftly:
2
+ day_start_hour: 6
3
+ shift_hours: 8
4
+ number_of_shifts: 3
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.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/time_ext.rb
79
- - lib/shiftly/fixnum_ext.rb
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
@@ -1,10 +0,0 @@
1
- Fixnum.class_eval do
2
-
3
- def next_shift
4
- self == 3 ? 1 : self + 1
5
- end
6
-
7
- def prev_shift
8
- self == 1 ? 3 : self - 1
9
- end
10
- end
@@ -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