Stancer

 view release on metacpan or  search on metacpan

lib/Stancer/Core/Object.pm  view on Meta::CPAN

    return !1;
}

sub is_not_modified {
    my $this = shift;

    return !$this->is_modified;
}


has populated => (
    is => 'rwp',
    isa => Bool,
    default => 0,
    writer => '_set__populated',
);

sub _set_populated {
    my ($this, $value) = @_;

    $this->_set__populated($value);

    for my $attr (@{$this->_inner_objects}) {
        $this->$attr->_set_populated($value) if defined $this->$attr;
    }

    return $this;
}


sub uri {
    my $this = shift;
    my $config = Stancer::Config->init();
    my @args = (
        $config->uri,
    );

    if ($this->endpoint) {
        push @args, $this->endpoint;
    }

    if ($this->id) {
        push @args, $this->id;
    }

    return join q!/!, @args;
}


sub del {
    my $this = shift;

    return $this unless defined $this->id;

    my $data = Stancer::Core::Request->new->del($this);

    if ($data) {
        $this->hydrate(decode_json $data);
    }

    my @parts = split m/::/sm, ref $this;
    my $class = $parts[-1];

    $log->info(sprintf '%s %s deleted', $class, $this->id);

    $this->clear_id;

    return $this;
}


sub get {
    my ($this, $target) = @_;

    return undef unless defined $this->_api_data;
    return dclone($this->_api_data) unless defined $target;

    my $data = $this->_api_data->{$target};

    return dclone($data) if ref $data ne q//;
    return $data;
}


sub hydrate {
    my ($this, @args) = @_;
    my $data;

    if (scalar @args == 1) {
        $data = $args[0];
    } else {
        $data = {@args};
    }

    $this->_set_process_hydratation(1);

    foreach my $key (keys %{$data}) {
        next if not defined $data->{$key};

        my $setter = '_set_' . $key;

        if (JSON::is_bool($data->{$key})) {
            my $tmp = $data->{$key};

            $data->{$key} = "$tmp";
            $data->{$key} = 1 if "$tmp" eq 'true';
            $data->{$key} = 0 if "$tmp" eq 'false';
        }

        if ($this->can($key) && blessed($this->$key) && $this->$key->can('hydrate')) {
            if (ref $data->{$key} eq 'HASH') {
                $this->$key->hydrate($data->{$key});
            } else {
                $this->$key->hydrate(id => $data->{$key});
            }
        } elsif ($this->can($setter)) {
            $this->$setter($data->{$key});
        } elsif ($this->can($key)) {
            $this->$key($data->{$key});
        }
    }

lib/Stancer/Core/Object.pm  view on Meta::CPAN

    return $this if !$this->id || $this->populated || !$this->endpoint;

    my $request = Stancer::Core::Request->new();
    my $data = $request->get($this);

    $this->_set_populated(1);

    if ($data) {
        my $decoded = decode_json $data;

        $this->_api_data($decoded);
        $this->hydrate($decoded);

        for my $attr (@{$this->_inner_objects}) {
            if (defined $this->{$attr} && defined $decoded->{$attr}) {
                $this->{$attr}->_api_data($decoded->{$attr});
            }
        }
    }

    $this->_reset_modified();

    return $this;
}


sub save {
    my $this = shift;

    carp '"save" method is deprecated and will be removed in a later release, use the "send" method instead';

    return $this->send();
}


sub send { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
    my $this = shift;

    return $this if $this->is_not_modified;

    my $request = Stancer::Core::Request->new();
    my $data;
    my $verb;

    if (defined $this->id) {
        $data = $request->patch($this);
        $verb = 'updated';
    } else {
        $data = $request->post($this);
        $verb = 'created';
    }

    $this->_set_populated(1);

    if ($data) {
        $this->hydrate(decode_json $data);
    }

    $this->_reset_modified();

    my @parts = split m/::/sm, ref $this;
    my $class = $parts[-1];

    $log->info(sprintf '%s %s %s', $class, $this->id, $verb);

    return $this;
}


## no critic (Capitalization)
sub toJSON {
    my $this = shift;

    return JSON->new->convert_blessed->canonical->encode($this);
}
## use critic


sub to_hash {
    my $this = shift;
    my $attrs = {};
    my @properties = keys %{$this->populate()};

    foreach my $attr (sort @properties) {
        next if any { $_ eq $attr } qw(endpoint populated refunds); # remove ignored attributes
        next if $attr =~ m/^_/sm; # remove private attributes

        if (blessed($this->$attr) && $this->$attr->isa(__PACKAGE__)) {
            $attrs->{$attr} = $this->$attr->to_hash;
        } else {
            $attrs->{$attr} = $this->$attr;
        }

        if (any { $_ eq $attr } @{$this->_boolean}) { # Parse boolean
            my $tmp = $this->$attr;

            $attrs->{$attr} = \1 if "$tmp" eq '1';
            $attrs->{$attr} = \0 if "$tmp" eq '0';
        }

        if (any { $_ eq $attr } @{$this->_integer}) { # Force integer
            $attrs->{$attr} *= 1;
        }
    }

    return $attrs;
}


sub TO_JSON {
    my $this = shift;
    my $attrs = {};
    my @properties = keys %{$this};

    if ($this->id) {
        @properties = keys %{$this->_modified};

        return $this->id() if $this->is_not_modified;
    }

    foreach my $attr (sort @properties) {



( run in 1.025 second using v1.01-cache-2.11-cpan-71847e10f99 )