Methods for ActiveSupport::Duration parsing from ISO 8601 and output to it#16917
Methods for ActiveSupport::Duration parsing from ISO 8601 and output to it#16917Envek wants to merge 21 commits into
Conversation
There was a problem hiding this comment.
does this test just repeat part of what's already tested in test_iso8601_output_and_reparsing?
There was a problem hiding this comment.
Yes, it repeat. This test only checks that validations doesn't complain. test_iso8601_output_and_reparsing tries to test some logic (as possible with weird ActiveSupport::Duration).
Should I remove this test case?
Thanks to @egilburg comment in rails/rails#16917 pull request for catching this.
Thanks to @Envek for catching it in rails/rails#16917
There was a problem hiding this comment.
There's enough going on here to extract a separate ISO8601DurationParser
There was a problem hiding this comment.
Where it should be placed? Same file (inside Duration) or separate file? Its location?
|
Fixed some issues:
Method Rebased branch on top of current master due to changes in |
|
Fixed some more remarks. I've noticed that now there is next error occurs when I run activerecord's tests for #16919 on top of this branch: How can I fix it? The reason seems to be in that I'm explicitly requires file with parser in |
|
To avoid circular require warnings (absolutely don't understand why are they appearing such a lot) included ISO8601 parser into file |
|
Did you require duration in the parser and vice versa? That sounds like it could cause the circular requires. |
|
No, I had required parser only from duration (no requires from file with parser at all): commit that caused a lot of warnings. |
|
|
|
I've accounted all the notes and force pushed changes in the same commit. I've tried split initializer on separate methods, but dislike current implementations (they are linked too tight with each other). P.S> in 6f8d9bd only one author specified. Git doesn't support multiple authors (at least with each one's email) |
|
@fxn I did remember seeing commits like that. I assumed it was for GitHub and not the contributors app, but it was the other way. Sorry about that! |
|
Ah, sorry, misunderstood you. Added both authors in |
| parse!(iso8601duration) | ||
| rescue ISO8601Parser::ParsingError | ||
| nil | ||
| end |
There was a problem hiding this comment.
IMO we don't need a method that swallows the parse error.
|
If we're pulling in code under MIT license, we need to include the license and copyright statement. Also, if we're pulling in code, perhaps we should do a gem dep instead? Nice work on this, and patience 😁 |
|
@jeremy there is not a much code left from the original code from ISO8601 gem as I've rewritten parser from regexp to string scanner by @pixeltrix suggestion. As far as I can see for now only duration examples strings inside tests are same with ISO8601. See the original code and tests. Anyway where I should place license and copyright statement if I should? @arnau what do you think? Gem dependency means that we should throw away whole @jeremy please take a look at #22806 while I'm fixing your notes— it's extracted from this pull request. |
|
@jeremy fixed your notes |
|
@Envek We can't replace AS::Duration with ISO8601::Duration, but perhaps we could delegate implementation to it. You tell me :) If you've rewritten the code, then you're no longer using the original code and aren't redistributing it, so you needn't pull in its license. If you are reusing code, include the license+copyright along with the code, in a Ruby comment. |
|
@Envek do whatever makes more sense to you :) |
|
|
||
| # ISO8601 string examples are taken from ISO8601 gem at https://github.com/arnau/ISO8601/blob/b93d466840/spec/iso8601/duration_spec.rb | ||
| # published under the conditions of MIT license: https://github.com/arnau/ISO8601/blob/b93d466840/LICENSE | ||
|
|
There was a problem hiding this comment.
The license is pretty clear about what we need to include here:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
So we need to include the license text in a comment here.
There was a problem hiding this comment.
Included the license text in a comment
|
|
||
| # Creates a new Duration from string formatted according to ISO 8601 Duration. | ||
| # | ||
| # See http://en.wikipedia.org/wiki/ISO_8601#Durations |
There was a problem hiding this comment.
See http://en.wikipedia.org/wiki/ISO_8601#Durations .
| class Duration | ||
| # Parses a string formatted according to ISO 8601 Duration into the hash. | ||
| # | ||
| # See [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601#Durations) for more information. |
There was a problem hiding this comment.
😢 Sorry this is rdoc, my bad. Let me see.
There was a problem hiding this comment.
So rdoc makes sure the link is proper even if it's followed by a '.' Anyway, lets change to -
See {ISO 8601}[http://en.wikipedia.org/wiki/ISO_8601#Durations] for more information.
|
Merged! 04c512d |
[ci skip]
Add #16917 to release notes
This PR add methods to
ActiveSupport::Durationto allow present it in ISO 8601 Duration format and instantiate from it.ISO 8601 Duration format is standartized way to represent duration for interchange, it's already recognized by some database engines (e.g. PostgreSQL) and client side libraries (e.g. plugin for Moment.js durations). So, I think it should be part of
ActiveSupport::Duration.Some parts of code and tests are taken from ISO8601 gem by Arnau Siches (@arnau) and contributors. Many thanks to them.
This PR is required for PostgreSQL
intervaldatatype support (for converting it from and toActiveSupport::Duration) as I've proposed in Google Group here. See #16919. Because of that my parsing allows individual datetime parts to be negative (see PostgreSQL interval output).There is no backward incompatible changes as I want it to be included in upcoming 4.2 release, if it still possible (pleeeaasee!).