From: David Kamholz Date: Fri, 3 Feb 2006 15:14:44 +0000 (+0000) Subject: add Serialize and ResultSetManager (neither complete but this will let people hack... X-Git-Tag: v0.05005~58 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ed28f83099a03e3ec42f55b404146e088c74c133;p=dbsrgits%2FDBIx-Class.git add Serialize and ResultSetManager (neither complete but this will let people hack on them) --- diff --git a/lib/DBIx/Class/ResultSetManager.pm b/lib/DBIx/Class/ResultSetManager.pm new file mode 100644 index 0000000..b3cd2b6 --- /dev/null +++ b/lib/DBIx/Class/ResultSetManager.pm @@ -0,0 +1,60 @@ +package DBIx::Class::MethodAttributes; +use strict; +use base 'DBIx::Class'; +use Class::Inspector; + +__PACKAGE__->mk_classdata($_) for qw/ _attr_cache custom_resultset_class_suffix /; +__PACKAGE__->_attr_cache({}); +__PACKAGE__->custom_resultset_class_suffix('::_rs'); + +sub base_resultset_class { + my ($self,$class) = @_; + $self->result_source_instance->resultset_class($class); +} + +sub table { + my ($self,@rest) = @_; + $self->next::method(@rest); + $self->_register_attributes; +} + +sub load_resultset_components { + my ($self,@comp) = @_; + my $resultset_class = $self->_setup_resultset_class; + $resultset_class->load_components(@comp); +} + +sub MODIFY_CODE_ATTRIBUTES { + my ($class,$code,@attrs) = @_; + $class->_attr_cache({ %{$class->_attr_cache}, $code => [@attrs] }); + return (); +} + +sub _register_attributes { + my $self = shift; + my $cache = $self->_attr_cache; + foreach my $meth (@{Class::Inspector->methods($self) || []}) { + my $attrs = $cache->{$self->can($meth)}; + next unless $attrs; + if ($attrs->[0] eq 'resultset') { + no strict 'refs'; + my $resultset_class = $self->_setup_resultset_class; + *{"$resultset_class\::$meth"} = *{"$self\::$meth"}; + undef *{"$self\::$meth"}; + } + } + $self->_attr_cache(undef); +} + +sub _setup_resultset_class { + my $self = shift; + my $resultset_class = $self . $self->custom_resultset_class_suffix; + no strict 'refs'; + unless (@{"$resultset_class\::ISA"}) { + @{"$resultset_class\::ISA"} = ($self->result_source_instance->resultset_class); + $self->result_source_instance->resultset_class($resultset_class); + } + return $resultset_class; +} + +1; \ No newline at end of file diff --git a/lib/DBIx/Class/Serialize.pm b/lib/DBIx/Class/Serialize.pm new file mode 100644 index 0000000..7fb4147 --- /dev/null +++ b/lib/DBIx/Class/Serialize.pm @@ -0,0 +1,22 @@ +package DBIx::Class::Serialize; +use strict; +use Storable qw/freeze thaw/; + +sub STORABLE_freeze { + my ($self,$cloning) = @_; + return if $cloning; + my $to_serialize = { %$self }; + delete $to_serialize->{result_source}; + return (freeze($to_serialize)); +} + +sub STORABLE_thaw { + my ($self,$cloning,$serialized) = @_; + %$self = %{ thaw($serialized) }; + no strict 'refs'; + my $class = ${(ref $self) . '::ISA'}[0]; + my $schema = DB->schema; + $self->result_source($schema->source($class)); +} + +1; \ No newline at end of file