jpinkham/webservice-datadog — pull request #1 by guillaumeaubert (2013-01-12 09:02:52+00:00)
Title: Build instantiate object

I see that WebService::DataDog uses a factory pattern to create instances of the subclasses, and while looking at the build() method I noticed this line:

```
my $object = bless( $self, $class ); # Copy internals of factory so we have connection information
```

I believe that your inline comment is correct and that the code just hasn't been synced with it yet. Otherwise, you can end up with a scenario like this:

```
my $datadog = WebService::DataDog->new( ... );
my $metric = $datadog->build( 'Metric' );
# Now $datadog is a WebService::DataDog::Metric object,
# the same as $metric, and you can't instantiate more
# modules via the factory.
```

The commits attached fix this scenario by instantiating submodule objects and via inheritance they end up calling WebService::DataDog->new(), effectively creating a new and independent object for the submodule that has all the properties of the main factory object. An interesting side benefit is that it allows adding WebService::DataDog::Mymodule->new() to tweak how submodule objects are created, in case those objects eventually gain some distinct properties.

I also added tests for that scenario to t/15-build.t. It forced me to add two dependencies to build_requires, but those are core modules so it won't make your distribution harder to install.