e6ad2034a1ae4211855d7c4594f6a57e5fe380e2
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSourceHandle.pm
1 package DBIx::Class::ResultSourceHandle;
2
3 use strict;
4 use warnings;
5 use Storable;
6
7 use base qw/DBIx::Class/;
8
9 use overload
10     q/""/ => sub { __PACKAGE__ . ":" . shift->source_moniker; },
11     fallback => 1;
12
13 __PACKAGE__->mk_group_accessors('simple' => qw/schema source_moniker/);
14
15 =head1 NAME
16
17 DBIx::Class::ResultSourceHandle
18
19 =head1 DESCRIPTION
20
21 This module removes fixed link between Rows/ResultSets and the actual source
22 objects, which gets round the following problems
23
24 =over 4
25
26 =item *
27
28 Needing to keep C<$schema> in scope, since any objects/result_sets
29 will have a C<$schema> object through their source handle
30
31 =item *
32
33 Large output when using Data::Dump(er) since this class can be set to
34 stringify to almost nothing
35
36 =item *
37
38 Closer to being able to do a Serialize::Storable that doesn't require class-based connections
39
40 =back
41
42 =head1 METHODS
43
44 =head2 new
45
46 =cut
47
48 sub new {
49     my ($class, $data) = @_;
50
51     $class = ref $class if ref $class;
52
53     bless $data, $class;
54 }
55
56 =head2 resolve
57
58 Resolve the moniker into the actual ResultSource object
59
60 =cut
61
62 sub resolve { return $_[0]->schema->source($_[0]->source_moniker) }
63
64 =head2 STORABLE_freeze
65
66 Freezes a handle.
67
68 =cut
69
70 sub STORABLE_freeze {
71     my ($self, $cloning) = @_;
72     my $to_serialize = { %$self };
73     delete $to_serialize->{schema};
74     return (Storable::freeze($to_serialize));
75 }
76
77 =head2 STORABLE_thaw
78
79 Thaws frozen handle.
80
81 =cut
82
83 sub STORABLE_thaw {
84     my ($self, $cloning,$ice) = @_;
85     %$self = %{ Storable::thaw($ice) };
86 }
87
88 1;