active_period 6.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +36 -0
- data/LICENSE.txt +21 -0
- data/README.md +342 -0
- data/Rakefile +10 -0
- data/active_period.gemspec +44 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/locales/en.yml +36 -0
- data/config/locales/fr.yml +59 -0
- data/lib/.DS_Store +0 -0
- data/lib/active_period/belongs_to/month.rb +12 -0
- data/lib/active_period/belongs_to/quarter.rb +12 -0
- data/lib/active_period/belongs_to/week.rb +12 -0
- data/lib/active_period/belongs_to/year.rb +12 -0
- data/lib/active_period/belongs_to.rb +7 -0
- data/lib/active_period/collection/free_period.rb +37 -0
- data/lib/active_period/collection/holiday_period.rb +44 -0
- data/lib/active_period/collection/standard_period.rb +33 -0
- data/lib/active_period/collection.rb +60 -0
- data/lib/active_period/comparable.rb +51 -0
- data/lib/active_period/day.rb +38 -0
- data/lib/active_period/free_period.rb +86 -0
- data/lib/active_period/has_many/days.rb +14 -0
- data/lib/active_period/has_many/holidays.rb +15 -0
- data/lib/active_period/has_many/months.rb +14 -0
- data/lib/active_period/has_many/quarters.rb +14 -0
- data/lib/active_period/has_many/weeks.rb +15 -0
- data/lib/active_period/has_many/years.rb +15 -0
- data/lib/active_period/has_many.rb +7 -0
- data/lib/active_period/holiday.rb +80 -0
- data/lib/active_period/month.rb +37 -0
- data/lib/active_period/period.rb +158 -0
- data/lib/active_period/quarter.rb +42 -0
- data/lib/active_period/standard_period.rb +51 -0
- data/lib/active_period/version.rb +5 -0
- data/lib/active_period/week.rb +41 -0
- data/lib/active_period/year.rb +34 -0
- data/lib/active_period.rb +28 -0
- data/lib/numeric.rb +6 -0
- data/lib/period.rb +63 -0
- data/lib/range.rb +8 -0
- metadata +146 -0
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'standard_period.rb'
|
2
|
+
require_relative 'has_many.rb'
|
3
|
+
require_relative 'has_many/days.rb'
|
4
|
+
require_relative 'has_many/weeks.rb'
|
5
|
+
require_relative 'has_many/months.rb'
|
6
|
+
require_relative 'belongs_to.rb'
|
7
|
+
require_relative 'belongs_to/year.rb'
|
8
|
+
|
9
|
+
module ActivePeriod
|
10
|
+
# @author Lucas Billaudot <billau_l@modulotech.fr>
|
11
|
+
# @note One of the StandardPeriod defined in the gem
|
12
|
+
class Quarter < ActivePeriod::StandardPeriod
|
13
|
+
include ActivePeriod::HasMany::Days
|
14
|
+
include ActivePeriod::HasMany::Weeks
|
15
|
+
include ActivePeriod::HasMany::Months
|
16
|
+
|
17
|
+
include ActivePeriod::BelongsTo::Year
|
18
|
+
|
19
|
+
def strftime(format)
|
20
|
+
format = format.gsub(':quarter', quarter_nb.to_s)
|
21
|
+
from.strftime(format)
|
22
|
+
end
|
23
|
+
|
24
|
+
def quarter_nb
|
25
|
+
@quarter_nb ||= (from.month / 3.0).ceil
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_s
|
29
|
+
i18n
|
30
|
+
end
|
31
|
+
|
32
|
+
def i18n(&block)
|
33
|
+
return yield(from, to) if block.present?
|
34
|
+
|
35
|
+
I18n.t(:default_format,
|
36
|
+
scope: i18n_scope,
|
37
|
+
quarter_nb: quarter_nb,
|
38
|
+
year: from.year)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module ActivePeriod
|
2
|
+
class StandardPeriod < ActivePeriod::Period
|
3
|
+
|
4
|
+
def initialize(object)
|
5
|
+
raise I18n.t(:base_class_id_abstract, scope: %i[active_period standard_period]) if self.class == StandardPeriod
|
6
|
+
time = time_parse(object, I18n.t(:date_is_invalid, scope: %i[active_period standard_period]) )
|
7
|
+
super(time.send("beginning_of_#{_period}")..time.send("end_of_#{_period}"))
|
8
|
+
end
|
9
|
+
|
10
|
+
def next
|
11
|
+
self.class.new(from.send("next_#{_period}"))
|
12
|
+
end
|
13
|
+
alias succ next
|
14
|
+
|
15
|
+
def prev
|
16
|
+
self.class.new(from.send("prev_#{_period}"))
|
17
|
+
end
|
18
|
+
|
19
|
+
def _period
|
20
|
+
self.class._period
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [String] get the name of the standard period
|
24
|
+
def self._period
|
25
|
+
name.split('::').last.downcase
|
26
|
+
end
|
27
|
+
|
28
|
+
# Don't realy return an Integer. ActiveSupport::Duration is a better numeric
|
29
|
+
# representation a in time manipulation context
|
30
|
+
# @return [ActiveSupport::Duration]
|
31
|
+
def to_i
|
32
|
+
1.send(_period)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Shift a period to the past acording to her starting point
|
36
|
+
# @return [self] A new period of the same kind
|
37
|
+
def -(duration)
|
38
|
+
self.class.new(from - duration)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Shift a period to the past acording to her ending point
|
42
|
+
# @return [self] A new period of the same kind
|
43
|
+
def +(duration)
|
44
|
+
self.class.new(to + duration)
|
45
|
+
end
|
46
|
+
|
47
|
+
def i18n_scope
|
48
|
+
[:active_period, :standard_period, _period]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative 'standard_period.rb'
|
2
|
+
require_relative 'has_many.rb'
|
3
|
+
require_relative 'has_many/days.rb'
|
4
|
+
require_relative 'belongs_to.rb'
|
5
|
+
require_relative 'belongs_to/week.rb'
|
6
|
+
require_relative 'belongs_to/month.rb'
|
7
|
+
require_relative 'belongs_to/quarter.rb'
|
8
|
+
require_relative 'belongs_to/year.rb'
|
9
|
+
|
10
|
+
module ActivePeriod
|
11
|
+
# @author Lucas Billaudot <billau_l@modulotech.fr>
|
12
|
+
# @note One of the StandardPeriod defined in the gem
|
13
|
+
class Week < ActivePeriod::StandardPeriod
|
14
|
+
include ActivePeriod::HasMany::Days
|
15
|
+
|
16
|
+
include ActivePeriod::BelongsTo::Month
|
17
|
+
include ActivePeriod::BelongsTo::Quarter
|
18
|
+
include ActivePeriod::BelongsTo::Year
|
19
|
+
|
20
|
+
def strftime(format)
|
21
|
+
from.strftime(format)
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s(format: '%V - %G')
|
25
|
+
strftime(format)
|
26
|
+
end
|
27
|
+
|
28
|
+
def i18n(&block)
|
29
|
+
return yield(from, to) if block.present?
|
30
|
+
|
31
|
+
I18n.t(:default_format,
|
32
|
+
scope: i18n_scope,
|
33
|
+
week: strftime('%V'),
|
34
|
+
year: strftime('%G'))
|
35
|
+
end
|
36
|
+
|
37
|
+
def include_date
|
38
|
+
from + 3.days
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative 'standard_period.rb'
|
2
|
+
require_relative 'has_many.rb'
|
3
|
+
require_relative 'has_many/days.rb'
|
4
|
+
require_relative 'has_many/weeks.rb'
|
5
|
+
require_relative 'has_many/months.rb'
|
6
|
+
require_relative 'has_many/quarters.rb'
|
7
|
+
|
8
|
+
module ActivePeriod
|
9
|
+
# @author Lucas Billaudot <billau_l@modulotech.fr>
|
10
|
+
# @note One of the StandardPeriod defined in the gem
|
11
|
+
class Year < ActivePeriod::StandardPeriod
|
12
|
+
include ActivePeriod::HasMany::Days
|
13
|
+
include ActivePeriod::HasMany::Weeks
|
14
|
+
include ActivePeriod::HasMany::Months
|
15
|
+
include ActivePeriod::HasMany::Quarters
|
16
|
+
|
17
|
+
def strftime(format)
|
18
|
+
from.strftime(format)
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s(format: '%Y')
|
22
|
+
strftime(format)
|
23
|
+
end
|
24
|
+
|
25
|
+
def i18n(&block)
|
26
|
+
return yield(from, to) if block.present?
|
27
|
+
|
28
|
+
I18n.t(:default_format,
|
29
|
+
scope: i18n_scope,
|
30
|
+
year: from.year)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'active_period/version.rb'
|
2
|
+
require 'active_support/all'
|
3
|
+
require 'i18n'
|
4
|
+
|
5
|
+
I18n.load_path << File.expand_path("../config/locales/en.yml", __dir__)
|
6
|
+
I18n.load_path << File.expand_path("../config/locales/fr.yml", __dir__)
|
7
|
+
|
8
|
+
require_relative 'numeric.rb'
|
9
|
+
require_relative 'range.rb'
|
10
|
+
require_relative 'active_period/collection.rb'
|
11
|
+
require_relative 'active_period/collection/free_period.rb'
|
12
|
+
require_relative 'active_period/collection/standard_period.rb'
|
13
|
+
require_relative 'active_period/collection/holiday_period.rb'
|
14
|
+
require_relative 'active_period/comparable.rb'
|
15
|
+
require_relative 'active_period/period.rb'
|
16
|
+
require_relative 'active_period/free_period.rb'
|
17
|
+
require_relative 'active_period/standard_period.rb'
|
18
|
+
require_relative 'active_period/day.rb'
|
19
|
+
require_relative 'active_period/week.rb'
|
20
|
+
require_relative 'active_period/month.rb'
|
21
|
+
require_relative 'active_period/quarter.rb'
|
22
|
+
require_relative 'active_period/year.rb'
|
23
|
+
require_relative 'active_period/holiday.rb'
|
24
|
+
require_relative 'period.rb'
|
25
|
+
|
26
|
+
module ActivePeriod
|
27
|
+
|
28
|
+
end
|
data/lib/numeric.rb
ADDED
data/lib/period.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
module Period
|
2
|
+
def self.new(*args)
|
3
|
+
ActivePeriod::FreePeriod.new(*args)
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.bounded(range)
|
7
|
+
ActivePeriod::FreePeriod.new(range, allow_beginless: false, allow_endless: false)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.env_time
|
11
|
+
(Time.zone || Time)
|
12
|
+
end
|
13
|
+
|
14
|
+
class << self
|
15
|
+
%i[day week month quarter year].each do |period|
|
16
|
+
define_method "last_#{period}" do
|
17
|
+
date = env_time.now.send(period == :day ? 'yesterday' : "last_#{period}")
|
18
|
+
Object.const_get("ActivePeriod::#{period.capitalize}").new(date)
|
19
|
+
end
|
20
|
+
|
21
|
+
define_method "this_#{period}" do
|
22
|
+
Object.const_get("ActivePeriod::#{period.capitalize}").new(env_time.now)
|
23
|
+
end
|
24
|
+
|
25
|
+
define_method "next_#{period}" do
|
26
|
+
date = env_time.now.send(period == :day ? 'tomorrow' : "next_#{period}")
|
27
|
+
Object.const_get("ActivePeriod::#{period.capitalize}").new(date)
|
28
|
+
end
|
29
|
+
|
30
|
+
define_method period.to_s do |range|
|
31
|
+
Object.const_get("ActivePeriod::#{period.capitalize}").new(range)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
alias yesterday last_day
|
36
|
+
alias tomorrow next_day
|
37
|
+
alias today this_day
|
38
|
+
|
39
|
+
def method_missing(method_name, *arguments, &block)
|
40
|
+
super unless method_name.match?(/(last|next)_\d+_(day|week|month|quarter|year)s?(_from_now)?/)
|
41
|
+
last_next, count, klass = method_name.to_s.split('_')
|
42
|
+
klass = klass.singularize
|
43
|
+
|
44
|
+
case last_next
|
45
|
+
when 'last'
|
46
|
+
from = count.to_i.send(klass).ago.send("beginning_of_#{klass}")
|
47
|
+
to = env_time.now
|
48
|
+
to -= 1.send(klass) unless method_name.match?(/from_now$/)
|
49
|
+
to = to.send("end_of_#{klass}")
|
50
|
+
when 'next'
|
51
|
+
from = env_time.now
|
52
|
+
from += 1.send(klass) unless method_name.match?(/from_now$/)
|
53
|
+
from = from.send("beginning_of_#{klass}")
|
54
|
+
to = count.to_i.send(klass).from_now.send("end_of_#{klass}")
|
55
|
+
end
|
56
|
+
self.new(from..to)
|
57
|
+
end
|
58
|
+
|
59
|
+
def respond_to_missing?(method_name, include_private = false)
|
60
|
+
method_name.match?(/(last|next)_\d+_(day|week|month|quarter|year)s?(_from_now)?/) || super
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/range.rb
ADDED
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_period
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 6.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- billau_l
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-05-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: i18n
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.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
|
+
description:
|
70
|
+
email:
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- ".gitignore"
|
76
|
+
- Gemfile
|
77
|
+
- Gemfile.lock
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- active_period.gemspec
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- config/locales/en.yml
|
85
|
+
- config/locales/fr.yml
|
86
|
+
- lib/.DS_Store
|
87
|
+
- lib/active_period.rb
|
88
|
+
- lib/active_period/belongs_to.rb
|
89
|
+
- lib/active_period/belongs_to/month.rb
|
90
|
+
- lib/active_period/belongs_to/quarter.rb
|
91
|
+
- lib/active_period/belongs_to/week.rb
|
92
|
+
- lib/active_period/belongs_to/year.rb
|
93
|
+
- lib/active_period/collection.rb
|
94
|
+
- lib/active_period/collection/free_period.rb
|
95
|
+
- lib/active_period/collection/holiday_period.rb
|
96
|
+
- lib/active_period/collection/standard_period.rb
|
97
|
+
- lib/active_period/comparable.rb
|
98
|
+
- lib/active_period/day.rb
|
99
|
+
- lib/active_period/free_period.rb
|
100
|
+
- lib/active_period/has_many.rb
|
101
|
+
- lib/active_period/has_many/days.rb
|
102
|
+
- lib/active_period/has_many/holidays.rb
|
103
|
+
- lib/active_period/has_many/months.rb
|
104
|
+
- lib/active_period/has_many/quarters.rb
|
105
|
+
- lib/active_period/has_many/weeks.rb
|
106
|
+
- lib/active_period/has_many/years.rb
|
107
|
+
- lib/active_period/holiday.rb
|
108
|
+
- lib/active_period/month.rb
|
109
|
+
- lib/active_period/period.rb
|
110
|
+
- lib/active_period/quarter.rb
|
111
|
+
- lib/active_period/standard_period.rb
|
112
|
+
- lib/active_period/version.rb
|
113
|
+
- lib/active_period/week.rb
|
114
|
+
- lib/active_period/year.rb
|
115
|
+
- lib/numeric.rb
|
116
|
+
- lib/period.rb
|
117
|
+
- lib/range.rb
|
118
|
+
homepage: https://github.com/billaul/period
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata:
|
122
|
+
bug_tracker_uri: https://github.com/billaul/period/issues
|
123
|
+
homepage_uri: https://github.com/billaul/period
|
124
|
+
documentation_uri: https://github.com/billaul/period
|
125
|
+
source_code_uri: https://github.com/billaul/period
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
- config
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '2.7'
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
requirements: []
|
142
|
+
rubygems_version: 3.1.2
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: Manage time ranges without brain damage.
|
146
|
+
test_files: []
|