Make Object aware of its containing repository, and able to return its contents.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object.pm
1 use MooseX::Declare;
2
3 class Gitalist::Git::Object {
4     use File::Stat::ModeString qw/mode_to_string/;
5     has project => ( isa => 'Gitalist::Git::Project',
6                      required => 1,
7                      is => 'ro',
8                      handles => [ 'run_cmd' ],
9                  );
10     has sha1 => ( isa => 'Str',
11                   required => 1,
12                   is => 'ro' );
13     has type => ( isa => 'Str',
14                   required => 1,
15                   is => 'ro',
16                   lazy_build => 1 );
17     has file => ( isa => 'Str',
18                   required => 1,
19                   is => 'ro' );
20     has mode => ( isa => 'Int',
21                   required => 1,
22                   is => 'ro' );
23     has modestr => ( isa => 'Str',
24                      is => 'ro',
25                      lazy_build => 1,
26                  );
27     has size => ( isa => 'Int',
28                   is => 'ro',
29                   lazy_build => 1);
30
31     method _build_type {
32         my $output = $self->run_cmd(qw/cat-file -t/, $self->{sha1});
33         chomp($output);
34         return $output;
35     }
36     
37     method _build_modestr {
38         my $modestr = mode_to_string($self->{mode});
39         return $modestr;
40     }
41
42     method _build_size {
43         my $output = $self->run_cmd(qw/cat-file -s/, $self->{sha1});
44         chomp($output);
45         return $output;
46     }
47
48 =head2 contents
49
50 Return the contents of a given file.
51
52 =cut
53
54     method contents {
55         if ( $self->type ne 'blob' ) {
56             die "object $self->sha1 is not a file\n"
57         }
58
59         my $output = $self->run_cmd(qw/cat-file -p/, $self->sha1);
60         return unless $output;
61
62         return $output;
63     }
64
65 } # end class