package DX::Lib::FS::Role::RunOn;

use DX::Lib::FS::Guts;
use Moo::Role;

my $local = DX::Lib::FS::Guts->new;
my %conn_cache; # HEINOUS

sub _call_guts {
  my ($self, $type, @args) = @_;
  my ($on, $path) = $self->path =~ /^(?:([^\/]+):)?(.*)$/;
  my $guts = $on ? $self->_conn_to($on) : $local;
  $guts->$type($path, @args);
}

sub _conn_to {
  my ($self, $on) = @_;
  $conn_cache{$on} ||= do {
    require Object::Remote;
    DX::Lib::FS::Guts->new::on($on);
  };
}

1;
