use MooseX::Declare;
+use Moose::Autobox;
class Gitalist::Git::Object {
- use MooseX::Types::Moose qw/Str Int/;
+ use MooseX::Types::Moose qw/Str Int Bool Maybe ArrayRef/;
+ use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
use File::Stat::ModeString qw/mode_to_string/;
+
+ # project and sha1 are required initargs
has project => ( isa => 'Gitalist::Git::Project',
required => 1,
is => 'ro',
- handles => [ 'run_cmd' ],
+ weak_ref => 1,
+ handles => {
+ _run_cmd => 'run_cmd',
+ _run_cmd_fh => 'run_cmd_fh',
+ _run_cmd_list => 'run_cmd_list',
+ _get_gpp_object => 'get_gpp_object',
+ },
);
- has $_ => ( isa => Str,
- required => 1,
- is => 'ro' )
- for qw/sha1 file/;
- has $_ => ( isa => Str,
+ has sha1 => ( isa => NonEmptySimpleStr,
required => 1,
+ is => 'ro' );
+
+ has type => ( isa => NonEmptySimpleStr,
is => 'ro',
- lazy_build => 1 )
- for qw/type modestr/;
- has $_ => ( isa => Int,
- required => 1,
- is => 'ro' )
- for qw/mode size/;
+ required => 1 );
- method _build_type {
- my $output = $self->run_cmd(qw/cat-file -t/, $self->{sha1});
- chomp($output);
- return $output;
- }
-
- method _build_modestr {
- my $modestr = mode_to_string($self->{mode});
- return $modestr;
- }
+ has $_ => ( isa => NonEmptySimpleStr,
+ required => 1,
+ is => 'ro',
+ lazy_build => 1 )
+ for qw/modestr size/;
- method _build_size {
- my $output = $self->run_cmd(qw/cat-file -s/, $self->{sha1});
- chomp($output);
- return $output;
- }
+ has _gpp_obj => ( isa => 'Git::PurePerl::Object',
+ required => 1,
+ is => 'ro',
+ lazy_build => 1,
+ handles => [ 'content',
+ ],
+ );
+
+ # objects can't determine their mode or filename
+ has file => ( isa => NonEmptySimpleStr,
+ required => 0,
+ is => 'ro' );
+ has mode => ( isa => Int,
+ required => 1,
+ default => 0,
+ is => 'ro' );
-=head2 contents
+ method BUILD { $self->$_() for qw/_gpp_obj size modestr/ }
-Return the contents of a given file.
+## Private methods
-=cut
+## Builders
+ method _build__gpp_obj {
+ return $self->_get_gpp_object($self->sha1)
+ }
- method contents {
- if ( $self->type ne 'blob' ) {
- die "object $self->sha1 is not a file\n"
- }
+ method "_build_size" {
+ my $v = $self->_cat_file_with_flag('s');
+ chomp($v);
+ return $v;
+ }
- my $output = $self->run_cmd(qw/cat-file -p/, $self->sha1);
- return unless $output;
+ method _build_modestr {
+ my $modestr = mode_to_string($self->mode);
+ return $modestr;
+ }
- return $output;
+ method _cat_file_with_flag ($flag) {
+ $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
}
} # end class