Factory class
.../Entity/Asset.pm
.../Entity/Datacentre.pm etc
type ... the entity type, which corresponds to the database table. Function type2source returns the name of the DBIC ResultSource file in the Result directory ($class). $class is used to load the specialized class (according to the $type) at run time, after which the specialized class object is created and returned;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package AssetManagerApi2::EntityFactory; use v5.018; use utf8; use open ':encoding(UTF-8)'; use feature 'unicode_strings'; use FindBin qw($Bin); use lib "$Bin/../lib"; use AssetManagerApi2::Helper::Entity; =head3 new IN: class hash of input params for the entity object to be created and c and entity type info =cut sub new { my $class = shift; my $entity = AssetManagerApi2::Helper::Entity::type2table(shift); my $path = "AssetManagerApi2/Entity/$entity.pm"; $class = "AssetManagerApi2::Entity::$entity"; require $path; return $class->new(@_); }; 1; |
Specialized classes
Specialized classes are Moose-based and are associated with database table rows represented by DBIx::Class ResultSources.
The input to the constructor is a hashref, containing:
- Catalyst object and the entity type
- properties of the DBIC object
example : { c => $c, type => $type, id => $dbic->id, name => $dbic->name }
The id, the Catalyst object and the entity type are set up first. Then this information is used by the Moose constructor to set up the rest of the properties through the builder methods.
Asset.pm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | package AssetManagerApi2::Entity::Asset; use Moose; use namespace::autoclean; use v5.018; use utf8; use open ':encoding(UTF-8)'; use feature 'unicode_strings'; use AssetManagerApi2::Helper::Entity qw( create_output_structure ); my $asset_model = 'AssetManagerDB::Asset'; has 'c' => (is => 'ro', isa => 'AssetManagerApi2', required => 1, ); has 'dbic' => (is => 'ro', isa => 'AssetManagerApi2::Schema::AssetManagerDB::Result::Asset', builder => '_build_dbic', ); has 'type' => (is => 'ro', isa => 'Str', lazy => 1, builder => '_build_type', ); has 'id' => (is => 'ro', isa => 'Int', required => 1, ); has 'name' => (is => 'rw', isa => 'Str', lazy => 1, builder => '_build_name', ); has 'datacentre' => (is => 'rw', isa => 'HashRef', lazy => 1, builder => '_build_datacentre', ); has 'software' => (is => 'ro', isa => "ArrayRef[HashRef]|ArrayRef", lazy => 1, builder => '_build_software', ); =head2 MOOSE METHODS =cut =head3 BUILDARGS The instance is associated with the DBIC object. 1) Retrieve the DBIC object 2) Set up some of the Asset object properties before Moose constructer takes over =cut around BUILDARGS => sub { my $orig = shift; my $class = shift; my $c = $_[0]->{c}; my $dbic = $c->model($asset_model)->find({id => $_[0]->{id}}); my %props = ( c => $c, id => $_[0]->{id}, type => 'asset', dbic => $dbic, ); return $class->$orig(%props); }; sub _build_type { my ($self) = @_; } sub _build_dbic { my ($self) = @_; } sub _build_name { my ($self) = @_; $self->name($self->dbic->name); } sub _build_datacentre { my ($self) = @_; my $c = $self->c; my $datacentre_dbic = $self->dbic->datacentre; my $datacentre = create_output_structure($c, $datacentre_dbic, 'datacentre'); $self->datacentre($datacentre); } sub _build_software { my ($self) = @_; my $c = $self->c; my $self_software_dbic = $self->dbic->softwares; my @software = (); while (my $package = $self_software_dbic->next) { my $software = create_output_structure($c, $package, 'software'); push @software, $software; } $self->software(\@software); } =head2 PRIVATE HELPER METHODS =cut =head2 PUBLIC API METHODS =cut sub associate_software { my ($self, $props) = @_; my $c = $self->c; my $dbic = $self->dbic->find_or_create($props); create_output_structure($c, $dbic, 'asset_software'); } __PACKAGE__->meta->make_immutable; 1; |
No comments:
Post a Comment
Note: only a member of this blog may post a comment.