smart-period 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd9b91f3f8553c817029a58445250a56093cc07ea8eb856c88798d5afd72d210
4
- data.tar.gz: e5a67dcedd0eae2113f489f46a472cf1b8b4be45caacc1d8910acc59f70be2a5
3
+ metadata.gz: f87a8447637e7722692fe6290a0526e1148faf4e25c95189a42ded68bce6e642
4
+ data.tar.gz: 51ff60a6dc37c5cb90bb3b821898f61f402df940c15a92827a598deca6a635c7
5
5
  SHA512:
6
- metadata.gz: ea9f3a5074c20e26c9e8718c91263b993bdef3a9e6b8f9b28015d60c67947a403fad37d2f51322f4053f87ca0590973084461a290515dd592a7eb677253dd599
7
- data.tar.gz: c703f75c931446e9114d06bacb43f0d613e38a5d21b03721d25d2e1e0b0bef1cb74b29cc73f2df1e7170b48fab6a30ddccda40d58a2f23c85e446616e5e3da6f
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
- ### Quick view (TL;DR)
30
+ ## Quick view (TL;DR)
31
31
  ``` ruby
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"
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
- ### Detailed view
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
- # 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])
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
- Period.new('01/01/2000'..'05/01/2000') + 3.day
68
- # is equal to
69
- Period.new('04/01/2000'..'08/01/2000')
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
- # 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)
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
- # 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
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
- Period.month('01/01/2000').next.next.next
104
- # Return the month of April 2020
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
- 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
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
- ### HasMany smaller-periods
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
- # 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
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
- ### BelongsTo greater-period
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
- # 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
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
- ### ActiveRecord
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
- # Get all book published this year
170
- Book.where(published_at: Period.this_year)
169
+ # Get all book published this year
170
+ Book.where(published_at: Period.this_year)
171
171
  ```
172
172
 
173
- ### I18n and to_s
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
- Period.new('01/01/2000'...'01/02/2001').to_s
179
- => "From the 01 January 2000 to the 31 January 2001 included"
180
- I18n.locale = :fr
181
- Period.new('01/01/2000'...'01/02/2001').to_s
182
- => "Du 01 janvier 2000 au 31 janvier 2001 inclus"
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
- ### Bug reports
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
- If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as possible to help us in fixing the potential bug. We also encourage you to help even more by forking and sending us a pull request.
256
+ [![Gem Version](https://fury-badge.herokuapp.com/rb/smart-period.png)](http://badge.fury.io/rb/smart-period)
257
+ [![Code Climate](https://codeclimate.com/github/billaul/period.png)](https://codeclimate.com/github/billaul/period)
258
+ [![Inline docs](http://inch-ci.org/github/billaul/period.png)](http://inch-ci.org/github/billaul/period)
204
259
 
205
- https://github.com/billaul/period/issues
206
260
 
207
261
  ## License
208
262
 
@@ -4,7 +4,6 @@ module Period
4
4
  # @note when include this module provide itterable access to the weeks of
5
5
  # the FreePeriod
6
6
  module Weeks
7
- # TODO, rewrite this to respect ISO %V %G
8
7
  def weeks
9
8
  @weeks ||= []
10
9
  return @weeks if @weeks.present?
@@ -1,5 +1,5 @@
1
1
  module Period
2
2
 
3
- VERSION = '1.0.3'.freeze
3
+ VERSION = '1.0.4'.freeze
4
4
 
5
5
  end
@@ -42,5 +42,4 @@ Gem::Specification.new do |spec|
42
42
  spec.add_runtime_dependency 'i18n', '1.6.0'
43
43
  spec.add_development_dependency 'bundler', '~> 2.0'
44
44
  spec.add_development_dependency 'rake', '~> 10.0'
45
- spec.add_development_dependency 'rails-i18n', '~> 6.0.0'
46
45
  end
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.3
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