Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7aec872
Showing
20 changed files
with
611 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Revision history for DBIx-Class-IndexSearch-Dezi | ||
|
||
0.01 Date/time | ||
First version, released on an unsuspecting world. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Changes | ||
lib/DBIx/Class/IndexSearch/Dezi.pm | ||
Makefile.PL | ||
MANIFEST This list of files | ||
README | ||
t/00-load.t | ||
t/manifest.t | ||
t/pod-coverage.t | ||
t/pod.t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use inc::Module::Install; | ||
|
||
name 'DBIx-Class-IndexSearch-Dezi'; | ||
all_from 'lib/DBIx/Class/IndexSearch/Dezi.pm'; | ||
author q{Logan Bell <loganbell@gmail.com>}; | ||
license 'perl'; | ||
|
||
build_requires 'Test::More'; | ||
|
||
auto_install; | ||
|
||
WriteAll; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
DBIx-Class-IndexSearch-Dezi | ||
|
||
The README is used to introduce the module and provide instructions on | ||
how to install the module, any machine dependencies it may have (for | ||
example C compilers and installed libraries) and any other information | ||
that should be provided before the module is installed. | ||
|
||
A README file is required for CPAN modules since CPAN extracts the README | ||
file from a module distribution so that people browsing the archive | ||
can use it to get an idea of the module's uses. It is usually a good idea | ||
to provide version information here so that people can decide whether | ||
fixes for the module are worth downloading. | ||
|
||
|
||
INSTALLATION | ||
|
||
To install this module, run the following commands: | ||
|
||
perl Makefile.PL | ||
make | ||
make test | ||
make install | ||
|
||
SUPPORT AND DOCUMENTATION | ||
|
||
After installing, you can find documentation for this module with the | ||
perldoc command. | ||
|
||
perldoc DBIx::Class::IndexSearch::Dezi | ||
|
||
You can also look for information at: | ||
|
||
RT, CPAN's request tracker (report bugs here) | ||
http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-IndexSearch-Dezi | ||
|
||
AnnoCPAN, Annotated CPAN documentation | ||
http://annocpan.org/dist/DBIx-Class-IndexSearch-Dezi | ||
|
||
CPAN Ratings | ||
http://cpanratings.perl.org/d/DBIx-Class-IndexSearch-Dezi | ||
|
||
Search CPAN | ||
http://search.cpan.org/dist/DBIx-Class-IndexSearch-Dezi/ | ||
|
||
|
||
LICENSE AND COPYRIGHT | ||
|
||
Copyright (C) 2011 Logan Bell | ||
|
||
This program is free software; you can redistribute it and/or modify it | ||
under the terms of either: the GNU General Public License as published | ||
by the Free Software Foundation; or the Artistic License. | ||
|
||
See http://dev.perl.org/licenses/ for more information. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
blib* | ||
Makefile | ||
Makefile.old | ||
Build | ||
Build.bat | ||
_build* | ||
pm_to_blib* | ||
*.tar.gz | ||
.lwpcookies | ||
cover_db | ||
pod2htm*.tmp | ||
DBIx-Class-IndexSearch-Dezi-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package DBIx::Class::IndexSearch; | ||
use Moo; | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
package DBIx::Class::IndexSearch::Dezi; | ||
use Moose; | ||
use MooseX::ClassAttribute; | ||
use Carp; | ||
extends 'DBIx::Class'; | ||
|
||
|
||
=head1 NAME | ||
DBIx::Class::IndexSearch::Dezi - The great new DBIx::Class::IndexSearch::Dezi! | ||
=head1 VERSION | ||
Version 0.01 | ||
=cut | ||
|
||
our $VERSION = '0.01'; | ||
|
||
=head1 SYNOPSIS | ||
package MyApp::Schema::Person; { | ||
use base 'DBIx::Class'; | ||
__PACKAGE__->load_components(qw[ | ||
IndexSearch::Dezi | ||
PK::Auto | ||
Core | ||
TimeStamp | ||
]); | ||
__PACKAGE__->table('person'); | ||
__PACKAGE__->add_columns( | ||
person_id => { | ||
data_type => 'varchar', | ||
size => '36', | ||
is_nullable => 0, | ||
}, | ||
name => { | ||
data_type => 'varchar', | ||
is_nullable => 0, | ||
indexed => 1 | ||
}, | ||
age => { | ||
data_type => 'integer', | ||
is_nullable => 0, | ||
}, | ||
email => { | ||
data_type => 'varchar', | ||
size=>'128', | ||
}, | ||
created => { | ||
data_type => 'timestamp', | ||
set_on_create => 1, | ||
is_nullable => 0, | ||
}, | ||
); | ||
__PACKAGE__->resultset_class('DBIx::Class::IndexSearch::ResultSet::Dezi'); | ||
__PACKAGE__->belongs_to_index('FooClient', { server => 'http://localhost:6000', map_to => 'person_id' }); | ||
=head1 ATTRIBUTES | ||
=head2 index_fields | ||
Registers index fields. | ||
=cut | ||
class_has 'index_fields' => ( | ||
traits => ['Hash'], | ||
is => 'rw', | ||
isa => 'HashRef[Str]', | ||
default => sub { {} }, | ||
handles => { | ||
set_index_field => 'set', | ||
get_index_field => 'get', | ||
get_index_keys => 'keys', | ||
index_key_exist => 'exists', | ||
} | ||
); | ||
|
||
class_has 'webservice_class' => ( | ||
is => 'rw' | ||
); | ||
|
||
class_has 'query_parameters' => ( | ||
is => 'rw' | ||
); | ||
|
||
class_has 'map_to' => ( | ||
is => 'rw' | ||
); | ||
|
||
class_has 'webservice' => ( | ||
is => 'rw', | ||
lazy => 1, | ||
builder => '_build_webservice' | ||
); | ||
|
||
sub _build_webservice { | ||
my ( $class ) = @_; | ||
|
||
my $package_name = $class->webservice_class; | ||
|
||
eval "require $package_name"; | ||
croak "Failed to load indexer: $@" if $@; | ||
|
||
return $package_name->new( server => $class->query_parameters->{server} ) ; | ||
} | ||
|
||
=head1 SUBROUTINES/METHODS | ||
=head2 register_column ( $column, \%info ) | ||
Overrides DBIx::Class's C<register_column>. If %info contains | ||
the key 'indexed', calls C<register_field>. | ||
=cut | ||
|
||
sub register_column { | ||
my ( $class, $column, $info ) = @_; | ||
|
||
$class->next::method( $column, $info ); | ||
|
||
if (exists $info->{ indexed }) { | ||
$class->set_index_field( $column => $info->{ indexed } ); | ||
} | ||
|
||
} | ||
|
||
=head2 register_column ( $class, $webservice_class, \%parameters ) | ||
This sets up the the webservice to use and maps the webservice index | ||
to the DB. | ||
=cut | ||
|
||
sub belongs_to_index { | ||
my ( $class, $webservice_class, $parameters ) = @_; | ||
|
||
croak 'Please specify a webservice' if !$webservice_class; | ||
croak 'Please supply hostname ' if !$parameters->{server}; | ||
croak 'Please supply map_to ' if !$parameters->{map_to}; | ||
|
||
$class->webservice_class( $webservice_class ); | ||
$class->query_parameters( $parameters || {} ); | ||
$class->map_to( $parameters->{map_to} || '' ); | ||
} | ||
|
||
=head1 AUTHOR | ||
Logan Bell, C<< <loganbell at gmail.com> >> | ||
=head1 BUGS | ||
Please report any bugs or feature requests to C<bug-dbix-class-indexsearch-dezi at rt.cpan.org>, or through | ||
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-IndexSearch-Dezi>. I will be notified, and then you'll | ||
automatically be notified of progress on your bug as I make changes. | ||
=head1 SUPPORT | ||
You can find documentation for this module with the perldoc command. | ||
perldoc DBIx::Class::IndexSearch::Dezi | ||
You can also look for information at: | ||
=over 4 | ||
=item * RT: CPAN's request tracker (report bugs here) | ||
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-IndexSearch-Dezi> | ||
=item * AnnoCPAN: Annotated CPAN documentation | ||
L<http://annocpan.org/dist/DBIx-Class-IndexSearch-Dezi> | ||
=item * CPAN Ratings | ||
L<http://cpanratings.perl.org/d/DBIx-Class-IndexSearch-Dezi> | ||
=item * Search CPAN | ||
L<http://search.cpan.org/dist/DBIx-Class-IndexSearch-Dezi/> | ||
=back | ||
=head1 ACKNOWLEDGEMENTS | ||
=head1 LICENSE AND COPYRIGHT | ||
Copyright 2011 Logan Bell. | ||
This program is free software; you can redistribute it and/or modify it | ||
under the terms of either: the GNU General Public License as published | ||
by the Free Software Foundation; or the Artistic License. | ||
See http://dev.perl.org/licenses/ for more information. | ||
=cut | ||
|
||
1; # End of DBIx::Class::IndexSearch::Dezi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package DBIx::Class::IndexSearch::ResultSet::Dezi; | ||
|
||
use Moose; | ||
extends 'DBIx::Class::ResultSet'; | ||
use Carp; | ||
|
||
=head1 SUBROUTINES/METHODS | ||
=head2 dezi_search ( \%search, \%attributes ) | ||
Searches the Dezi webservice for results and maps the results over | ||
to inflate objects. | ||
=cut | ||
sub search_dezi { | ||
my ( $self, $search, $attributes ) = @_; | ||
|
||
my $result_class = $self->result_class; | ||
|
||
my @search = ref $search eq 'ARRAY' ? @{$search} : | ||
ref $search eq 'HASH' ? %{$search} : | ||
croak 'search_dezi only accepts an arrayref or a hashref'; | ||
|
||
my $query = []; | ||
|
||
my $map_field = $result_class->map_to; | ||
my @ids; | ||
|
||
while ( my $column = shift @search ) { | ||
my $value = shift @search; | ||
if ( $result_class->index_key_exist($column) ) { | ||
my $dezi = $result_class->webservice; | ||
my $response = $dezi->search( q => $value ); | ||
|
||
@ids = map { | ||
$_->{$map_field}->[0] | ||
} | ||
@{$response->results}; | ||
} | ||
} | ||
|
||
return $self->search({ $map_field => \@ids }); | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!perl -T | ||
|
||
use Test::More tests => 1; | ||
|
||
BEGIN { | ||
use_ok( 'DBIx::Class::IndexSearch::Dezi' ) || print "Bail out!\n"; | ||
} | ||
|
||
diag( "Testing DBIx::Class::IndexSearch::Dezi $DBIx::Class::IndexSearch::Dezi::VERSION, Perl $], $^X" ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use Test::More; | ||
|
||
use lib 't/lib'; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
BEGIN { | ||
use_ok('Test'); | ||
} | ||
|
||
my $schema = Test->initialize; | ||
|
||
my $resultset = $schema->resultset('Person'); | ||
|
||
my $person0 = $resultset->new({ | ||
name => 'FooBar', | ||
age => 18, | ||
}); | ||
|
||
$person0->insert; | ||
|
||
ok $resultset->search_dezi( { name => 'Foo*' } ); | ||
|
||
done_testing; | ||
|
||
|
Oops, something went wrong.