shorthand support?

12 views
Skip to first unread message

ladd.james

unread,
Oct 11, 2012, 9:38:20 PM10/11/12
to mari...@googlegroups.com
Would it be possible for Maria to support a short hand for creating getters and setters?
(Personally I don't like either but thats another topic)

In Ruby there is a short hand like this:

attr_reader: :foo, :bar, :baz
attr_writer: :foo, :bar, :baz

and

attr_accessor: :foo, :bar, :baz

These create methods on your instance to get (attr_reader) the variables foo, bar and baz,
and to set (attr_writer) foo, bar, baz. With attr_accessor generating both an accessor and a
setter.

I imagine something like this:

handy.attrAccessor(this, ['foo', 'bar', 'baz']);

Thoughts?

Peter Michaux

unread,
Oct 11, 2012, 10:13:48 PM10/11/12
to mari...@googlegroups.com
Hi James,

On Thu, Oct 11, 2012 at 6:38 PM, ladd.james <ladd....@gmail.com> wrote:
> Would it be possible for Maria to support a short hand for creating getters
> and setters?

It is definitely possible. This is a perfect idea for a Maria plugin
for people who like accessors.


> (Personally I don't like either but thats another topic)

(Not everyone likes accessors.)


> In Ruby there is a short hand like this:
>
> attr_reader: :foo, :bar, :baz
> attr_writer: :foo, :bar, :baz
>
> and
>
> attr_accessor: :foo, :bar, :baz
>
> These create methods on your instance to get (attr_reader) the variables
> foo, bar and baz,
> and to set (attr_writer) foo, bar, baz. With attr_accessor generating both
> an accessor and a
> setter.
>
> I imagine something like this:
>
> handy.attrAccessor(this, ['foo', 'bar', 'baz']);

A generic library could be built as a separate project. Something like
the following in a library handy.js

----------------------------------------
var handy = {};
handy.capitalize = function(str) {
return str.charAt(0).toUpperCase() + str.substr(1);
};
handy.attrReader = function(obj, names) {
for (var i = 0, ilen = names.length; i < ilen; i++) {
(function(name) {
obj['get'+handy.capitalize(name)] = function() {
return this['_'+name];
};
}(names[i]));
}
obj = names = null;
};
handy.attrWriter = function(obj, names) {
for (var i = 0, ilen = names.length; i < ilen; i++) {
(function(name) {
obj['get'+handy.capitalize(name)] = function(val) {
this['_'+name] = val;
};
}(names[i]));
}
obj = names = null;
};
handy.attrAccessor = function(obj, names) {
handy.attrReader(obj, names);
handy.attrWriter(obj, names);
};
----------------------------------------

Now that we have that library available, we can integrate it into
Maria. I think the following would do in a plugin called
maria-handy.js

----------------------------------------
maria.subclass = (function() {
var original = maria.subclass;
return function(namespace, name, options) {
var Constructor = original.apply(this, arguments);
if (options.readers) {
handy.attrReaders(Constructor.prototype, options.readers);
}
if (options.writers) {
handy.attrWriters(Constructor.prototype, options.writers);
}
if (options.accessors) {
handy.attrAccessors(Constructor.prototype, options.accessors);
}
return Constructor;
};
}());
----------------------------------------

Finally use it when creating classes

maria.Model.subclass(myApp, 'myClass', {
readers: ['id'],
accessors: ['username']
});

which is shorter but would be functionally identical to writing

maria.Model.subclass(myApp, 'myClass', {
properties: {
getId: function() {
return this._id;
},
getUsername: function() {
return this._username;
},
setUsername: function(un) {
this._username = un;
}
}
});


> Thoughts?

There are many variations on this theme that could be played which is
exactly why this is good material for a plugin rather than core Maria
functionality. For example, the setters/writers above do not call
dispatchEvent to tell observers of the model object that the model has
changed. That type of functionality could be mixed into
maria.Model.subclass (rather than the example shown above with mixes
into maria.subclass.)

(None of the above code is tested.)

Peter
Reply all
Reply to author
Forward
0 new messages