1 package Object::Builder;
3 use Module::Runtime qw(use_module);
4 use List::Util qw(reduce);
5 use Scalar::Util qw(weaken);
8 our $VERSION = '0.000001'; # 0.0.1
10 $VERSION = eval $VERSION;
13 is => 'rw', lazy => 1, builder => 1,
14 trigger => sub { shift->_clear_final_class },
17 sub _build_class { die "No default class set" }
20 is => 'rw', lazy => 1, builder => 1, clearer => 1,
21 trigger => sub { shift->_clear_final_class },
22 coerce => sub { ref($_[0]) eq 'ARRAY' ? { '' => $_[0] } : $_[0] },
26 my ($self, $for, @roles) = @_;
27 $self->roles({ %{$self->roles}, $for => \@roles });
31 after clear_roles => sub { shift->_clear_final_class };
33 sub _build_roles { {} }
37 my $roles = $self->roles;
38 map @{$roles->{$_}}, sort keys %$roles;
41 has _final_class => (is => 'lazy', clearer => 1);
43 sub _build__final_class {
46 # This path will only be taken if class => undef was intentionally passed
47 # to our constructor; this allows for a subref constructor which of course
48 # doesn't necessarily need a class at all.
50 return unless defined($self->class);
52 my $class = use_module($self->class);
53 if (my @roles = $self->_role_list) {
55 return Moo::Role->create_class_with_roles($class, @roles);
61 after _clear_final_class => sub { shift->clear_object };
63 has constructor => (is => 'ro', default => sub { 'new' });
66 is => 'rw', builder => 1, clearer => 1,
67 trigger => sub { shift->_clear_final_arguments },
71 my ($self, $name, @arg) = @_;
72 $self->arguments({ %{$self->arguments}, $name => \@arg });
75 sub set_argument_weaken {
76 my ($self, $name, @arg) = @_;
78 $self->arguments({ %{$self->arguments}, $name => \@arg });
81 sub _build_arguments { {} }
83 after clear_arguments => sub { shift->_clear_final_arguments };
85 has _final_arguments => (is => 'lazy', clearer => 1);
87 after _clear_final_arguments => sub { shift->clear_object };
89 sub _build__final_arguments {
91 my %arguments = %{$self->arguments};
92 map +($_ => $self->_resolve($arguments{$_})), keys %arguments;
96 my ($self, $to_resolve) = @_;
99 # [ $inv, 'x', 'y' ] -> $inv->x->y
100 # [ $inv, [ 'x', [ 'y' ] ], 'z' ] -> $inv->x('y')->z
101 # [ $inv, [ 'x', [ $other, 'y' ] ], 'z' ] -> $inv->x($other->y)->z
103 no warnings 'once'; # $a
106 my ($meth, @arg) = (ref($b) eq 'ARRAY' ? @$b : $b);
107 $a->$meth(map $self->_resolve($_), @arg);
111 has object => (is => 'lazy', clearer => 1);
115 $self->_final_class->${\$self->constructor}($self->_final_arguments);
120 $self->_build_object;
124 my ($self, $args) = @_;
126 $args->{object} or exists $args->{class}
127 or ($self->can('_build_class') ne __PACKAGE__->can('_build_class'))
129 die "No static object passed, and no class supplied or defaulted";
137 Object::Builder - An object for building other objects.
145 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
149 None yet - maybe this software is perfect! (ahahahahahahahahaha)
153 Copyright (c) 2012 the Object::Builder L</AUTHOR> and L</CONTRIBUTORS>
158 This library is free software and may be distributed under the same terms