basically works
[dbsrgits/DBIx-Class-ResultSet-WithMetaData.git] / lib / DBIx / Class / ResultSet / WithMetaData.pm
1 package DBIx::Class::ResultSet::WithMetaData;
2
3 use strict;
4 use warnings;
5
6 use Data::Alias;
7 use Moose;
8 use MooseX::Method::Signatures;
9 extends 'DBIx::Class::ResultSet';
10
11 has '_row_info' => (
12         is => 'rw',
13         isa => 'HashRef'
14 );
15
16 has 'was_row' => (
17         is => 'rw',
18         isa => 'Int'
19 );
20
21 has 'id_cols' => (
22         is => 'rw',
23         isa => 'ArrayRef',
24 );
25         
26
27 sub new {
28         my $self = shift;
29
30         my $new = $self->next::method(@_);
31         foreach my $key (qw/_row_info was_row id_cols/) {
32                 alias $new->{$key} = $new->{attrs}{$key};
33         }
34
35         unless ($new->_row_info) {
36                 $new->_row_info({});
37         }
38
39         unless ($new->id_cols && scalar(@{$new->id_cols})) {
40                 $new->id_cols([sort $new->result_source->primary_columns]);
41         }
42
43         return $new;
44 }
45
46 method display () {
47   my $rs = $self->search({});
48   $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
49   my @rows;
50         foreach my $row ($rs->all) {
51                 if (my $info = $self->row_info_for(id => $self->_mk_id(row => $row))) {
52                         $row = { %{$row}, %{$info} };
53                 }
54                 push(@rows, $row);
55         }
56
57   return ($self->was_row) ? $rows[0] : \@rows;
58 }
59
60 method add_row_info (Int :$id, HashRef :$info) {
61         unless ($self->find($id)) {
62                 warn $id;
63                 die 'invalid id passed to add_row_info';
64         }
65
66         if (my $existing = $self->_row_info->{$id}) {
67                 $info = { %{$existing}, %{$info} };
68         }
69
70         $self->_row_info->{$id} = $info;        
71 }
72
73 method row_info_for (Int :$id) {
74         return $self->_row_info->{$id};
75 }
76
77 method order_by (Str :$col) {
78         $col = "me.$col" unless ($col =~ m/\./);
79         return $self->search({}, { order_by => $col });
80 }
81
82 method limit (Int :$count) {
83         return $self->search({}, { rows => $count });
84 }
85
86 method with_token {
87         foreach my $row ($self->all) {
88                 my $token = $self->tokenify($row->get_column($self->token_col));
89                 $self->add_row_info(id => $row->id, info => { join('_', 'token', $self->token_col) => $token });
90         }
91
92         return $self;
93 }
94
95 method _mk_id (HashRef :$row) {
96         return join('-', map { $row->{$_} } @{$self->id_cols});
97 }
98         
99
100 sub tokenify {
101   my ($self, $string) = @_;
102
103   $string =~ s/\s+$//;
104   $string =~ s/[,\.\-\+]//g;
105   $string =~ s/^\s+//;
106   $string =~ s/ /-/g;
107   $string = lc($string);
108   return $string;
109 }
110
111 sub clean_rs {
112   my $self = shift;
113
114   return $self->result_source->resultset;
115 }
116
117 1;