Giter Site home page Giter Site logo

Comments (3)

dkubb avatar dkubb commented on July 22, 2024

@emmanuel it looks like you added Virtus::Attribute#value_coerced? about a month ago. What's it's purpose? It doesn't look like anything in Virtus is using it.

It's marked as part of the private API, which means nothing outside of Virtus should be relying on it. If something is, then we should probably update it to have @api private in the docs.

Also maybe this is a case where the actual implementation of testing whether a value was properly coerced or not should be pushed off to the coercion libs. Worst case we keep the Virtus::Attribute#value_coerced? method, but just have it delegate all the work to the coercion libs. Best case we can get rid a method that nothing uses.

from virtus.

solnic avatar solnic commented on July 22, 2024

AFAIR #value_coerced? was added as a convenient method for the case mentioned in the issue.

Regarding custom attributes - I used this example to show that you can implement coercion in the attribute class (kind of like a DM1-style) but the preferred way of handling coercions is to use Virtus::Coercion classes, either extend the existing ones or add new.

The philosophy behind the coercion system is very simple - for every kind of object that may "come in" we have a dedicated Virtus::Coercion class that can handle all sorts of coercions. Every coercion class inherits from Virtus::Coercion::Object which provides default coercion behavior but you are free to override it in your custom classes (that's what most of the built-in classes already do). There's also this trick in Virtus::Coercion::Object that when coercion is not needed then the value will be returned as-is. For example when you call Virtus::Coercion::String.to_string passing in a string object then it will just be returned. This helped us eliminating the need for explicit checks in virtus if the value needs to be coerced or not (the if/else code smell) - it was a pretty cool refactor actually :)

from virtus.

fgrehm avatar fgrehm commented on July 22, 2024

hum... so this is the preferred way of doing it then?

from virtus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.