Skip to content

Commit

Permalink
add hash_to_row method.
Browse files Browse the repository at this point in the history
  • Loading branch information
nekokak committed Nov 2, 2010
1 parent 9ac581b commit 49b0ec3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Changes
@@ -1,5 +1,10 @@
Revision history for Perl extension DBIx::Skinny 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 0.0726
2010-11-02 2010-11-02
- ADD : disconnect method.(thanks lestrrat) - ADD : disconnect method.(thanks lestrrat)
Expand Down
29 changes: 28 additions & 1 deletion lib/DBIx/Skinny.pm
Expand Up @@ -453,11 +453,27 @@ sub find_or_new {
my ($class, $table, $args) = @_; my ($class, $table, $args) = @_;
my $row = $class->single($table, $args); my $row = $class->single($table, $args);
unless ($row) { unless ($row) {
$row = $class->data2itr($table, [$args])->first; $row = $class->hash_to_row($table, $args);
} }
return $row; 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 { sub _get_sth_iterator {
my ($class, $sql, $sth, $opt_table_info) = @_; my ($class, $sql, $sth, $opt_table_info) = @_;


Expand Down Expand Up @@ -1185,6 +1201,17 @@ get transaction scope object.
$txn->commit; $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) =item $skinny->data2itr($table_name, \@rows_data)
DBIx::Skinny::Iterator is made based on \@rows_data. DBIx::Skinny::Iterator is made based on \@rows_data.
Expand Down
28 changes: 28 additions & 0 deletions 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;

0 comments on commit 49b0ec3

Please sign in to comment.