Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

store generation added

  • Loading branch information...
commit 750335ba20d92aec37f869a3e1a746e59dfa2445 1 parent aa28136
@poum authored
Showing with 216 additions and 19 deletions.
  1. +137 −17 lib/DBICx/Generator/ExtJS/Model.pm
  2. +79 −2 t/test.t
View
154 lib/DBICx/Generator/ExtJS/Model.pm
@@ -20,7 +20,8 @@ DBICx::Generator::ExtJS::Model - ExtJS model producer
# this are the default args passed to JSON::DWIW->new
json_args => {
bare_keys => 1,
- pretty => 1,
+ bare_solidus => 1,
+ pretty => 1
},
extjs_args => {
extend => 'MyApp.data.Model',
@@ -35,6 +36,16 @@ DBICx::Generator::ExtJS::Model - ExtJS model producer
$generator->extjs_models_to_file( '/my/dir/' );
+ my $extjs_store_for_foo = $generator->extjs_store('Foo');
+
+ my @extjs_stores = $generator->extjs_stores;
+
+ $generator->extjs_store_to_file( 'Foo', '/my/dir/' );
+
+ $generator->extjs_stores_to_file( '/my/dir/' );
+
+ $generator->extjs_MVC_to_file( '/my/dir/' );
+
=head1 DESCRIPTION
Creates ExtJS model classes.
@@ -79,6 +90,12 @@ has '_path' => (
default => sub { {}; }
);
+has '_extjs_build' => (
+ is => 'ro',
+ isa => 'HashRef',
+ default => sub { {}; }
+);
+
sub _build__json {
my $self = shift;
@@ -90,6 +107,7 @@ has 'json_args' => (
isa => 'HashRef',
default => sub {
{ bare_keys => 1,
+ bare_solidus => 1,
pretty => 1,
};
},
@@ -188,19 +206,67 @@ sub extjs_model_name {
=item extjs_model
-This method returns an arrayref containing the parameters that can be
+This method returns an arrayref containing the model parameters that can be
serialized to JSON and then passed to Ext.define for one
DBIx::Class::ResultSource.
=cut
sub extjs_model {
+ _extjs_type(@_, 'model');
+}
+
+=item extjs_store
+
+This method returns an arrayref containing the store parameters that can be
+serialized to JSON and then passed to Ext.define for one
+DBIx::Class::ResultSource.
+
+=cut
+
+sub extjs_store {
+ _extjs_type(@_, 'store');
+}
+
+# This method returns an arrayref containing the type parameters that can be
+# serialized to JSON and then passed to Ext.define for one
+# DBIx::Class::ResultSource.
+#
+# type: model, store
+sub _extjs_type {
+ my ($self, $rsrcname, $type) = @_;
+
+ my $build = $self->_extjs_build;
+
+ $self->_extjs_generate($rsrcname)
+ unless exists $build->{$rsrcname};
+
+ return [ $build->{$rsrcname}->{extjsname}, $build->{$rsrcname}->{$type} ];
+}
+
+sub _extjs_generate {
my ( $self, $rsrcname ) = @_;
my $schema = $self->schema;
my $rsrc = $schema->source($rsrcname);
my $extjsname = $self->extjs_model_name($rsrcname);
+ my @pk = $rsrc->primary_columns;
+
+ my $model = { extend => 'Ext.data.Model' };
+ $model->{idProperty} = $pk[0]
+ if @pk == 1;
+
+ my $store = {
+ extend => 'Ext.data.Store',
+ model => $self->appname . '.model.' . $extjsname,
+ autoload => 'true',
+ proxy => {
+ type => 'ajax',
+ url => "/$extjsname"
+ }
+ };
+
my $columns_info = $rsrc->columns_info;
my (@fields, @validations) = ();
foreach my $colname ( $rsrc->columns ) {
@@ -238,20 +304,13 @@ sub extjs_model {
}
push @fields, $field_params;
- # Add presence validations
- unless ($column_info->{is_nullable}) {
- push @validations, { type => 'presence', field => $colname };
- }
+ # Add presence validations
+ unless ($column_info->{is_nullable}) {
+ push @validations, { type => 'presence', field => $colname };
+ }
}
- my @pk = $rsrc->primary_columns;
-
- my $model = {
- extend => 'Ext.data.Model',
- fields => \@fields,
- };
- $model->{idProperty} = $pk[0]
- if @pk == 1;
+ $model->{fields} = \@fields;
my @assocs;
foreach my $relname ( $rsrc->relationships ) {
@@ -336,7 +395,11 @@ sub extjs_model {
$model = \%foo;
}
- return [ $extjsname, $model ];
+ $self->_extjs_build->{$rsrcname} = {
+ extjsname => $extjsname,
+ model => $model,
+ store => $store,
+ };
}
=item extjs_models
@@ -361,6 +424,28 @@ sub extjs_models {
return \%output;
}
+=item extjs_stores
+
+This method returns the generated ExtJS store classes as hashref indexed by
+their ExtJS names.
+
+=cut
+
+sub extjs_stores {
+ my $self = shift;
+
+ my $schema = $self->schema;
+
+ my %output;
+ foreach my $rsrcname ( $schema->sources ) {
+ my $extjs_store = $self->extjs_store($rsrcname);
+
+ $output{ $extjs_store->[0] } = $extjs_store;
+ }
+
+ return \%output;
+}
+
# This method groups the directory check/creation operation needed
# for generation
# created dir are cached in _path hashref
@@ -394,8 +479,8 @@ sub _get_dir {
This method takes a single DBIx::Class::ResultSource name and a directory name
and outputs the generated ExtJS model class to a file according to ExtJS
naming standards.
-An error is thrown if the directory doesn't exist or if the file already
-exists.
+An error is thrown if the file already exists.
+If last path directory is'nt 'model', a model subdirectory is added.
=cut
@@ -426,6 +511,8 @@ sub extjs_model_to_file {
This method takes a directory name and outputs the generated ExtJS model
classes to a file per model according to ExtJS naming standards.
+An error is thrown if the file already exists.
+If last path directory is'nt 'model', a model subdirectory is added.
=cut
@@ -439,15 +526,43 @@ sub extjs_models_to_file {
=item extjs_store_to_file
+This method takes a single DBIx::Class::ResultSource name and a directory name
+and outputs the generated ExtJS store class to a file according to ExtJS
+naming standards.
+An error is thrown if the file already exists.
+If last path directory is'nt 'store', a store subdirectory is added.
+
=cut
sub extjs_store_to_file {
+ my ( $self, $rsrcname, $dirname ) = @_;
+
+ my $dir = $self->_get_dir($dirname,'store');
+
+ my ( $extjs_store_name, $extjs_store_code ) =
+ @{ $self->extjs_store($rsrcname) };
+
+ my $json =
+ 'Ext.define('
+ . $self->_json->to_json(
+ $self->appname . '.store.' . $extjs_store_name )
+ . ', '
+ . $self->_json->to_json($extjs_store_code) . ');';
+
+ my $file = $dir->file("$extjs_store_name.js");
+
+ my $fh = $file->open( O_CREAT | O_WRONLY | O_EXCL )
+ or croak "$file already exists: $!";
+
+ $fh->write($json);
}
=item extjs_stores_to_file
This method takes a directory name and outputs the generated ExtJS store
classes to a file per store according to ExtJS naming standards.
+An error is thrown if the file already exists.
+If last path directory is'nt 'store', a store subdirectory is added.
=cut
@@ -464,6 +579,7 @@ sub extjs_stores_to_file {
This method takes a directory name and outputs the generated model,
store, controller, and view - form and list or tree - classes to a
file per class according to naming standards.
+model, store, view and controller subdirectories are automatically added.
=cut
@@ -472,8 +588,12 @@ sub extjs_MVC_to_file {
$self->extjs_models_to_file($dirname);
$self->extjs_stores_to_file($dirname);
+ carp 'controllers are not yet mplemented ...';
+ carp 'form and grid/tree view are not yet implemented ...';
}
=back
+=cut
+
1;
View
81 t/test.t
@@ -56,10 +56,29 @@ eq_or_diff(
"'Another' model ok"
);
+my $extjs_store_for_another;
+lives_ok { $extjs_store_for_another = $generator->extjs_store('Another') }
+"generation of 'Another' successful";
+eq_or_diff(
+ $extjs_store_for_another,
+ [
+ 'Another',
+ {
+ autoload => 'true',
+ extend => 'Ext.data.Store',
+ model => 'MyApp.model.Another',
+ proxy => {
+ type => 'ajax',
+ url => '/Another'
+ }
+ }
+ ],
+"'Another' model ok"
+);
+
my $extjs_models;
lives_ok { $extjs_models = $generator->extjs_models; }
'generation successful';
-
eq_or_diff(
$extjs_models,
{ 'Another' => [
@@ -170,6 +189,40 @@ eq_or_diff(
"extjs_models output ok"
);
+my $extjs_stores;
+lives_ok { $extjs_stores = $generator->extjs_stores; }
+'generation successful';
+eq_or_diff(
+ $extjs_stores,
+ {
+ Another => [
+ 'Another',
+ {
+ autoload => 'true',
+ extend => 'Ext.data.Store',
+ model => 'MyApp.model.Another',
+ proxy => {
+ type => 'ajax',
+ url => '/Another'
+ }
+ }
+ ],
+ Basic => [
+ 'Basic',
+ {
+ autoload => 'true',
+ extend => 'Ext.data.Store',
+ model => 'MyApp.model.Basic',
+ proxy => {
+ type => 'ajax',
+ url => '/Basic'
+ }
+ }
+ ]
+ },
+ "extjs_stores output ok"
+);
+
# this creates a File::Temp object which immediatly goes out of scope and
# results in deleting of the dir
my $non_existing_dirname = File::Temp->newdir->dirname;
@@ -183,7 +236,7 @@ my $non_existing_dirname = File::Temp->newdir->dirname;
my $dirname = $dir->dirname;
diag("writing 'Another' to $dirname");
lives_ok { $generator->extjs_model_to_file( 'Another', $dirname ) }
- "file generation of 'Another' ok";
+ "model file generation of 'Another' ok";
}
{
@@ -194,4 +247,28 @@ my $non_existing_dirname = File::Temp->newdir->dirname;
"file generation of all models ok";
}
+{
+ my $dir = File::Temp->newdir;
+ my $dirname = $dir->dirname;
+ diag("writing 'Another' store to $dirname");
+ lives_ok { $generator->extjs_store_to_file( 'Another', $dirname ) }
+ "store file generation of 'Another' ok";
+}
+
+{
+ my $dir = File::Temp->newdir;
+ my $dirname = $dir->dirname;
+ diag("writing all stores to $dirname");
+ lives_ok { $generator->extjs_stores_to_file( $dirname ) }
+ "file generation of all stores ok";
+}
+
+{
+ my $dir = File::Temp->newdir;
+ my $dirname = $dir->dirname;
+ diag("writing all to $dirname");
+ lives_ok { $generator->extjs_MVC_to_file( $dirname ) }
+ "file generation of all ok";
+}
+
done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.