
Recently we presented you 40 most used Ruby gems that can be very helpful for you. Before getting into the details of one of the gems, you might be wondering: what are value objects, and why would I want to use them? ValueSemantics is a Ruby gem for making value classes, so let’s explore it together.
ValueSemantics provides a way to make value classes, with a few additional features. These value classes are like immutable Struct
s, but they can be strict and explicit about what attributes they allow, and they come with a little bit of additional functionality.
The main design goals for this gem, in order of importance, are:
- Raise on developer mistakeIf any attributes are missing, that’s an exception. If there is a typo in the attribute name, that’s an exception. If you said the attribute should be a
String
but it wasn’t, that’s an exception. ValueSemantics can be used to make reliable guarantees about attributes. - Be extensibleValueSemantics is primarily concerned with data types, and those vary from project to project. You should be able to define your own custom validators and coercers easily, and they should be as powerful as the built-in ones.
- Be unobtrusiveWhile ValueSemantics does have affordances that encourage you to use it as intended, it shouldn’t restrict your choices, or conflict with other code. Most features are optional.
- Follow conventions as much as possible, ValueSemantics should conform to existing Ruby standards.
- Be standalone, gem should be light-weight, with minimal (currently zero) dependencies.
Check out the documentation for all of the details.
And please investigate the way to create Ruby Gem with native extensions!