smart-period 1.0.3 → 1.0.4
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 +124 -70
- data/lib/period/has_many/weeks.rb +0 -1
- data/lib/period/version.rb +1 -1
- data/period.gemspec +0 -1
- metadata +1 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f87a8447637e7722692fe6290a0526e1148faf4e25c95189a42ded68bce6e642
|
4
|
+
data.tar.gz: 51ff60a6dc37c5cb90bb3b821898f61f402df940c15a92827a598deca6a635c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6714f0cf758a3d3a898f567014b736babc4ca5f20ea2a47aa21c4cfa7814303dc609cfae6b7cef8ee7ad1b8f62e32b22a1cc719151443143d1c168994f7a017
|
7
|
+
data.tar.gz: 0b83c1d0f4975ec8fcc90d604b3204ffbe0add82ca8d1c44487dd956d499c3d641936c0588404518ca399a36bd25f82fc6494001b5e0c67bde7c0833ef6ce21f
|
data/README.md
CHANGED
@@ -27,22 +27,22 @@ Or install it yourself as:
|
|
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
|
+
# Get all user created today
|
33
|
+
User.where(created_at: Period.today)
|
34
|
+
# Get how many weeks there is from the beginning of time ?
|
35
|
+
Period.new('01/01/1970'..Time.now).weeks.count
|
36
|
+
# Is Trump still in charge ?
|
37
|
+
Time.now.in? Period.new('20/01/2017'...'20/01/2021')
|
38
|
+
# Get the week of an arbitrary date
|
39
|
+
Period.week('24/04/1990')
|
40
|
+
# Write a date for me (I18n supported)
|
41
|
+
Period.new('20/01/2017'...'20/01/2021').to_s
|
42
|
+
=> "From the 20 January 2017 to the 19 January 2021 included"
|
43
43
|
```
|
44
44
|
|
45
|
-
|
45
|
+
## Detailed view
|
46
46
|
|
47
47
|
There's two way to create and manipulate a period of time `FreePeriod` and `StandardPeriod`
|
48
48
|
|
@@ -51,22 +51,22 @@ There's two way to create and manipulate a period of time `FreePeriod` and `Stan
|
|
51
51
|
You can declare **FreePeriod** as simply as :
|
52
52
|
|
53
53
|
```ruby
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
54
|
+
# With Date objects
|
55
|
+
Period.new(3.month.ago..Date.today)
|
56
|
+
# or with Strings
|
57
|
+
Period.new('01/01/2000'...'01/02/2000')
|
58
|
+
# or with a mix
|
59
|
+
Period.new('01/01/2000'..1.week.ago)
|
60
|
+
# or in a rails Controller with params
|
61
|
+
Period.new(params[:start_date]..params[:end_date])
|
62
62
|
```
|
63
63
|
|
64
64
|
**FreePeriod** can be manipulated with `+` and `-`
|
65
65
|
Doing so will move the start **and** the end of the period
|
66
66
|
```ruby
|
67
|
-
|
68
|
-
|
69
|
-
|
67
|
+
Period.new('01/01/2000'..'05/01/2000') + 3.day
|
68
|
+
# is equal to
|
69
|
+
Period.new('04/01/2000'..'08/01/2000')
|
70
70
|
```
|
71
71
|
|
72
72
|
### Standard Period of time
|
@@ -75,49 +75,49 @@ Using **StandardPeriod** you are limited to strictly bordered periods of time
|
|
75
75
|
These periods are `day`, `week`, `month`, `quarter` and `year`
|
76
76
|
|
77
77
|
```ruby
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
78
|
+
# To get the week, 42th day ago
|
79
|
+
Period.week(42.day.ago)
|
80
|
+
# To get the first month of 2020
|
81
|
+
Period.month('01/01/2020')
|
82
|
+
# or if you like it verbious
|
83
|
+
Period::Month.new('01/01/2020')
|
84
|
+
# or if you need the current week
|
85
|
+
Period.week(Time.now)
|
86
86
|
```
|
87
87
|
|
88
88
|
**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
89
|
|
90
90
|
**StandardPeriod** can be manipulated with `+` and `-` and will always return a **StandardPeriod** of the same type
|
91
91
|
```ruby
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
92
|
+
# Subtraction are made from the start of the period
|
93
|
+
Period.month('10/02/2000') - 1.day
|
94
|
+
# Return the previous month
|
95
|
+
# Addition are made from the end
|
96
|
+
Period.month('10/02/2000') + 1.day
|
97
|
+
# Return the next month
|
98
|
+
Period.week('10/02/2000') + 67.day
|
99
|
+
# Return a week
|
100
100
|
```
|
101
101
|
**StandardPeriod** also respond to `.next` and `.prev`
|
102
102
|
```ruby
|
103
|
-
|
104
|
-
|
103
|
+
Period.month('01/01/2000').next.next.next
|
104
|
+
# Return the month of April 2020
|
105
105
|
```
|
106
106
|
|
107
107
|
You can quickly access close period of time with `.(last|this|next)_(day|week|month|quarter|year)` and `.yesterday` `.today` `.tomorrow`
|
108
108
|
|
109
109
|
```ruby
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
110
|
+
Period.this_week
|
111
|
+
# Same as Period.week(Time.now) but shorter
|
112
|
+
Period.next_month
|
113
|
+
# Return the next month
|
114
|
+
Period.last_year
|
115
|
+
# Return the last year
|
116
|
+
Period.today
|
117
|
+
# No comment
|
118
118
|
```
|
119
119
|
|
120
|
-
|
120
|
+
## HasMany smaller-periods
|
121
121
|
|
122
122
|
**FreePeriod** and some **StandardPeriod** respond to `.days`, `.weeks`, `.months`, `.quarters` and `.years`
|
123
123
|
These methods return an array of **StandardPeriod** who are include inside the current period
|
@@ -133,13 +133,13 @@ These methods return an array of **StandardPeriod** who are include inside the c
|
|
133
133
|
|
134
134
|
#### Example
|
135
135
|
```ruby
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
136
|
+
# Get how many weeks there is from the beginning of time ?
|
137
|
+
Period.new('01/01/1970'..Time.now).weeks.count
|
138
|
+
# How many day in the current quarter
|
139
|
+
Period.this_quarter.days.count
|
140
140
|
```
|
141
141
|
|
142
|
-
|
142
|
+
## BelongsTo greater-period
|
143
143
|
|
144
144
|
**StandardPeriod** respond to `.day`, `.week`, `.month`, `.quarter` and `.year`
|
145
145
|
These methods return a **StandardPeriod** who include the current period
|
@@ -157,29 +157,61 @@ These methods return a **StandardPeriod** who include the current period
|
|
157
157
|
#### Example with BelongTo and HasMany
|
158
158
|
|
159
159
|
```ruby
|
160
|
-
|
161
|
-
|
160
|
+
# Get the first day, of the last week, of the second month, of the current year
|
161
|
+
Period.this_year.months.second.weeks.last.days.first
|
162
162
|
```
|
163
163
|
|
164
|
-
|
164
|
+
## ActiveRecord
|
165
165
|
|
166
166
|
As **Period** inherite from **Range**, you can natively use them in **ActiveRecord** query
|
167
167
|
|
168
168
|
```ruby
|
169
|
-
|
170
|
-
|
169
|
+
# Get all book published this year
|
170
|
+
Book.where(published_at: Period.this_year)
|
171
171
|
```
|
172
172
|
|
173
|
-
|
173
|
+
## Rails Controller
|
174
|
+
|
175
|
+
In a Controller, use the error handling to validate the date for you
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
class BookController < ApplicationController
|
179
|
+
def between # match via GET and POST
|
180
|
+
# Default value for the range in GET context
|
181
|
+
params[:from] ||= 1.month.ago
|
182
|
+
params[:to] ||= Time.now
|
183
|
+
|
184
|
+
begin
|
185
|
+
# Retrieve books from the DB
|
186
|
+
@books = Book.where(published: Period.new(params[:from]..params[:to]))
|
187
|
+
rescue ArgumentError => e
|
188
|
+
# Period will handle mis-formatted date and incoherent period
|
189
|
+
# I18n is support for errors messages
|
190
|
+
flash[:alert] = e.message
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
```
|
195
|
+
|
196
|
+
## I18n and to_s
|
174
197
|
|
175
198
|
I18n is supported for `en` and `fr`
|
176
199
|
|
177
200
|
```ruby
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
201
|
+
Period.new('01/01/2000'...'01/02/2001').to_s
|
202
|
+
=> "From the 01 January 2000 to the 31 January 2001 included"
|
203
|
+
I18n.locale = :fr
|
204
|
+
Period.new('01/01/2000'...'01/02/2001').to_s
|
205
|
+
=> "Du 01 janvier 2000 au 31 janvier 2001 inclus"
|
206
|
+
```
|
207
|
+
Errors are also supported
|
208
|
+
```ruby
|
209
|
+
Period.new 'Foo'..'Bar'
|
210
|
+
#=> ArgumentError (The start date is invalid)
|
211
|
+
Period.new '01/02/3030'..'Bar'
|
212
|
+
#=> ArgumentError (The end date is invalid)
|
213
|
+
Period.new '01/02/3030'..'01/01/2020'
|
214
|
+
#=> ArgumentError (The start date is greater than the end date)
|
183
215
|
```
|
184
216
|
|
185
217
|
See `locales/en.yml` to implement your language support
|
@@ -198,11 +230,33 @@ For a FreePeriod or if you need to print the start and the end of your period di
|
|
198
230
|
end
|
199
231
|
```
|
200
232
|
|
201
|
-
|
233
|
+
## The tricky case of Weeks
|
234
|
+
|
235
|
+
Weeks are implemented following the [ISO 8601](https://en.wikipedia.org/wiki/ISO_week_date)
|
236
|
+
So `Period.this_month.weeks.first` doesn't necessarily include the first days of the month
|
237
|
+
|
238
|
+
## TimeZone
|
239
|
+
|
240
|
+
Time zone are supported, you have nothing to do
|
241
|
+
If you change the global `Time.zone` of your app, you have nothing to do
|
242
|
+
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
|
243
|
+
|
244
|
+
## Bug reports
|
245
|
+
|
246
|
+
If you discover any bugs, feel free to create an [issue on GitHub](https://github.com/billaul/period/issues)
|
247
|
+
Please add as much information as possible to help us in fixing the potential bug
|
248
|
+
We also encourage you to help even more by forking and sending us a pull request
|
249
|
+
|
250
|
+
No issues will be addressed outside GitHub
|
251
|
+
|
252
|
+
## Maintainer
|
253
|
+
|
254
|
+
* Myself (https://github.com/billaul)
|
202
255
|
|
203
|
-
|
256
|
+
[](http://badge.fury.io/rb/smart-period)
|
257
|
+
[](https://codeclimate.com/github/billaul/period)
|
258
|
+
[](http://inch-ci.org/github/billaul/period)
|
204
259
|
|
205
|
-
https://github.com/billaul/period/issues
|
206
260
|
|
207
261
|
## License
|
208
262
|
|
data/lib/period/version.rb
CHANGED
data/period.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- billau_l
|
@@ -66,20 +66,6 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
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
69
|
description:
|
84
70
|
email:
|
85
71
|
- billau_l@modulotech.fr
|