Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add hash_to_row method.

  • Loading branch information...
commit 49b0ec3e1bc26aede32828913066f67a46860a0e 1 parent 9ac581b
@nekokak authored
Showing with 61 additions and 1 deletion.
  1. +5 −0 Changes
  2. +28 −1 lib/DBIx/Skinny.pm
  3. +28 −0 t/228_hash_to_row.t
View
5 Changes
@@ -1,5 +1,10 @@
Revision history for Perl extension DBIx::Skinny
+0.0727
+ 2010-11-02
+ - ADD : hash_to_row class.
+ - MOD : DBIx::Skinny::attribute method rename DBIx::Skinny::_attributes. [INCOMPATIBLE CHANGE]
+
0.0726
2010-11-02
- ADD : disconnect method.(thanks lestrrat)
View
29 lib/DBIx/Skinny.pm
@@ -453,11 +453,27 @@ sub find_or_new {
my ($class, $table, $args) = @_;
my $row = $class->single($table, $args);
unless ($row) {
- $row = $class->data2itr($table, [$args])->first;
+ $row = $class->hash_to_row($table, $args);
}
return $row;
}
+sub hash_to_row {
+ my ($class, $table, $hash) = @_;
+
+ my $row_class = $class->_mk_row_class($table.$hash, $table);
+ my $row = $row_class->new(
+ {
+ sql => undef,
+ row_data => $hash,
+ skinny => $class,
+ opt_table_info => $table,
+ }
+ );
+ $row->setup;
+ $row;
+}
+
sub _get_sth_iterator {
my ($class, $sql, $sth, $opt_table_info) = @_;
@@ -1185,6 +1201,17 @@ get transaction scope object.
$txn->commit;
}
+=item $skinny->hash_to_row($table_name, $row_data_hash_ref)
+
+make DBIx::Skinny::Row's class from hash_ref.
+
+ my $row = Your::Model->hash_to_row('user',
+ {
+ id => 1,
+ name => 'lestrrat',
+ }
+ );
+
=item $skinny->data2itr($table_name, \@rows_data)
DBIx::Skinny::Iterator is made based on \@rows_data.
View
28 t/228_hash_to_row.t
@@ -0,0 +1,28 @@
+use t::Utils;
+use Mock::Basic;
+use Test::More;
+
+Mock::Basic->setup_test_db;
+
+{
+ my $hash = +{
+ id => 1,
+ name => 'perl',
+ };
+
+ my $row = Mock::Basic->hash_to_row('mock_basic', $hash);
+ isa_ok $row, 'DBIx::Skinny::Row';
+ is $row->id, 1;
+ is $row->name, 'perl';
+
+ $row->insert;
+}
+
+{
+ my $row = Mock::Basic->single('mock_basic',{id => 1});
+ isa_ok $row, 'DBIx::Skinny::Row';
+ is $row->id, 1;
+ is $row->name, 'perl';
+}
+
+done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.