Modify

Ticket #101 (closed enhancement: wontfix)

Opened 7 years ago

Last modified 6 years ago

Accessor and mutator autogeneration via class-method call

Reported by: javier.amor.garcia@… Owned by: javier.amor.garcia@…
Milestone: 0.11 Component: base
Severity: normal Keywords:
Cc:

Description

I am toying with the idea of making a method in gconfmodule that takes care of reating the mutators and accessors for the module. The method will take the name of the module and a argument list of hash references. Each hash will provide a simple definition of the attribute so we can create the methods.

The fields available for the definitios may be:

name - the name of the attribute type - the type of the attriubte (default: object) The type may be:

  • object: the attribute is stored in the object itself (i.e

$self->{attr} = $value)

  • a gconf type: bool, int, string, list

read - wether to make an accesor to the attribute (default: true) write - wether to make a mutator to the attribute (default: true) check - a sub reference that will be called when setting new values, if the sub returns false a exception will be throw. (the sub also can throw their own exceptions, in fact this is encouraged). (default: values are not checked)

A simple example of use (only 3 attributes!):

package EBox::Macaco; use base 'EBox::GConfModule; use strict; use warnings;

EBox::GConfModule::attributes(PACKAGE,

{name => 'mood', check =>

\&correctMoods},

{name => fleas, type => 'integer'}, {name => 'hideColour', type => 'string',

write => 0}

);

This call would be roughly equivalent to write this:

sub mood {

my ($self) = @_; return $self->{mood};

}

sub setMood {

my ($self, $newMood) = @_; unless (correcMoods($newMood)) {

throw EBox::EXceptions::InvalidData?(data => 'mood', value => $mood);

}

$self->{mood} = $newMood;

}

sub fleas {

my ($self) = @_; return $self->get_int('fleas');

}

sub setFleas {

my ($self, $fleas) = @_; return $self->set_int('fleas', $fleas);

}

sub hideColour {

my ($self) = @_; return $self->get_string('hideColour');

}

As you see it pays the worth. Besides the writing avoided we may reap other benefits for uniformity of access.

Cons:

  • defined in run time so it add overhead to the modules' loading. I

think that is not outer-weight the benefits but we can avoid his use in the more used modules like Global or Firewall

Attachments

Change History

comment:1 Changed 7 years ago by hblanco@…

  • Milestone set to 0.11

comment:2 Changed 6 years ago by javier.amor.garcia@…

  • Status changed from new to assigned

comment:3 Changed 6 years ago by javier.amor.garcia@…

  • Status changed from assigned to new

comment:4 Changed 6 years ago by javier.amor.garcia@…

  • Status changed from new to closed
  • Resolution set to wontfix

The new model framework does this

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.