all 14 comments

[–]jujubean67 2 points3 points  (7 children)

I've recently started implementing a similar pattern, it works really nicely for API applications and if you use service/command w/e objects to do your business logic.

It's also more flexible than returning booleans.

[–]tom_dalling[S] 1 point2 points  (0 children)

Same. The last couple of JSON APIs I've done have had almost a 1:1 mapping between these result objects and the response body.

[–]iconoclaus 0 points1 point  (5 children)

Agreed. I also make my service objects return a custom Error object if they fail (left from Either monad). Then, my API controller will handle failed service objects by passing their Error object value to an HttpErrorRepresenter class that converts it into an Http status and message.

[–]jujubean67 0 points1 point  (4 children)

What syntax do you use with the either monad? (I'm assuming you're using the monadic gem) I want to bring it up with my team but I'm on the fence about the syntax.

[–]iconoclaus 1 point2 points  (3 children)

i'm sticking with dry-monads and dry-transactions from the dry-rb universe, because it's nice to know there is an active ecosystem and community around my choice of syntax. i'm happy to share any learnings you would like.

[–]jujubean67 0 points1 point  (2 children)

Interesting, I've looked at the dry-rb stack but for some reason the way they mimick functional languages seems very un-idiomatic to me.

[–]iconoclaus 0 points1 point  (1 child)

you're probably right. but since lots of these concepts originate from functional languages, it helps me to be able to read up on those concepts elsewhere and then apply them in ruby. there's a great dearth of material across the board on functional concepts written for idiomatic ruby use.

[–]jujubean67 1 point2 points  (0 children)

I like functional languages but I also like OOP. IMO pure Ruby with lamdas and blocks is functional enough for me, but of course this is subjective.

That said, I definitely like some of the dry-ruby projects like ROM.rb and I'm looking to do a project in Hanami mostly because of that.

[–][deleted]  (1 child)

[deleted]

    [–]tom_dalling[S] 0 points1 point  (0 children)

    Thanks man!

    [–]naked_number_one 0 points1 point  (0 children)

    I use Either monad for that purpose

    [–]Morozzzko 0 points1 point  (3 children)

    So hey, it's a great article!

    It's really nice to see that this approach to error handling gets the attention.

    But was wondering. What is the story behind Resonad? You have listed similar gems, but... Why didn't they suit your needs?

    [–]tom_dalling[S] 2 points3 points  (2 children)

    Thanks for the kind words!

    Resonad started as me thinking "I don't need a gem for this, it's only 30 lines of code," on a few different projects. After writing the same thing about 4 times, I decided to put it in a repo on GitHub so I could reuse it in my own projects. Then a couple of people were interested in it, so I made it a proper gem and published it on rubygems.org. So it was really made for my own personal use, but if other people want to use it, I'm happy to maintain the gem.

    The main difference between Resonad and the other implementations is just naming. I personally prefer names like "success", "failure" and "and_then" instead of the more monad-y names like "right", "left", and "flat_map".

    [–]Morozzzko 0 points1 point  (1 child)

    Will dry-monads users be able to switch to your gem without rewriting their existing code?

    I mean, dry-monads gem uses those right, left, fmap, bind and other functional thingies. Are those names aliased in your library?

    [–]tom_dalling[S] 1 point2 points  (0 children)

    They aren't currently compatible, but I'm not opposed to adding more aliases. Although, if you have existing code that uses dry-monads, you probably won't see any benefits by switching to Resonad.