K. Lottz > Su > Su::Model

Download:
Su-0.110.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  0
View/Report Bugs
Source  

NAME ^

Su::Model - A module to treat user data.

SYNOPSYS ^

  Su::Model::load_model('Pkg::SomeModel', {share => 1} )->{field_A} = $value;

  my $value  = Su::Model::load_model('Pkg::SomeModel')->{field_A};

DESCRIPTION ^

Su::Model holds the data used in your application. For convenience, Su provides method to generate Model class.

 use Su::Model;
 generate_model('NewModel');

ATTRIBUTES ^

FUNCTIONS ^

attr()

Save the passed data in application scope.

 Su::Model->attr( 'key1', 'value1' );
 my $value = Su::Model->attr('key1');

 Su::Model->attr->{key4} = 'value4';
 my $value = Su::Model->attr->{key4};
new()

A Constructor.

generate_model()
  generate_model('SomeModel', qw(field1 string field2 number field3 date));
  $mdl->generate_model('Nest/Mdl2');
  $mdl->generate_model('Nest::Mdl3');
  $mdl->generate_model('Nest::Mdl4',"field1",{"key1"=>"value1","key2"=>"value2"},"field2","value3");

  generate_model(NAME, &rest @(FIELD, VALUE));

Generate the model class using the passed model name. If the optional parameters are passed, then generate the model class using the passed parameter as the value of the model field of the Model. VALUE can be specified as scalar or hash reference.

The model field of the generated Model is like the following.

  my $model=
    {
      field1 =>  "value1",
      field2 => {somekey => "hashvalue"},
  };

You can generate Model class from command line using the following command.

  perl -I../lib -MSu::Model -e '{generate_model("ModelClass",field1,"value1",field2,"value2")}'
  perl -I../lib -MSu::Model -e '{generate_model("Pkg::ModelClass",field1,"value1",field2,"value2")}'

If you want to specify the directory to generate the Model class, then pass the base parameter like the following sample.

  perl -MSu::Model=base,lib -e '{generate_model("ModelClass",field1,value1,field2,value2)}'

You can specify the package name using the dir parameter.

  perl -MSu::Model=dir,PkgName -e '{generate_model("ModelClass",field1,value1,field2,value2)}'

Note that if the model name is specified with qualified package name, then this dir parameter not effect.

If generation is success, this subroutine return the generated file name, else should die or return undef.

load_model()

Loat the Model object from the passed model name and return it's model field. Note that this mothod do not return the instance of the loaded model object itself.

Functional style usage is like the following.

 my $model_href = Su::Model::load_model('SomeModel');

OO Style usage is like the following.

 my $mdl = Su::Model->new;
 $model_href = $mdl->load_model('Pkg/Mdl2');
 $model_href = $mdl->load_model('Pkg::Mdl3');

If you want to set some data to the model and access the data from the model, then the sample code becomes as follwings:

  Su::Model::load_model('Pkg::SomeModel')->{value} = $value;

  my $value  = Su::Model::load_model('Pkg::SomeModel')->{value};

If you want to suppress dying because of module require error, then pass the second parameter like the following.

  my $model = $mdl->load_model( 'Pkg::SomeModel', {suppress_error => 1} );

When the second parameter is passed and load error occured, then this method return undef.

If you want to share and reuse model data, then pass the share parameter as the second parameter.

  my $model = $mdl->load_model( 'Pkg::SomeModel', {share => 1} );
syntax highlighting: