smart-period 1.0.3 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +16 -15
- data/README.md +132 -75
- data/{locales → config/locales}/en.yml +0 -0
- data/{locales → config/locales}/fr.yml +0 -0
- data/lib/period.rb +0 -1
- data/lib/period/free_period.rb +0 -3
- data/lib/period/has_many/weeks.rb +0 -1
- data/lib/period/version.rb +1 -1
- data/period.gemspec +4 -6
- metadata +19 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a3d9947a640e16d37b4388f379169c0001a3ae7ec3f62364ce5649c459815a6
|
4
|
+
data.tar.gz: c4e126393399c839bf7a14f1c5484048d3f37d187a4edfcb86b6c120377c66b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24cf1e781b82c179b9423dc996ab9e125aa673e616814dfcd3492466838fe583f24aa0eebfcdd4e30eda0ee875e4064d7a96b34749b2557a9bf4464879ec9073
|
7
|
+
data.tar.gz: 6b9f7c388255bc3cffd73704d916577faad5e739d764df421ce1785bc46065ff81526d9bba4324aae1f2d928f7f5e3b3f53a2aba351ad23fe4cfe20e72cc6cc5
|
data/Gemfile.lock
CHANGED
@@ -1,26 +1,27 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
period (0.
|
5
|
-
activesupport (
|
6
|
-
i18n (
|
4
|
+
smart-period (1.0.6)
|
5
|
+
activesupport (>= 5, < 7)
|
6
|
+
i18n (~> 1)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activesupport (
|
11
|
+
activesupport (6.1.0)
|
12
12
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
-
i18n (>=
|
14
|
-
minitest (
|
15
|
-
tzinfo (~>
|
16
|
-
|
17
|
-
|
13
|
+
i18n (>= 1.6, < 2)
|
14
|
+
minitest (>= 5.1)
|
15
|
+
tzinfo (~> 2.0)
|
16
|
+
zeitwerk (~> 2.3)
|
17
|
+
concurrent-ruby (1.1.7)
|
18
|
+
i18n (1.8.5)
|
18
19
|
concurrent-ruby (~> 1.0)
|
19
|
-
minitest (5.14.
|
20
|
+
minitest (5.14.2)
|
20
21
|
rake (10.5.0)
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
tzinfo (2.0.4)
|
23
|
+
concurrent-ruby (~> 1.0)
|
24
|
+
zeitwerk (2.4.2)
|
24
25
|
|
25
26
|
PLATFORMS
|
26
27
|
ruby
|
@@ -28,7 +29,7 @@ PLATFORMS
|
|
28
29
|
DEPENDENCIES
|
29
30
|
bundler (~> 2.0)
|
30
31
|
rake (~> 10.0)
|
31
|
-
period!
|
32
|
+
smart-period!
|
32
33
|
|
33
34
|
BUNDLED WITH
|
34
|
-
2.
|
35
|
+
2.1.2
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# Smart-Period
|
1
|
+
# Smart-Period [![Gem Version](https://badge.fury.io/rb/smart-period.svg)](https://badge.fury.io/rb/smart-period) [![Code Climate](https://codeclimate.com/github/billaul/period.svg)](https://codeclimate.com/github/billaul/period) [![Inline docs](http://inch-ci.org/github/billaul/period.svg)](http://inch-ci.org/github/billaul/period)
|
2
2
|
|
3
|
-
Smart-Period aims to simplify Time-range manipulation
|
3
|
+
Smart-Period aims to simplify Time-range manipulation.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,29 +20,35 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
**Smart-Period** was designed to simplify time-range manipulation, specialy with rails and user input
|
23
|
+
**Smart-Period** was designed to simplify time-range manipulation, specialy with rails (~> 5) and user input
|
24
24
|
|
25
25
|
**Warning** :
|
26
26
|
- A time-range take place between two date and it's different from an abstract duration of time
|
27
27
|
- **Smart-Period** is limited at full day of time and will always round the starting and ending to the beginning and the ending of the day
|
28
28
|
|
29
29
|
|
30
|
-
|
30
|
+
## Quick view (TL;DR)
|
31
31
|
``` ruby
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
32
|
+
require 'period'
|
33
|
+
|
34
|
+
# Get all user created today
|
35
|
+
User.where(created_at: Period.today)
|
36
|
+
|
37
|
+
# Get how many weeks there is from the beginning of time ?
|
38
|
+
Period.new('01/01/1970'..Time.now).weeks.count
|
39
|
+
|
40
|
+
# Is Trump still in charge ?
|
41
|
+
Time.now.in? Period.new('20/01/2017'...'20/01/2021')
|
42
|
+
|
43
|
+
# Get the week of an arbitrary date
|
44
|
+
Period.week('24/04/1990')
|
45
|
+
|
46
|
+
# Write a date for me (I18n supported)
|
47
|
+
Period.new('20/01/2017'...'20/01/2021').to_s
|
48
|
+
=> "From the 20 January 2017 to the 19 January 2021 included"
|
43
49
|
```
|
44
50
|
|
45
|
-
|
51
|
+
## Detailed view
|
46
52
|
|
47
53
|
There's two way to create and manipulate a period of time `FreePeriod` and `StandardPeriod`
|
48
54
|
|
@@ -51,76 +57,76 @@ There's two way to create and manipulate a period of time `FreePeriod` and `Stan
|
|
51
57
|
You can declare **FreePeriod** as simply as :
|
52
58
|
|
53
59
|
```ruby
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
# With Date objects
|
61
|
+
Period.new(3.month.ago..Date.today)
|
62
|
+
# or with Strings
|
63
|
+
Period.new('01/01/2000'...'01/02/2000')
|
64
|
+
# or with a mix
|
65
|
+
Period.new('01/01/2000'..1.week.ago)
|
66
|
+
# or in a rails Controller with params
|
67
|
+
Period.new(params[:start_date]..params[:end_date])
|
62
68
|
```
|
63
69
|
|
64
70
|
**FreePeriod** can be manipulated with `+` and `-`
|
65
71
|
Doing so will move the start **and** the end of the period
|
66
72
|
```ruby
|
67
|
-
|
68
|
-
|
69
|
-
|
73
|
+
Period.new('01/01/2000'..'05/01/2000') + 3.day
|
74
|
+
# is equal to
|
75
|
+
Period.new('04/01/2000'..'08/01/2000')
|
70
76
|
```
|
71
77
|
|
72
78
|
### Standard Period of time
|
73
79
|
|
74
|
-
Using **StandardPeriod** you are limited to strictly bordered periods of time
|
80
|
+
Using **StandardPeriod** you are limited to strictly bordered periods of time
|
75
81
|
These periods are `day`, `week`, `month`, `quarter` and `year`
|
76
82
|
|
77
83
|
```ruby
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
84
|
+
# To get the week, 42th day ago
|
85
|
+
Period.week(42.day.ago)
|
86
|
+
# To get the first month of 2020
|
87
|
+
Period.month('01/01/2020')
|
88
|
+
# or if you like it verbious
|
89
|
+
Period::Month.new('01/01/2020')
|
90
|
+
# or if you need the current week
|
91
|
+
Period.week(Time.now)
|
86
92
|
```
|
87
93
|
|
88
94
|
**Note** : If you ask for a `month`, `quarter` of `year`, the day part of your param doesn't matter `01/01/2020` give the same result as `14/01/2020` or `29/01/2020`
|
89
95
|
|
90
96
|
**StandardPeriod** can be manipulated with `+` and `-` and will always return a **StandardPeriod** of the same type
|
91
97
|
```ruby
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
98
|
+
# Subtraction are made from the start of the period
|
99
|
+
Period.month('10/02/2000') - 1.day
|
100
|
+
# Return the previous month
|
101
|
+
# Addition are made from the end
|
102
|
+
Period.month('10/02/2000') + 1.day
|
103
|
+
# Return the next month
|
104
|
+
Period.week('10/02/2000') + 67.day
|
105
|
+
# Return a week
|
100
106
|
```
|
101
107
|
**StandardPeriod** also respond to `.next` and `.prev`
|
102
108
|
```ruby
|
103
|
-
|
104
|
-
|
109
|
+
Period.month('01/01/2000').next.next.next
|
110
|
+
# Return the month of April 2020
|
105
111
|
```
|
106
112
|
|
107
113
|
You can quickly access close period of time with `.(last|this|next)_(day|week|month|quarter|year)` and `.yesterday` `.today` `.tomorrow`
|
108
114
|
|
109
115
|
```ruby
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
116
|
+
Period.this_week
|
117
|
+
# Same as Period.week(Time.now) but shorter
|
118
|
+
Period.next_month
|
119
|
+
# Return the next month
|
120
|
+
Period.last_year
|
121
|
+
# Return the last year
|
122
|
+
Period.today
|
123
|
+
# No comment
|
118
124
|
```
|
119
125
|
|
120
|
-
|
126
|
+
## HasMany
|
121
127
|
|
122
128
|
**FreePeriod** and some **StandardPeriod** respond to `.days`, `.weeks`, `.months`, `.quarters` and `.years`
|
123
|
-
These methods return an array of **StandardPeriod** who are
|
129
|
+
These methods return an array of **StandardPeriod** who are overlapping the current period
|
124
130
|
|
125
131
|
| HasMany -> [\<StandardPeriod>] | .days | .weeks | .months | .quarters | .years |
|
126
132
|
|-------------------------------|:----:|:-----:|:------:|:--------:|:-----:|
|
@@ -133,13 +139,15 @@ These methods return an array of **StandardPeriod** who are include inside the c
|
|
133
139
|
|
134
140
|
#### Example
|
135
141
|
```ruby
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
142
|
+
# Get how many weeks there is from the beginning of time ?
|
143
|
+
Period.new('01/01/1970'..Time.now).weeks.count
|
144
|
+
# How many day in the current quarter
|
145
|
+
Period.this_quarter.days.count
|
146
|
+
# Get all the quarters overlapping a Period of time
|
147
|
+
Period.new(...).quarters
|
140
148
|
```
|
141
149
|
|
142
|
-
|
150
|
+
## BelongsTo
|
143
151
|
|
144
152
|
**StandardPeriod** respond to `.day`, `.week`, `.month`, `.quarter` and `.year`
|
145
153
|
These methods return a **StandardPeriod** who include the current period
|
@@ -157,29 +165,61 @@ These methods return a **StandardPeriod** who include the current period
|
|
157
165
|
#### Example with BelongTo and HasMany
|
158
166
|
|
159
167
|
```ruby
|
160
|
-
|
161
|
-
|
168
|
+
# Get the first day, of the last week, of the second month, of the current year
|
169
|
+
Period.this_year.months.second.weeks.last.days.first
|
162
170
|
```
|
163
171
|
|
164
|
-
|
172
|
+
## ActiveRecord
|
165
173
|
|
166
174
|
As **Period** inherite from **Range**, you can natively use them in **ActiveRecord** query
|
167
175
|
|
168
176
|
```ruby
|
169
|
-
|
170
|
-
|
177
|
+
# Get all book published this year
|
178
|
+
Book.where(published_at: Period.this_year)
|
171
179
|
```
|
172
180
|
|
173
|
-
|
181
|
+
## Rails Controller
|
182
|
+
|
183
|
+
In a Controller, use the error handling to validate the date for you
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
class BookController < ApplicationController
|
187
|
+
def between # match via GET and POST
|
188
|
+
# Default value for the range in GET context
|
189
|
+
params[:from] ||= 1.month.ago
|
190
|
+
params[:to] ||= Time.now
|
191
|
+
|
192
|
+
begin
|
193
|
+
# Retrieve books from the DB
|
194
|
+
@books = Book.where(published: Period.new(params[:from]..params[:to]))
|
195
|
+
rescue ArgumentError => e
|
196
|
+
# Period will handle mis-formatted date and incoherent period
|
197
|
+
# I18n is support for errors messages
|
198
|
+
flash[:alert] = e.message
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
```
|
203
|
+
|
204
|
+
## I18n and to_s
|
174
205
|
|
175
206
|
I18n is supported for `en` and `fr`
|
176
207
|
|
177
208
|
```ruby
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
209
|
+
Period.new('01/01/2000'...'01/02/2001').to_s
|
210
|
+
=> "From the 01 January 2000 to the 31 January 2001 included"
|
211
|
+
I18n.locale = :fr
|
212
|
+
Period.new('01/01/2000'...'01/02/2001').to_s
|
213
|
+
=> "Du 01 janvier 2000 au 31 janvier 2001 inclus"
|
214
|
+
```
|
215
|
+
Errors are also supported
|
216
|
+
```ruby
|
217
|
+
Period.new 'Foo'..'Bar'
|
218
|
+
#=> ArgumentError (The start date is invalid)
|
219
|
+
Period.new '01/02/3030'..'Bar'
|
220
|
+
#=> ArgumentError (The end date is invalid)
|
221
|
+
Period.new '01/02/3030'..'01/01/2020'
|
222
|
+
#=> ArgumentError (The start date is greater than the end date)
|
183
223
|
```
|
184
224
|
|
185
225
|
See `locales/en.yml` to implement your language support
|
@@ -198,11 +238,28 @@ For a FreePeriod or if you need to print the start and the end of your period di
|
|
198
238
|
end
|
199
239
|
```
|
200
240
|
|
201
|
-
|
241
|
+
## The tricky case of Weeks
|
242
|
+
|
243
|
+
Weeks are implemented following the [ISO 8601](https://en.wikipedia.org/wiki/ISO_week_date)
|
244
|
+
So `Period.this_month.weeks.first` doesn't necessarily include the first days of the month
|
245
|
+
|
246
|
+
## TimeZone
|
247
|
+
|
248
|
+
Time zone are supported, you have nothing to do
|
249
|
+
If you change the global `Time.zone` of your app, you have nothing to do
|
250
|
+
If your Period [begin in a time zone and end in another](https://en.wikipedia.org/wiki/Daylight_saving_time), you have nothing to do
|
251
|
+
|
252
|
+
## Bug reports
|
253
|
+
|
254
|
+
If you discover any bugs, feel free to create an [issue on GitHub](https://github.com/billaul/period/issues)
|
255
|
+
Please add as much information as possible to help us in fixing the potential bug
|
256
|
+
We also encourage you to help even more by forking and sending us a pull request
|
257
|
+
|
258
|
+
No issues will be addressed outside GitHub
|
202
259
|
|
203
|
-
|
260
|
+
## Maintainer
|
204
261
|
|
205
|
-
https://github.com/billaul
|
262
|
+
* Myself (https://github.com/billaul)
|
206
263
|
|
207
264
|
## License
|
208
265
|
|
File without changes
|
File without changes
|
data/lib/period.rb
CHANGED
data/lib/period/free_period.rb
CHANGED
data/lib/period/version.rb
CHANGED
data/period.gemspec
CHANGED
@@ -6,7 +6,6 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.name = 'smart-period'
|
7
7
|
spec.version = Period::VERSION
|
8
8
|
spec.authors = ['billau_l']
|
9
|
-
spec.email = ['billau_l@modulotech.fr']
|
10
9
|
|
11
10
|
spec.summary = 'Manage time ranges without brain damage.'
|
12
11
|
# spec.description = "Period.new('01/01/2020'..Time.now)"
|
@@ -35,12 +34,11 @@ Gem::Specification.new do |spec|
|
|
35
34
|
end
|
36
35
|
spec.bindir = 'exe'
|
37
36
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
38
|
-
spec.require_paths = %w[lib
|
37
|
+
spec.require_paths = %w[lib]
|
39
38
|
|
40
|
-
spec.required_ruby_version = '
|
41
|
-
spec.add_runtime_dependency 'activesupport', '5
|
42
|
-
spec.add_runtime_dependency 'i18n', '1
|
39
|
+
spec.required_ruby_version = '>= 2.5'
|
40
|
+
spec.add_runtime_dependency 'activesupport', '>= 5', '< 7'
|
41
|
+
spec.add_runtime_dependency 'i18n', '~> 1'
|
43
42
|
spec.add_development_dependency 'bundler', '~> 2.0'
|
44
43
|
spec.add_development_dependency 'rake', '~> 10.0'
|
45
|
-
spec.add_development_dependency 'rails-i18n', '~> 6.0.0'
|
46
44
|
end
|
metadata
CHANGED
@@ -1,43 +1,49 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart-period
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- billau_l
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 5
|
19
|
+
version: '5'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '7'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- -
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '5'
|
30
|
+
- - "<"
|
25
31
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
32
|
+
version: '7'
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: i18n
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
30
36
|
requirements:
|
31
|
-
- -
|
37
|
+
- - "~>"
|
32
38
|
- !ruby/object:Gem::Version
|
33
|
-
version: 1
|
39
|
+
version: '1'
|
34
40
|
type: :runtime
|
35
41
|
prerelease: false
|
36
42
|
version_requirements: !ruby/object:Gem::Requirement
|
37
43
|
requirements:
|
38
|
-
- -
|
44
|
+
- - "~>"
|
39
45
|
- !ruby/object:Gem::Version
|
40
|
-
version: 1
|
46
|
+
version: '1'
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
48
|
name: bundler
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,23 +72,8 @@ dependencies:
|
|
66
72
|
- - "~>"
|
67
73
|
- !ruby/object:Gem::Version
|
68
74
|
version: '10.0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rails-i18n
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: 6.0.0
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: 6.0.0
|
83
75
|
description:
|
84
76
|
email:
|
85
|
-
- billau_l@modulotech.fr
|
86
77
|
executables: []
|
87
78
|
extensions: []
|
88
79
|
extra_rdoc_files: []
|
@@ -95,6 +86,8 @@ files:
|
|
95
86
|
- Rakefile
|
96
87
|
- bin/console
|
97
88
|
- bin/setup
|
89
|
+
- config/locales/en.yml
|
90
|
+
- config/locales/fr.yml
|
98
91
|
- lib/.DS_Store
|
99
92
|
- lib/numeric.rb
|
100
93
|
- lib/period.rb
|
@@ -117,8 +110,6 @@ files:
|
|
117
110
|
- lib/period/version.rb
|
118
111
|
- lib/period/week.rb
|
119
112
|
- lib/period/year.rb
|
120
|
-
- locales/en.yml
|
121
|
-
- locales/fr.yml
|
122
113
|
- period.gemspec
|
123
114
|
homepage: https://github.com/billaul/period
|
124
115
|
licenses:
|
@@ -132,10 +123,9 @@ post_install_message:
|
|
132
123
|
rdoc_options: []
|
133
124
|
require_paths:
|
134
125
|
- lib
|
135
|
-
- locals
|
136
126
|
required_ruby_version: !ruby/object:Gem::Requirement
|
137
127
|
requirements:
|
138
|
-
- - "
|
128
|
+
- - ">="
|
139
129
|
- !ruby/object:Gem::Version
|
140
130
|
version: '2.5'
|
141
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|