Dallycot

 view release on metacpan or  search on metacpan

lib/Dallycot/Value/URI.pm  view on Meta::CPAN

package Dallycot::Value::URI;
our $AUTHORITY = 'cpan:JSMITH';

# ABSTRACT: A URI value that can be dereferenced

use strict;
use warnings;

use utf8;
use parent 'Dallycot::Value::Any';

use Dallycot::Registry;
use Promises qw(deferred);
use Scalar::Util qw(blessed);
use URI;

use experimental qw(switch);

sub new {
  my ( $class, $uri ) = @_;

  $class = ref $class || $class;

  $uri = URI->new($uri)->canonical;

  return bless [$uri] => $class;
}

sub to_rdf {
  my( $self, $model ) = @_;

  return RDF::Trine::Node::Resource->new($self->[0]->as_string);
}

sub calculate_length {
  my ( $self, $engine ) = @_;

  return Dallycot::Value::Numeric->new( length $self->[0]->as_string );
}

sub value_at {
  my ( $self, $engine, $index ) = @_;

  my $d = deferred;

  if($index > length($self -> [0] -> as_string)) {
    $d -> resolve($engine -> UNDEFINED);
  }
  else {
    $d->resolve(
      bless [ substr( $self->[0]->as_string, $index - 1, 1 ), 'en' ] => 'Dallycot::Value::String' );
  }

  return $d->promise;
}

sub id {
  my ($self) = @_;

  return "<" . $self->[0]->as_string . ">";
}

sub as_text {
  my ($self) = @_;

  return $self->id;
}

sub is_lambda {
  my ($self) = @_;

  my ( $lib, $method ) = $self->_get_library_and_method;

  return unless defined $lib;
  return $lib->get_assignment($method)->then(
    sub {
      my ($def) = @_;

      return unless blessed($def);
      return 1 if $def->isa(__PACKAGE__);
      return $def->is_lambda;
    }
  );
}

sub is_defined { return 1 }



( run in 0.889 second using v1.01-cache-2.11-cpan-437f7b0c052 )