File Coverage

blib/lib/OpenERP/OOM/Link/DBIC.pm
Criterion Covered Total %
statement 8 23 34.7
branch 0 2 0.0
condition n/a
subroutine 3 9 33.3
pod 3 3 100.0
total 14 37 37.8


line stmt bran cond sub pod time code
1             package OpenERP::OOM::Link::DBIC;
2              
3             =head1 NAME
4              
5             OpenERP::OOM::Link::DBIC
6              
7             =head1 DESCRIPTION
8              
9             Class used to link OpenERP data with data in DBIC.
10              
11             =head1 PROPERTIES
12              
13             =head2 dbic_schema
14              
15             This is the DBIC Schema object. If you need a generic DBIC schema object
16             this is normally the simplest way to access it.
17              
18             =head1 METHODS
19              
20             These methods are not normally called directly.
21              
22             =head2 create
23              
24             Returns the new ID of a row it creates in a table using DBIC.
25              
26             my $id = $link->create({ class => 'RSName' }, $object_data);
27              
28             =head2 retrieve
29              
30             This is equivalent to doing a find on a ResultSet.
31              
32             my $object = $link->retrieve({ class => 'RSName' }, $id);
33              
34             =head2 search
35              
36             This is equivalent to doing a search on a ResultSet and then returning a list
37             of all the id fields.
38              
39             my @ids = $link->search({ class => 'RSName' }, $search, $options);
40              
41             =head1 COPYRIGHT & LICENSE
42              
43             Copyright (C) 2011 OpusVL
44              
45             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
46              
47             =cut
48              
49 2     2   3326 use 5.010;
  2         6  
50 2     2   12 use Moose;
  2         3  
  2         12  
51 2     2   11524 use Try::Tiny;
  2         4  
  2         749  
52             extends 'OpenERP::OOM::Link';
53             with 'OpenERP::OOM::DynamicUtils';
54              
55             has 'dbic_schema' => (
56             is => 'ro',
57             lazy => 1,
58             builder => '_build_dbic_schema',
59             );
60              
61             sub _build_dbic_schema {
62 0     0     my $self = shift;
63            
64 0           $self->ensure_class_loaded($self->config->{schema_class});
65            
66 0           return $self->config->{schema_class}->connect(@{$self->config->{connect_info}});
  0            
67             }
68              
69              
70             #-------------------------------------------------------------------------------
71              
72             sub create {
73 0     0 1   my ($self, $args, $data) = @_;
74            
75             try {
76 0     0     my $object = $self->dbic_schema->resultset($args->{class})->create($data);
77             ### Created linked object with ID $object->id
78 0           return $object->id;
79             } catch {
80 0     0     die "Could not create linked object: $_";
81 0           };
82             }
83              
84              
85             #-------------------------------------------------------------------------------
86              
87             sub retrieve {
88 0     0 1   my ($self, $args, $id) = @_;
89            
90 0 0         if (my $object = $self->dbic_schema->resultset($args->{class})->find($id)) {
91 0           return $object;
92             }
93             }
94              
95              
96             #-------------------------------------------------------------------------------
97              
98             sub search {
99 0     0 1   my ($self, $args, $search, $options) = @_;
100            
101             # FIXME - Foreign primary key column is hard-coded to "id"
102 0           return map {$_->id} $self->dbic_schema->resultset($args->{class})->search($search, $options)->all;
  0            
103             }
104              
105              
106             #-------------------------------------------------------------------------------
107              
108             1;