facets icon indicating copy to clipboard operation
facets copied to clipboard

`mattr`/`cattr` (aka attribute accessors) to keep up with the times

Open SandNerd opened this issue 9 years ago • 6 comments

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.

SandNerd avatar Oct 04 '16 02:10 SandNerd

Submit a PR?

ioquatix avatar Oct 13 '16 01:10 ioquatix

@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 ;-)

trans avatar Oct 26 '16 14:10 trans

@0maxxam0 funded this issue with $10. Visit this issue on Issuehunt

IssueHuntBot avatar Aug 16 '18 15:08 IssueHuntBot

@boostio funded this issue with $10. Visit this issue on Issuehunt

IssueHuntBot avatar Aug 28 '18 02:08 IssueHuntBot

@BoostIO @0maxxam0 what is the problem you are having? I can probably help sort this out.

ioquatix avatar Aug 28 '18 02:08 ioquatix

@Rokt33r is this spam?

ioquatix avatar Aug 28 '18 05:08 ioquatix