move the actual doing stuff code for FS lib into a Guts.pm
[scpubgit/DKit.git] / lib / DX / Lib / FS / Action / CreateFile.pm
1 package DX::Lib::FS::Action::CreateFile;
2
3 use aliased 'DX::Lib::FS::Fact::FileContent';
4 use aliased 'DX::Lib::FS::Fact::PathStatus';
5 use aliased 'DX::Lib::FS::Fact::PathStatusInfo';
6 use DX::Lib::FS::Guts;
7 use Moo;
8
9 with 'DX::Role::Action';
10
11 has path => (is => 'ro', required => 1);
12
13 has mode => (is => 'ro', predicate => 1);
14
15 has data => (is => 'ro', default => sub { '' });
16
17 sub expected_effect {
18   my ($self) = @_;
19   return +(path_status => PathStatus->new(
20     path => $self->path,
21     info => PathStatusInfo->new(
22       is_file => 1,
23       mode => ($self->has_mode ? $self->mode : '')
24     )
25   ), file_content => FileContent->new(
26     path => $self->path,
27     data => $self->data,
28   ));
29 }
30
31 sub _do_run {
32   my ($self) = @_;
33   DX::Lib::FS::Guts->create_file($self->path, $self->mode, $self->data);
34   +(path_status => $self->path);
35 }
36
37 sub but_add {
38   $_[0]->but(data => $_[0]->data.$_[1]."\n")
39 }
40
41 1;