-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Global datetime formatter #143
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making these changes! I’ll make the CHANGELOG updates at the time of the next release. Just want @philipqnguyen to get a chance to look at this and I’ll merge if these changes look good to him too 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!!! We really needed this. I have some comments that I would like to be addressed or at least discussed about.
let(:blueprint) do | ||
Class.new(Blueprinter::Base) do | ||
identifier :id | ||
field :first_name, datetime_format: "%m/%d/%Y" | ||
end | ||
end | ||
it('raises a BlueprinterError') { expect{subject}.to raise_error(Blueprinter::BlueprinterError) } | ||
it('does not apply the date format') { should eq(result) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is technically a breaking change. Previously we promised to raise an error if they passed an object with a datetime_format
option that did not respond to #strftime
, but now we are returning the original value.
if value.respond_to?(:strftime) | ||
value = format_datetime(value, options) | ||
end | ||
value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to alter the logic here a bit in order to prevent a breaking change.
- If
options[:datetime_format]
is explicitly passed and!value.respond_to?(:strftime)
, we should raise the originalBlueprinterError
. - Otherwise, follow through with the rest of this logic.
end | ||
|
||
def extract(field_name, object, local_options, options = {}) | ||
extraction = extractor(object, options).extract(field_name, object, local_options, options) | ||
value = options.key?(:datetime_format) ? format_datetime(extraction, options[:datetime_format]) : extraction | ||
|
||
value = @datetime_formatter.extract(extraction, options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to follow the current naming paradigm, how do you feel if we renamed DateTimeFormatter#extract
to DateTimeFormatter#format
? Since #extract
makes more sense with the Extractor
classes, but not so for this Formatter class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for making the changes I requested @ritikesh . Looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better 💯
No description provided.