Show
Ignore:
Timestamp:
09/03/08 19:44:42 (6 years ago)
Author:
masaki
Message:

Documents を無くしてシンプルにした

Location:
lang/perl/CouchDB-Object/trunk
Files:
1 removed
8 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/CouchDB-Object/trunk/Makefile.PL

    r18660 r18743  
    44 
    55requires 'Moose'; 
    6 requires 'MooseX::AttributeHelpers'; 
    76requires 'MooseX::Types::URI'; 
    87requires 'Data::Dump::Streamer'; 
  • lang/perl/CouchDB-Object/trunk/lib/CouchDB/Object.pm

    r18660 r18743  
    3333 
    3434    my $res = $self->request(GET => $self->uri_for('_all_dbs')); 
    35     return $self->ping ? map { $self->db($_) } @{ $res->parsed_content } : (); 
     35    return $self->ping ? map { $self->db($_) } @{ $res->content } : (); 
    3636} 
    3737 
  • lang/perl/CouchDB-Object/trunk/lib/CouchDB/Object/Response.pm

    r18660 r18743  
    66use JSON::XS (); 
    77use CouchDB::Object::Document; 
    8 use CouchDB::Object::Documents; 
    98 
    109has 'http_response' => ( 
     
    2928has 'content' => ( 
    3029    is       => 'ro', 
    31     isa      => 'Hash::AsObject', 
     30    isa      => 'Object', # XXX 
    3231    required => 1, 
    3332); 
     
    4039    my ($class, $res) = @_; 
    4140 
    42     my $content = {}; 
    43     $content = JSON::XS->new->decode($res->content) if $res->content_type =~ /json/i; 
    44  
    4541    return $class->new( 
    4642        http_response => $res, 
    47         uri           => $res->request->uri, 
    48         content       => Hash::AsObject->new($content), 
     43        uri           => $res->request->uri->clone, 
     44        content       => $class->parse_content($res), 
    4945    ); 
    5046} 
    5147 
    52 sub to_document { 
    53     my $self = shift; 
     48sub parse_content { 
     49    my ($class, $res) = @_; 
    5450 
    55     if (exists $self->content->{_id}) { 
    56         return CouchDB::Object::Document->new_from_json($self->content); 
     51    my $content = $res->content_type =~ /json/i ? JSON::XS->new->decode($res->content) : {}; 
     52 
     53    if (ref $content->{rows} eq 'ARRAY') { # _all_docs 
     54        my @docs = grep { exists $_->{id} and exists $_->{value} } @{ $content->{rows} }; 
     55        for my $doc (@docs) { 
     56            my $id = delete $doc->{id}; 
     57            $doc = CouchDB::Object::Document->new_from_json($doc->{value}); 
     58            $doc->id($id) if defined $id; 
     59        } 
     60        $content->{rows} = \@docs; 
    5761    } 
    58     elsif (exists $self->content->{rows}) { 
    59         return CouchDB::Object::Documents->new_from_json($self->content); 
     62    elsif (ref $content->{new_revs} eq 'ARRAY') { # _bulk_docs 
     63        for my $doc (@{ $content->{new_revs} }) { 
     64            $doc = CouchDB::Object::Document->new_from_json($doc); 
     65        } 
     66    } 
     67 
     68    if (exists $content->{_id} and exists $content->{_rev}) { 
     69        return CouchDB::Object::Document->new_from_json($content); 
    6070    } 
    6171    else { 
    62         return; 
     72        return Hash::AsObject->new($content); 
    6373    } 
    6474} 
  • lang/perl/CouchDB-Object/trunk/t/00_compile.t

    r17771 r18743  
    11use strict; 
    2 use Test::More tests => 7; 
     2use Test::More tests => 5; 
    33 
    44BEGIN { 
    55    use_ok 'CouchDB::Object'; 
    6     use_ok 'CouchDB::Object::Utils'; 
    7     use_ok 'CouchDB::Object::UserAgent'; 
    8     use_ok 'CouchDB::Object::Server'; 
    96    use_ok 'CouchDB::Object::Database'; 
    107    use_ok 'CouchDB::Object::Document'; 
    118    use_ok 'CouchDB::Object::Response'; 
     9    use_ok 'CouchDB::Object::UserAgent'; 
    1210} 
  • lang/perl/CouchDB-Object/trunk/t/04_open_doc.t

    r18660 r18743  
    88} 
    99else { 
    10     plan tests => 10; 
     10    plan tests => 12; 
    1111} 
    1212 
     
    1818    my $res = $db->open_doc('doc id 4'); 
    1919    ok $res->is_success; # 200 
    20     my $doc = $res->to_document; 
     20    my $doc = $res->content; 
     21    isa_ok $doc => 'CouchDB::Object::Document'; 
    2122    is $doc->id => 'doc id 4'; 
    2223    ok $doc->rev; 
     
    2930    my $res = $db->open_doc('doc id 3'); 
    3031    ok $res->is_success; # 200 
    31     my $doc = $res->to_document; 
     32    my $doc = $res->content; 
     33    isa_ok $doc => 'CouchDB::Object::Document'; 
    3234    is $doc->id => 'doc id 3'; 
    3335    ok $doc->rev; 
  • lang/perl/CouchDB-Object/trunk/t/05_remove_doc.t

    r18660 r18743  
    1616for (1..5) { 
    1717    my $id = "doc id $_"; 
    18     my $doc = $db->open_doc($id)->to_document; 
     18    my $doc = $db->open_doc($id)->content; 
    1919    ok $db->remove_doc($doc)->is_success; # 200 
    2020    ok $db->open_doc($id)->is_error;      # 404 
  • lang/perl/CouchDB-Object/trunk/t/06_all_docs.t

    r18660 r18743  
    88} 
    99else { 
    10     plan tests => 24; 
     10    plan tests => 22; 
    1111} 
    1212 
     
    1818    ok $res->is_success; 
    1919 
    20     my $docs = $res->to_document; 
    21     is $docs->total_docs => 5; 
     20    my $docs = $res->content; 
     21    is $docs->total_rows => 5; 
    2222    is $docs->offset => 0; 
    2323 
    24     is $docs->count => 5; 
    25     for my $doc ($docs->all) { # 2 x 5 = 10 
     24    for my $doc (@{ $docs->rows }) { # 2 x 5 = 10 
    2625        ok $doc->id; 
    2726        ok $doc->rev; 
     
    3332    ok $res->is_success; 
    3433 
    35     my $docs = $res->to_document; 
    36     is $docs->total_docs => 5; 
     34    my $docs = $res->content; 
     35    is $docs->total_rows => 5; 
    3736    is $docs->offset => 0; 
    3837 
    39     is $docs->count => 3; 
    40     for my $doc ($docs->all) { # 2 x 3 = 6 
     38    for my $doc (@{ $docs->rows }) { # 2 x 3 = 6 
    4139        ok $doc->id; 
    4240        ok $doc->rev; 
  • lang/perl/CouchDB-Object/trunk/t/07_query.t

    r18660 r18743  
    11use strict; 
    22use t::CouchDB; 
    3 use String::TT qw(strip tt); 
     3use String::TT qw(strip); 
    44 
    55my $couch = test_couch(); 
     
    99} 
    1010else { 
    11     plan tests => 8; 
     11    plan tests => 7; 
    1212} 
    1313 
     
    1616 
    1717{ # doc 3 only 
    18     my $res = $db->query(strip tt q[ 
     18    my $res = $db->query(strip q[ 
    1919        function(doc) { 
    2020            if (doc.title == "doc 3") { 
     
    2525    ok $res->is_success; 
    2626 
    27     my $docs = $res->to_document; 
    28     is $docs->total_docs => 1; 
     27    my $docs = $res->content; 
     28    is $docs->total_rows => 1; 
    2929    is $docs->offset => 0; 
    30     is $docs->count => 1; 
    3130 
    32     for my $doc ($docs->all) { # 1 x 4 = 4 
     31    for my $doc (@{ $docs->rows }) { # 1 x 4 = 4 
    3332        is $doc->id => 'doc id 3'; 
    3433        ok $doc->rev;