ActiveResource

 view release on metacpan or  search on metacpan

lib/ActiveResource/Base.pm  view on Meta::CPAN

        $u->query_form(%$query_options);
        $path = $path . $u->as_string
    }
    return $path;
}

sub element_path {
    my ($class, $id, $prefix_options, $query_options) = @_;
    my $resource_name = PL lc(ref($class) || $class);
    my $path = "/${resource_name}/${id}.xml";
    if ($prefix_options) {
        my ($k, $v) = each %$prefix_options;
        $k =~ s/_id$//s;
        my $prefix_resource_name = PL lc $k;
        $path = "/${prefix_resource_name}/${v}" . $path;
    }
    if ($query_options) {
        my $u = URI->new;
        $u->query_form(%$query_options);
        $path = $path . $u->as_string
    }
    return $path;
}

sub load {
    my ($self, $attr) = @_;
    my $a = {};
    while(my($name, $value) = each %$attr) {
        $a->{$name} = !ref($value) ? $value : $value->{text} || Hash::AsObject->new($value);
    }
    $self->attributes = $a;
    return $self;
}

sub encode {
    my ($self) = @_;
    $self->format->encode($self->attributes);
}

sub AUTOLOAD {
    no strict;
    local $, = ", ";
    my $self = shift;
    my @args = @_;

    my ($sub) = ${__PACKAGE__."::AUTOLOAD"} =~ /::(.+?)$/;

    $self->attributes = {} unless $self->attributes;

    if (@args == 1) {
        $self->attributes->{$sub} = ref($args[0]) ? $args[0] : { text => $args[0] };
        return $self;
    }

    my $attr = $self->attributes->{$sub};
    return $attr if !ref $attr;
    return $attr->{text} if $attr->{text};
    return Hash::AsObject->new($attr);
}

# protected methods start from here

sub load_attributes_from_response {
    my ($self, $response) = @_;
    my $record_xml = $response->content;

    return unless $record_xml;

    my $hash = $self->format->decode($record_xml);
    my (undef, $attr) = each %$hash;
    $self->load($attr);
}

# end of protected methods

1;



( run in 0.843 second using v1.01-cache-2.11-cpan-39bf76dae61 )