DBIx-Class-Indexer-WebService-Solr

 view release on metacpan or  search on metacpan

lib/DBIx/Class/Indexer/WebService/Solr.pm  view on Meta::CPAN

            @values = grep { defined }
                 map  {
                       Scalar::Util::blessed( $_ ) and $_->can( $accessor ) ? $_->$accessor
                     : ref $_ eq 'HASH' ? $_->{ $accessor }
                     : undef
                 } @values;
        }
        return wantarray ? @values : $values[ 0 ];
    }
}

=head2 as_document( $object )

Constructs a new WebService::Solr::Document object, populates it with
data from C<$object>, and returns it.

=cut

sub as_document {
    my( $self, $object ) = @_;
    my $document = WebService::Solr::Document->new;

    # this is basically a no-op if it's already been done
    # but it needs to be done in case the same indexer is
    # used for multiple sources
    $self->setup_fields( ref $object );
    
    my $fields = $object->index_fields;

    # for each field...
    for my $name ( keys %$fields ) {
        my $opts    = $fields->{$name};
        my @values  = $self->value_for_field( $object, $name );
    
        for( @values ) {
            $document->add_fields( [ $name => $_, $opts ] );
        }
    }

    return $document;
}

=head2 insert( $object )

Calls C<update_or_create_document>.

=cut

sub insert {
    my $self   = shift;
    my $object = shift;
    $self->update_or_create_document( $object );
}

=head2 update( $object )

Calls C<update_or_create_document>.

=cut

sub update {
    my $self   = shift;
    my $object = shift;
    
    $self->update_or_create_document( $object );
}

=head2 delete( $object )

Deletes document from the index.

=cut

sub delete {
    my $self   = shift;
    my $object = shift;
    my $solr  = $self->_obj;

    $self->setup_fields( ref $object );
    my $id = $self->value_for_field( $object, 'id' );

    $solr->delete_by_id( $id );  
}

=head2 update_or_create_document( $object )

Will either update or add a document to the index.

=cut

sub update_or_create_document {
    my $self   = shift;
    my $object = shift;
    my $solr  = $self->_obj;

    $self->setup_fields( ref $object );

    # add == update
    $solr->add( $self->as_document( $object ) );
}

=head1 SEE ALSO

=over 4

=item * DBIx::Class

=item * DBIx::Class::Indexed

=item * WebService::Solr

=back

=head1 AUTHOR

Brian Cassidy E<lt>bricas@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2008-2009 by Brian Cassidy

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. 

=cut

1;



( run in 2.309 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )