`mattr`/`cattr` (aka attribute accessors) to keep up with the times
ActiveSupport's version of mattr changed a bit. Specifically what throw my code off was that I used the facets version but with a block which ActiveSupport version now supports.
Submit a PR?
@sahal2080 Thanks for submitting this.
First note that Facets and ActiveSupports cattr/mattr methods are a bit different and in some regards that might always be the case. Unfortunately that's just the nature of beast, so to speak. Sometimes Facets needs to be on the forefront, and it's up to ActiveSupport to take lessons from it -- it's not a one-way street. So for instance, Facets supports :x= symbols. So you can do,
cattr :x, :x=
Instead of,
cattr_accessor :x
On the other hand ActiveSupport has options for :instance_reader and :instance_accessor that Facets doesn't.
cattr :x, :instance_accessor => true
I have to give this some thought (more below).
As for supporting blocks for default values, I guess that would be okay. Looks like all we would need to do is add class_variable_set("@@#{sym}", yield) if block_given? to the cattr methods. I think this is kind of funny/ironic though. A long time time ago people suggested the same thing for attr itself, but Matz never bit. Then I came up with an even even better notation.
attr :x => default
That way you can set defaults for all attributes listed. Not just the same default for all of them. Unless there is something I've overlooked, I like that approach better (though it is still possible to support block notation for comparability). Unfortunately it collides with the :instance_* options. So I'll have to think about that a bit more. And no, Matz never bit on that either ;-)
@0maxxam0 funded this issue with $10. Visit this issue on Issuehunt
@boostio funded this issue with $10. Visit this issue on Issuehunt
@BoostIO @0maxxam0 what is the problem you are having? I can probably help sort this out.
@Rokt33r is this spam?