Prechádzať zdrojové kódy

Make all the classes dynamically! WHEE

George S. Baugh 5 rokov pred
rodič
commit
2bda4a2acc
2 zmenil súbory, kde vykonal 18 pridanie a 85 odobranie
  1. 13 69
      lib/Playwright.pm
  2. 5 16
      lib/Playwright/Base.pm

+ 13 - 69
lib/Playwright.pm

@@ -8,11 +8,13 @@ use sigtrap qw/die normal-signals/;
 use File::Basename();
 use Cwd();
 use LWP::UserAgent();
+use Sub::Install();
 use Net::EmptyPort();
 use JSON::MaybeXS();
 use File::Slurper();
 use Carp qw{confess};
 
+use Playwright::Base();
 use Playwright::Util();
 
 #ABSTRACT: Perl client for Playwright
@@ -82,6 +84,17 @@ BEGIN {
             my $class = "Playwright::$class";
             return $class->new( handle => $self, id => $res->{_guid}, type => $class );
         };
+
+        #All of the Playwright::* Classes are made by this MAGIC
+        Sub::Install::install_sub({
+            code => sub ($classname,%options) {
+                @class::ISA = qw{Playwright::Base};
+                $options{type} = $class;
+                return Playwright::Base::new($classname,%options);
+            },
+            as   => 'new',
+            into => "Playwright::$class",
+        });
     }
 
     # Make sure it's possible to start the server
@@ -94,7 +107,6 @@ sub new ($class, %options) {
     #XXX yes, this is a race, so we need retries in _start_server
     my $port = Net::EmptyPort::empty_port();
     my $self = bless({
-        spec    => $spec,
         ua      => $options{ua} // LWP::UserAgent->new(),
         port    => $port,
         debug   => $options{debug},
@@ -155,71 +167,3 @@ sub _start_server($port, $debug) {
 }
 
 1;
-
-#TODO just define these based on the dang JSON
-
-package Playwright::Browser;
-
-use parent qw{Playwright::Base};
-
-sub new ($class,%options) {
-    $options{type} = 'Browser';
-    return $class->SUPER::new(%options);
-}
-
-1;
-
-package Playwright::BrowserContext;
-
-use parent qw{Playwright::Base};
-
-sub new ($class,%options) {
-    $options{type} = 'BrowserContext';
-    $class->SUPER::new(%options);
-}
-
-1;
-
-package Playwright::Page;
-
-use parent qw{Playwright::Base};
-
-sub new ($class,%options) {
-    $options{type} = 'Page';
-    $class->SUPER::new(%options);
-}
-
-1;
-
-package Playwright::Frame;
-
-use parent qw{Playwright::Base};
-
-sub new ($class,%options) {
-    $options{type} = 'Frame';
-    $class->SUPER::new(%options);
-}
-
-1;
-
-package Playwright::Response;
-
-use parent qw{Playwright::Base};
-
-sub new ($class,%options) {
-    $options{type} = 'Response';
-    $class->SUPER::new(%options);
-}
-
-1;
-
-package Playwright::ElementHandle;
-
-use parent qw{Playwright::Base};
-
-sub new ($class,%options) {
-    $options{type} = 'Result';
-    $class->SUPER::new(%options);
-}
-
-1;

+ 5 - 16
lib/Playwright/Base.pm

@@ -25,11 +25,11 @@ use feature qw{signatures};
 Base class for each Playwright class.
 You probably shouldn't use this directly; instead use a subclass.
 
-The specification for each class can also be inspected with the 'spec' method:
+The specification for each class can also be inspected with the 'spec' property:
 
     use Data::Dumper;
-    my $page = Playwright::Base->new(...);
-    print Dumper($page->spec('Page'));
+    my $object = Playwright::Base->new(...);
+    print Dumper($object->{spec});
 
 =head1 CONSTRUCTOR
 
@@ -61,27 +61,16 @@ sub new ($class, %options) {
         Sub::Install::install_sub({
             code => sub {
                 my $self = shift;
-                $self->_request( args => [@_], command => $method, object => $self->{guid}, type => $self->{type} )
+                Playwright::Base::_request($self, args => [@_], command => $method, object => $self->{guid}, type => $self->{type} );
             },
             as   => $method,
+            into => $class,
         }) unless $self->can($method);
     }
 
     return ($self);
 }
 
-=head1 METHODS
-
-=head2 spec
-
-Return the relevant methods and their definitions for this module which are built dynamically from the Playwright API spec.
-
-=cut
-
-sub spec ($self) {
-    return %{$self->{spec}};
-}
-
 sub _request ($self, %args) {
     my $msg = Playwright::Util::request ('POST', 'command', $self->{port}, $self->{ua}, %args);
     return $Playwright::mapper{$msg->{_type}}->($self,$msg) if (ref $msg eq 'HASH') && $msg->{_type} && exists $Playwright::mapper{$msg->{_type}};