| 1 | package Rose::DBx::Object::Indexed; | ||
| 2 | |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use base qw( Rose::DB::Object ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | use Carp; | ||
| 7 | use Class::C3; | ||
| 8 | use Rose::DBx::Object::Indexed::Indexer; | ||
| 9 | |||
| 10 | use Rose::Object::MakeMethods::Generic ( | ||
| 11 | boolean => [ 'index_eligible' => { default => 0 }, ], | ||
| 12 | 'scalar --get_set_init' => 'indexer', | ||
| 13 | 'scalar --get_set_init' => 'indexer_class', | ||
| 14 | ); | ||
| 15 | |||
| 16 | our $VERSION = '0.009'; | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Rose::DBx::Object::Indexed - full-text search for RDBO classes | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | package MyRDBO; | ||
| 25 | use strict; | ||
| 26 | use base qw( Rose::DBx::Object::Indexed ); | ||
| 27 | |||
| 28 | sub index_eligible { 1 } | ||
| 29 | |||
| 30 | 1; | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Rose::DBx::Object::Indexed is a base class like Rose::DB::Object, | ||
| 35 | with the added feature that your objects are added to a full-text index | ||
| 36 | every time they are saved. | ||
| 37 | |||
| 38 | The idea is that you can provide full-text search for your database simply | ||
| 39 | by subclassing Rose::DBx::Object::Indexed instead of Rose::DB::Object | ||
| 40 | directly. | ||
| 41 | |||
| 42 | =head1 METHODS | ||
| 43 | |||
| 44 | Only new or overridden method are documented here. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | =head2 init_index_eligible | ||
| 49 | |||
| 50 | Boolean indicating whether this class should be indexed on insert, | ||
| 51 | update and delete. | ||
| 52 | |||
| 53 | The default is false (off), which means that even though you may | ||
| 54 | subclass Rose::DBx::Object::Indexed, you must still turn the flag | ||
| 55 | on by setting index_eligible() to true in the specific subclasses | ||
| 56 | you want included in the index. This allows you to have a common base | ||
| 57 | class that inherits from Rose::DBx::Object::Indexed and then selectively | ||
| 58 | index various subclasses of your common base class. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub init_index_eligible {0} | ||
| 63 | |||
| 64 | =head2 init_indexer_class | ||
| 65 | |||
| 66 | Returns the name of the indexer class. | ||
| 67 | The default is 'Rose::DBx::Object::Indexed::Indexer'. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub init_indexer_class {'Rose::DBx::Object::Indexed::Indexer'} | ||
| 72 | |||
| 73 | =head2 init_indexer( I<args> ) | ||
| 74 | |||
| 75 | Returns a new instance of indexer_class(). I<args> are passed through to | ||
| 76 | the indexer_class new() method. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub init_indexer { | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | # call as class or object method just for kinder api | ||
| 84 | $self = $self->new unless ref($self); | ||
| 85 | |||
| 86 | $self->indexer_class->new(@_); | ||
| 87 | } | ||
| 88 | |||
| 89 | =head2 insert | ||
| 90 | |||
| 91 | Calls through to next::method and then indexes the object if index_eligible() is true. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub insert { | ||
| 96 | my $self = shift; | ||
| 97 | my $ret = $self->next::method(@_); | ||
| 98 | if ( $ret and $self->index_eligible ) { | ||
| 99 | $self->write_index('insert'); | ||
| 100 | } | ||
| 101 | return $ret; | ||
| 102 | } | ||
| 103 | |||
| 104 | =head2 update | ||
| 105 | |||
| 106 | Calls through to next::method and then indexes the object if index_eligible() is true. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub update { | ||
| 111 | my $self = shift; | ||
| 112 | my $ret = $self->next::method(@_); | ||
| 113 | if ( $ret and $self->index_eligible ) { | ||
| 114 | $self->write_index('update'); | ||
| 115 | } | ||
| 116 | return $ret; | ||
| 117 | } | ||
| 118 | |||
| 119 | =head2 delete | ||
| 120 | |||
| 121 | Calls through to next::method and then indexes the object if index_eligible() is true. | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub delete { | ||
| 126 | my $self = shift; | ||
| 127 | my $ret = $self->next::method(@_); | ||
| 128 | if ( $ret and $self->index_eligible ) { | ||
| 129 | $self->write_index('delete'); | ||
| 130 | } | ||
| 131 | return $ret; | ||
| 132 | } | ||
| 133 | |||
| 134 | =head2 write_index( I<mode> ) | ||
| 135 | |||
| 136 | Passes the object ($self) on to the indexer method I<mode>. This method | ||
| 137 | is called internally by insert(), update() and delete(). | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub write_index { | ||
| 142 | my $self = shift; | ||
| 143 | my $mode = shift || 'insert'; | ||
| 144 | $self->indexer->$mode($self); | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; | ||
| 148 | |||
| 149 | __END__ | ||