Don't bother looking up type - the subclasses know what they are.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object.pm
1 use MooseX::Declare;
2 use Moose::Autobox;
3
4 class Gitalist::Git::Object {
5     use MooseX::Types::Moose qw/Str Int Bool Maybe ArrayRef/;
6     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
7     use File::Stat::ModeString qw/mode_to_string/;
8
9     # project and sha1 are required initargs
10     has project => ( isa => 'Gitalist::Git::Project',
11                      required => 1,
12                      is => 'ro',
13                      weak_ref => 1,
14                      handles => {
15                          _run_cmd => 'run_cmd',
16                          _run_cmd_list => 'run_cmd_list',
17                          _get_gpp_object => 'get_gpp_object',
18                      },
19                  );
20     has sha1 => ( isa => NonEmptySimpleStr,
21                   required => 1,
22                   is => 'ro' );
23
24     has type => ( isa => NonEmptySimpleStr,
25                   is => 'ro',
26                   required => 1 );
27
28     has $_ => ( isa => NonEmptySimpleStr,
29                 required => 1,
30                 is => 'ro',
31                 lazy_build => 1 )
32         for qw/modestr size/;
33
34     has _gpp_obj => ( isa => 'Git::PurePerl::Object',
35                       required => 1,
36                       is => 'ro',
37                       lazy_build => 1,
38                       handles => [ 'content',
39                                ],
40                   );
41
42     # objects can't determine their mode or filename
43     has file => ( isa => NonEmptySimpleStr,
44                   required => 0,
45                   is => 'ro' );
46     has mode => ( isa => Int,
47                   required => 1,
48                   default => 0,
49                   is => 'ro' );
50
51     method BUILD { $self->$_() for qw/_gpp_obj size modestr/ }
52
53 ## Private methods
54
55 ## Builders
56     method _build__gpp_obj {
57         return $self->_get_gpp_object($self->sha1)
58     }
59
60     method "_build_size" {
61         my $v = $self->_cat_file_with_flag('s');
62         chomp($v);
63         return $v;
64     }
65
66     method _build_modestr {
67         my $modestr = mode_to_string($self->mode);
68         return $modestr;
69     }
70
71     method _cat_file_with_flag ($flag) {
72         $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
73     }
74
75 } # end class