Change Project->description to use GPP.
[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/;
6     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
7     use File::Stat::ModeString qw/mode_to_string/;
8     # project and sha1 are required initargs
9     has project => ( isa => 'Gitalist::Git::Project',
10                      required => 1,
11                      is => 'ro',
12                      weak_ref => 1,
13                      handles => {
14                          _run_cmd => 'run_cmd',
15                          _get_gpp_object => 'get_gpp_object',
16                      },
17                  );
18     has sha1 => ( isa => NonEmptySimpleStr,
19                required => 1,
20                is => 'ro' );
21
22     has $_ => ( isa => NonEmptySimpleStr,
23                   required => 1,
24                   is => 'ro',
25                   lazy_build => 1 )
26         for qw/type modestr size/;
27
28     has _gpp_obj => ( isa => 'Git::PurePerl::Object',
29                       required => 1,
30                       is => 'ro',
31                       lazy_build => 1,
32                       handles => [ 'parents',
33                                    'parent_sha1',
34                                    'author',
35                                    'authored_time',
36                                    'committer',
37                                    'committed_time',
38                                ],
39                   );
40
41     # This feels wrong, but current templates assume
42     # these attributes are present on every object.
43     foreach my $key (qw/tree_sha1 comment content/) {
44         has $key => ( isa => Str,
45                       required => 1,
46                       is => 'ro',
47                       lazy_build => 1,
48                   );
49         method "_build_$key" {
50             confess("Object can't " . $key) unless $self->_gpp_obj->can($key);
51             return $self->_gpp_obj->$key;
52         }
53     }
54
55     # objects can't determine their mode or filename
56     has file => ( isa => NonEmptySimpleStr,
57                   required => 0,
58                   is => 'ro' );
59     has mode => ( isa => Int,
60                 required => 1,
61                 default => 0,
62                 is => 'ro' );
63
64     method BUILD { $self->$_() for qw/_gpp_obj type size modestr/ }
65
66     method _build__gpp_obj {
67         return $self->_get_gpp_object($self->sha1)
68     }
69
70     foreach my $key (qw/ type size /) {
71         method "_build_$key" {
72             my $v = $self->_cat_file_with_flag(substr($key, 0, 1));
73             chomp($v);
74             return $v;
75         }
76     }
77
78     method _build_modestr {
79         my $modestr = mode_to_string($self->mode);
80         return $modestr;
81     }
82
83     method _cat_file_with_flag ($flag) {
84         $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
85     }
86
87 } # end class