Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixed documents for release 0.06

  • Loading branch information...
commit b133ed9785a8b5f637f8d8f70ce0d6f88feb906a 1 parent 18c695d
Kentaro Kuribayashi authored
8 Changes
... ... @@ -1,5 +1,13 @@
1 1 Revision history for Perl extension Data::Mapper
2 2
  3 +0.06 2012-03-04
  4 + - data now can be a Hash-based POPO not apply only to subclass of
  5 + Data::Mapper::Data (Naoki Tomita: http://e8y.net/; hereinafter the
  6 + same)
  7 + - keys in data which start with `_' are regarded as private
  8 + - data_class() method is now public. you can customize which
  9 + data class to be mapped
  10 +
3 11 0.05 2012-02-21
4 12 - fixed a bug: data_class now caches class name by full-qualified one
5 13
58 lib/Data/Mapper.pm
@@ -71,20 +71,22 @@ sub adapter {
71 71 $self->{adapter} || die 'You must set an adapter first';
72 72 }
73 73
74   -### PRIVATE_METHODS ###
75   -
76 74 our %DATA_CLASSES = ();
77 75 sub data_class {
78 76 my ($self, $name) = @_;
79 77
80 78 $DATA_CLASSES{ref $self}{$name} ||= do {
81 79 my $data_class = join '::', (ref $self), 'Data', $self->to_class_name($name);
  80 +
82 81 eval { Class::Load::load_class($data_class) };
83 82 Carp::croak("no such data class: $data_class for $name") if $@;
  83 +
84 84 $data_class;
85 85 }
86 86 }
87 87
  88 +### PRIVATE_METHODS ###
  89 +
88 90 sub to_class_name {
89 91 my ($self, $name) = @_;
90 92 return $name if !$name;
@@ -116,10 +118,12 @@ sub map_data {
116 118 if (Scalar::Util::blessed($data)) {
117 119 if ($data->can('as_serializable')) {
118 120 $data = $data->as_serializable;
119   - } elsif (Scalar::Util::reftype($data) eq 'HASH') {
  121 + }
  122 + elsif (Scalar::Util::reftype($data) eq 'HASH') {
120 123 $data = $self->as_serializable($data);
121   - } else {
122   - Carp::croak('blessed data must be blessed hashref or have as_serializable method');
  124 + }
  125 + else {
  126 + Carp::croak('$data must be either a Hash-based object or a plain HashRef');
123 127 }
124 128 }
125 129
@@ -133,18 +137,24 @@ sub mapped_params {
133 137 or Carp::croak("no such table: $table");
134 138
135 139 my $primary_keys = $schema->primary_keys;
136   - die "Data::Mapper doesn't support tables have no primary keys"
  140 + die "Data::Mapper doesn't support tables which have no primary keys"
137 141 if !scalar @$primary_keys;
138 142
139 143 my $result = { set => {}, where => {}, table => $table };
140   -
  144 +
  145 + # Data::Mapper::Data-based object
141 146 if ($data->isa('Data::Mapper::Data')) {
142 147 $result->{set} = $data->changes;
  148 +
143 149 for my $key (@$primary_keys) {
144 150 $result->{where}{$key} = $data->param($key);
145 151 }
146   - } else {
147   - $result->{set} = $self->as_serializable($data); # everything
  152 + }
  153 +
  154 + # Hash-based POPO
  155 + else {
  156 + $result->{set} = $self->as_serializable($data);
  157 +
148 158 for my $key (@$primary_keys) {
149 159 $result->{where}{$key} = $data->{$key};
150 160 }
@@ -282,8 +292,8 @@ Deletes the C<$data> from the datasource.
282 292 =head2 Adapter
283 293
284 294 I<Adapter> does CRUD operations against a datasource (database,
285   -memcached, etc.). It must implement some methods according to the
286   -convention.
  295 +memcached, external API, etc.). It must implement some methods
  296 +according to the convention.
287 297
288 298 I<Adapter> must implements the methods below:
289 299
@@ -315,9 +325,10 @@ Deletes the data specified by C<\%conditions> from a datasource.
315 325
316 326 =back
317 327
318   -The return value of C<create()>, C<find()>, C<search()> is either a
319   -HashRef or an object which has C<as_serializable()> method to return
320   -its contents as a HashRef.
  328 +The return value of C<create()>, C<find()>, C<search()> must be either
  329 +a plain HashRef or a Hash-based object. If the object has
  330 +C<as_serializable()>, it'll be called before mapping to extract data
  331 +as a HashRef.
321 332
322 333 You can adapt any data-retrieving module to Data::Model convention if
323 334 only you implement the methods described above.
@@ -326,15 +337,23 @@ only you implement the methods described above.
326 337
327 338 I<Data> represents a data model where you can define some business
328 339 logic. You must notice that I<Data> layer has no idea about what
329   -I<Mapper> and I<Adapter> are. It just hold the data passed by
  340 +I<Mapper> and I<Adapter> are. It just holds the data passed by
330 341 I<Mapper>
331 342
332   -I<Mapper> returns some instance whose class inherits
333   -I<Data::Mapper::Data> object.
  343 +I<Data> can be either I<Data::Mapper::Data>-based object or your own
  344 +POPO.
334 345
  346 + # Data::Mapper::Data-based class
335 347 package My::Mapper::Data::User;
336 348 use parent qw(Data::Mapper::Data);
337 349
  350 + # Or, Hash-based POPO
  351 + package My::Mapper::Data::User;
  352 + sub new {
  353 + my ($class, %args) = @_;
  354 + bless \%args, $class;
  355 + }
  356 +
338 357 package My::Mapper;
339 358 use parent qw(Data::Mapper);
340 359
@@ -344,6 +363,11 @@ I<Data::Mapper::Data> object.
344 363 my $mapper = My::Mapper->new(...);
345 364 $mapper->find(user => ...) #=> Now returns data as a My::Mapper::Data::User
346 365
  366 +What data class will be used is determined by
  367 +C<Data::Mapper#data_class> method. In default, data class will be
  368 +C<Your::Mapper::Data::$table> as shown above. You can customize the
  369 +behaviour by overriding the method.
  370 +
347 371 =head1 AUTHOR
348 372
349 373 Kentaro Kuribayashi E<lt>kentarok@gmail.comE<gt>
2  t/mapper.t
@@ -146,7 +146,7 @@ subtest 'map_data' => sub {
146 146
147 147 subtest 'croaks when no-hashref object passed' => sub {
148 148 like exception { $mapper->map_data(test => (bless [], 't::Dummy')) },
149   - qr/^blessed data/;
  149 + qr/^\$data must be either/;
150 150 };
151 151
152 152 subtest 'but not croaks if the object has as_serializable() method' => sub {
1  xt/01_podspell.t
@@ -69,3 +69,4 @@ datasource
69 69 memcached
70 70 versa
71 71 GitHub
  72 +POPO

0 comments on commit b133ed9

Please sign in to comment.
Something went wrong with that request. Please try again.