/
Source.pm
133 lines (90 loc) · 2.83 KB
/
Source.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use 5.010001;
use strict;
use warnings;
package Mite::Source;
use Mite::Miteception;
our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION = '0.005002';
use Mite::Compiled;
use Mite::Class;
has file =>
is => ro,
isa => Path->no_coercions->plus_coercions(Str, 'Path::Tiny::path($_)'),,
coerce => true,
required => true;
has classes =>
is => ro,
isa => HashRef[InstanceOf['Mite::Class']],
default => sub { {} };
has compiled =>
is => ro,
isa => InstanceOf['Mite::Compiled'],
lazy => true,
default => sub {
my $self = shift;
return Mite::Compiled->new( source => $self );
};
has project =>
is => rw,
isa => InstanceOf['Mite::Project'],
# avoid a circular dep with Mite::Project
weak_ref => true;
##-
sub has_class {
my ( $self, $name ) = ( shift, @_ );
return defined $self->classes->{$name};
}
sub compile {
my $self = shift;
return $self->compiled->compile();
}
# Add an existing class instance to this source
sub add_classes {
my ( $self, @classes ) = ( shift, @_ );
for my $class (@classes) {
$self->classes->{$class->name} = $class;
$class->source($self);
}
return;
}
# Create or reuse a class instance for this source give a name
sub class_for {
my ( $self, $name, $metaclass ) = ( shift, @_ );
$metaclass ||= 'Mite::Class';
return $self->classes->{$name} ||= $metaclass->new(
name => $name,
source => $self,
);
}
1;
__END__
=pod
=head1 NAME
Mite::Source - Representing the human written .pm file.
=head1 SYNOPSIS
use Mite::Source;
my $source = Mite::Source->new( file => $pm_filename );
=head1 DESCRIPTION
NO USER SERVICABLE PARTS INSIDE. This is a private class.
Represents a .pm file, written by a human, which uses Mite.
It is responsible for information about the source file.
* The Mite classes contained in the source.
* The compiled Mite file associated with it.
It delegates most work to other classes.
This object is necessary because there can be multiple Mite classes in
one source file.
=head1 BUGS
Please report any bugs to L<https://github.com/tobyink/p5-mite/issues>.
=head1 AUTHOR
Michael G Schwern E<lt>mschwern@cpan.orgE<gt>.
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2011-2014 by Michael G Schwern.
This software is copyright (c) 2022 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
=cut