-
-
Notifications
You must be signed in to change notification settings - Fork 77
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
to_str
vs to_s
#569
Comments
Hey @joelmoss, this was intentional because For this use-case, you could define def format_object(object)
case object
when ActiveSupport::TimeWithZone
object.strftime("%H:%M on %d %B, %Y")
else
super
end
end This method is designed to be overridden like this with rich object formatters. |
Sorry, I got this wrong. The |
Yeah, I'm talking about attributes. |
Regarding attributes, I can see why this is frustrating, but on balance, I do think it's better to use In the case of The same is true of other rich objects such as One final example is "#<User:0x000000012733b378>" It could have been represented like this: '#<User id: 6, email: "joel@drapper.me", role: "admin", first_name: "Joel", last_name: "Drapper">' Or perhaps as an identifier: 6 Or a global identifier: "gid://example/User/6" Or a URL: "/users/6" Or JSON: {
"id" : 6,
"email" : "joel@drapper.me",
"role" : "admin",
"first_name" : "Joel",
"last_name" : "Drapper"
} Or a name: "Joel Drapper" I don’t think there's a sensible default for many objects besides the currently supported |
That's actually really interesting, as it seems we both have a different view on For example in pretty much every model or class I have, I would define a But the real issue here - at least for me - is that |
There are a few articles on this topic:
Essentially, every Ruby class (with the exception of I think this is the interface we're looking for. With a def template
div(data_date: date)
end
def date = @date.strftime("%H:%M on %d %B, %Y") With your own POROs, it may make sense to just implement UserID = Data.define(:value) do
def to_str = case value
in Integer then value.to_s
in String then value
end
end |
Sorry to bring this up again, but this keeps cropping up over and over for me, and having to implement It's even more frustrating that only While I understand the reasonings behind preferring Is there some sort of compromise we can find here? |
We could allow |
I agree with not using it for implicit text, so I think some sort of convention is needed so as to avoid me having to implement
Could show an example? |
What I mean is this wouldn't call h1 { object } But this would h1 { text object } |
Yeah, but I'm talking about attributes remember 😏 |
🤦♂️ Oh god, I keep doing this. I guess it makes sense to call |
Yeah I like that idea. I'm guessing that would be best implemented at https://github.com/phlex-ruby/phlex/blob/main/lib/phlex/sgml.rb#L422 But that currently calls |
I’d prefer to try |
"upgrading" (the geese are there to signal that it certainly does not feel so) Phlex now hands me
first I went hunting some of my own 'darlings' until I realized - when trying out endpoints not touched since like forever, that this upgrade really has "paid off"
I'm (as you may remember) a huge fan of Phlex - the DSL, the bang and all the fizz - but you might put up a (not so small) sign warning current users:
|
Thanks for the feedback @wdiechmann. All of these changes were made after very careful consideration and documented in the release notes. Turbo stream shouldn't have stopped working though. Could you share a bit more about that in a new discussion thread? We actually managed to get some changes into Also, I’m sorry the upgrade wasted 35 minutes of your time. I’ve probably spent over eighty thousand minutes working on |
I understand that my 'ventilating discomfort' came out all wrong - apologies! I'll hurry to the release notes while I enjoy all the time I've saved using Phlex! If you ever feel adventurous please ping me - I've got 1-2 places for you to have a well-deserved R/R 😄 guess I'll stay with 1.3.2 for the time being though... |
@joelmoss want me to assign this issue to you or shall I take it? Happy either way. |
I'll be very happy to volunteer testing this issue in a 'real' project |
I was planning on grabbing it. Just need to find the time. 😕 |
So it turns out that when trying So we either switch the order to Thoughts? FYI: draft PR #629 |
Fixes #569 Attribute values can be any object that responds to `to_phlex_attribute_value` or `to_str`, falling back to `to_s` if neither is defined. Signed-off-by: Joel Drapper <joel@drapper.me> Co-authored-by: Joel Drapper <joel@drapper.me>
So at https://github.com/phlex-ruby/phlex/blob/main/lib/phlex/sgml.rb#L422
to_str
is used, but not all objects respond to that method. Think usingto_s
is better.As an example,
ActiveSupport::TimeWithZone
does not implementto_str
, but doesto_s
, resulting in an exception when using a ActiveSupport::TimeWithZone instance as an attribute...undefined method 'to_str' for Wed, 23 May 2018 14:59:48.000000000 UTC +00:00:ActiveSupport::TimeWithZone
The text was updated successfully, but these errors were encountered: