Commit | Line | Data |
22e75839 |
1 | use strictures 1; |
2 | use Test::More; |
3 | |
4 | { |
5 | package My::Logger; |
6 | |
7 | use Moo; |
8 | |
9 | has log_file_name => (is => 'ro'); |
10 | |
11 | package My::DB; |
12 | |
13 | use Moo; |
14 | |
15 | has $_ => (is => 'ro') for qw(dsn username password); |
16 | |
17 | sub connect { |
18 | my $class = shift; |
19 | my %args; @args{qw(dsn username password)} = @_; |
20 | $class->new(\%args); |
21 | } |
22 | |
23 | package My::App; |
24 | |
25 | use Moo; |
26 | |
27 | has $_ => (is => 'ro') for qw(logger db); |
28 | } |
29 | |
30 | my $c = do { |
31 | package My::Builder; |
32 | |
33 | use Object::Inline |
34 | importing => { 'Moo' => [] }, |
35 | subs => [ qw(has before after around) ]; |
36 | |
37 | sub _subify { |
38 | ref($_[0]) eq 'CODE' |
39 | ? $_[0] |
40 | : do { my $v = $_[0]; sub { $v } } |
41 | } |
42 | |
43 | sub static { |
44 | has $_[0] => (is => 'lazy'); |
45 | install "_build_$_[0]" => _subify($_[1]); |
46 | } |
47 | |
48 | sub dynamic { |
49 | install $_[0] => _subify($_[1]); |
50 | } |
51 | |
52 | object { |
53 | |
54 | static log_file_name => 'logfile.log'; |
55 | |
56 | static logger => sub { |
57 | My::Logger->new(log_file_name => $_[0]->log_file_name); |
58 | }; |
59 | |
60 | static database => sub { |
61 | |
62 | object { |
63 | static dsn => "dbi:SQLite:dbname=my-app.db"; |
64 | |
65 | static username => 'user234'; |
66 | |
67 | static password => '****'; |
68 | |
69 | dynamic db => sub { |
70 | my $self = shift; |
71 | My::DB->connect($self->dsn, $self->username, $self->password); |
72 | }; |
73 | }; |
74 | }; |
75 | |
76 | dynamic application => sub { |
77 | my $self = shift; |
78 | My::App->new(logger => $self->logger, db => $self->database->db); |
79 | } |
80 | }; |
81 | }; |
82 | |
83 | my $app = $c->application; |
84 | |
85 | ok($app->logger->isa('My::Logger'), 'Logger object exists'); |
86 | |
87 | is($app->logger->log_file_name, 'logfile.log', 'log file name'); |
88 | |
89 | ok($app->db->isa('My::DB'), 'DB object exists'); |
90 | |
91 | is($app->db->dsn, "dbi:SQLite:dbname=my-app.db", 'DB object populated'); |
92 | |
93 | done_testing; |
94 | |
95 | 1; |