1 package Object::Builder;
3 use Module::Runtime qw(use_module);
6 our $VERSION = '0.000001'; # 0.0.1
8 $VERSION = eval $VERSION;
11 is => 'rw', lazy => 1, builder => 1,
12 trigger => sub { shift->_clear_final_class },
15 sub _build_class { die "No default class set" }
18 is => 'rw', lazy => 1, builder => 1,
19 trigger => sub { shift->_clear_final_class },
20 clearer => 'reset_roles',
23 after reset_roles => sub { shift->_clear_final_class };
25 sub _build_roles { [] }
27 has _final_class => (is => 'lazy', clearer => 1);
29 sub _build__final_class {
31 my $class = use_module($self->class);
32 if (my @roles = @{$self->roles}) {
34 return Moo::Role->create_class_with_roles($class, @roles);
40 after _clear_final_class => sub { shift->clear_object };
42 has constructor => (is => 'ro', default => sub { 'new' });
45 is => 'rw', builder => 1,
46 trigger => sub { shift->_clear_final_arguments },
47 clearer => 'reset_arguments',
50 after reset_arguments => sub { shift->_clear_final_arguments };
52 sub _build_arguments { {} }
54 has argument_filter => (
55 is => 'rw', builder => 1,
56 trigger => sub { shift->_clear_final_arguments },
57 clearer => 'reset_argument_filter',
60 sub _build_argument_filter { sub { shift } }
62 has _final_arguments => (is => 'lazy', clearer => 1);
64 after _clear_final_arguments => sub { shift->_clear_object };
66 sub _build__final_arguments {
68 $self->argument_filter->($self->arguments);
71 has object => (is => 'lazy', clearer => 1);
75 $self->_final_class->${\$self->constructor}($self->_final_arguments);
79 my ($self, $args) = @_;
81 $args->{object} or $args->{class}
82 or ($self->can('_build_class') ne __PACKAGE__->can('_build_class')
84 die "No static object passed, and no class supplied or defaulted";
92 Object::Builder - An object for building other objects.
100 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
104 None yet - maybe this software is perfect! (ahahahahahahahahaha)
108 Copyright (c) 2012 the Object::Builder L</AUTHOR> and L</CONTRIBUTORS>
113 This library is free software and may be distributed under the same terms