Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions lib/OpenERP/OOM/Class/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ sub _build_object_class {
return $self->object->new;
}

#-------------------------------------------------------------------------------

=head2 search

Searches OpenERP and returns a list of objects matching a given query.
Expand Down Expand Up @@ -356,14 +354,7 @@ sub _get_context
my $self = shift;
my $context = shift;

my %translation = ( lang => $self->schema->lang );
if($context)
{
# merge the context with our language for translation.
@translation{keys %$context} = values %$context;
}
$context = \%translation;
return $context;
return;
}

sub _inflate_object
Expand Down
81 changes: 31 additions & 50 deletions lib/OpenERP/OOM/Link/DBIC.pm
Original file line number Diff line number Diff line change
@@ -1,56 +1,42 @@
package OpenERP::OOM::Link::DBIC;

=head1 NAME

OpenERP::OOM::Link::DBIC
# ABSTRACT: Provides a basic link into a DBIC schema

=head1 DESCRIPTION

Class used to link OpenERP data with data in DBIC.
If you do not provide your own C<link_provider> when you create your
L<OpenERP::OOM::Schema>, this class will be used by default whenever you create
a link whose C<class> is C<DBIC>.

It provides a very simple interface into a DBIC schema by assuming every class
in your schema has a single-column primary key. This PK value is stored against
the configured column in the OpenERP object, typically C<x_dbic_link_id>.

This is where the C<key> property ends up, from
L<OpenERP::OOM::Object/has_link>.

=head1 PROPERTIES

See also L<OpenERP::OOM::Roles::DefaultLink> for inherited properties.

=head2 dbic_schema

This is the DBIC Schema object. If you need a generic DBIC schema object
this is normally the simplest way to access it.

=head1 METHODS

These methods are not normally called directly.

=head2 create

Returns the new ID of a row it creates in a table using DBIC.

my $id = $link->create({ class => 'RSName' }, $object_data);

=head2 retrieve

This is equivalent to doing a find on a ResultSet.

my $object = $link->retrieve({ class => 'RSName' }, $id);

=head2 search

This is equivalent to doing a search on a ResultSet and then returning a list
of all the id fields.

my @ids = $link->search({ class => 'RSName' }, $search, $options);
See L<OpenERP::OOM::Roles::Link> for methods.

=head1 COPYRIGHT & LICENSE

Copyright (C) 2011 OpusVL

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
All return values are DBIC row objects (or arrayrefs thereof).

=cut

use 5.010;
use Moose;
use Try::Tiny;
extends 'OpenERP::OOM::Link';
with 'OpenERP::OOM::DynamicUtils';
with 'OpenERP::OOM::Roles::DefaultLink',
'OpenERP::OOM::DynamicUtils';

has 'dbic_schema' => (
is => 'ro',
Expand All @@ -60,18 +46,15 @@ has 'dbic_schema' => (

sub _build_dbic_schema {
my $self = shift;

$self->ensure_class_loaded($self->config->{schema_class});

return $self->config->{schema_class}->connect(@{$self->config->{connect_info}});
}


#-------------------------------------------------------------------------------

sub create {
my ($self, $args, $data) = @_;

try {
my $object = $self->dbic_schema->resultset($args->{class})->create($data);
### Created linked object with ID $object->id
Expand All @@ -81,28 +64,26 @@ sub create {
};
}


#-------------------------------------------------------------------------------

sub retrieve {
my ($self, $args, $id) = @_;

if (my $object = $self->dbic_schema->resultset($args->{class})->find($id)) {
return $object;
}
return;
}

sub retrieve_list {
my ($self, $args, $ids) = @_;

#-------------------------------------------------------------------------------
# Note we do not support a compound PK. That behaviour would require a
# custom class that serialises the PK into OpenERP and deserialises it for
# search.
my $RS = $self->dbic_schema->resultset($args->{class});
my ($pk) = $rs->result_source->primary_columns;

sub search {
my ($self, $args, $search, $options) = @_;

# FIXME - Foreign primary key column is hard-coded to "id"
return map {$_->id} $self->dbic_schema->resultset($args->{class})->search($search, $options)->all;
my $rs = $RS->search({ $pk => { -in => $ids } });
return [$rs->all];
}


#-------------------------------------------------------------------------------

1;
14 changes: 10 additions & 4 deletions lib/OpenERP/OOM/Object.pm
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ are specified in OpenERP in those terms.

=head2 has_link

Used to indicate links with other systems. Typically this is to another table
in DBIC at the moment.
Used to indicate links with other systems.

The key field is in OpenERP and is used for the ids of the objects to link to.

The class is used to ask the link provider to provide a link by this name. It
is not necessarily treated as a class, but the default link provider does do that.
The C<class> is used merely to select a link by the name you have defined. When
you construct your schema, you pass it a link provider; the link provider
answers the request for a link of this class.

The C<args> is an arbitrary property, typically a hashref. This is passed to all
calls to the link object when the link provider returns it. A common use of this
is to provide the C<class> key, referencing the ResultSet to link to on the
other side of a DBIC link. The actual value here is entirely dictated by the
C<class> of link you are using, and what your link provider uses to access it.

Possible options for type are C<single> and C<multiple>.

Expand Down
26 changes: 4 additions & 22 deletions lib/OpenERP/OOM/Object/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ OpenERP::OOM::Class::Base

my $obj = $schema->class('Name')->create(\%args);

:say $obj->id;
say $obj->id;

$obj->name('New name');
$obj->update;
Expand Down Expand Up @@ -62,6 +62,9 @@ sub BUILD {
$self->meta->add_method(
$name,
sub {
# XXX !!!
# This cannot rely on 'retrieve' because links are not
# required to provide 'retrieve'
my $obj = shift;
$obj->{"_$name"} //= $obj->class->schema->link($link->{class})->retrieve($link->{args}, $obj->{$link->{key}});

Expand Down Expand Up @@ -98,9 +101,6 @@ sub BUILD {
}
}


#-------------------------------------------------------------------------------

=head1 METHODS

=head2 update
Expand Down Expand Up @@ -167,8 +167,6 @@ sub update {
return $self;
}

#-------------------------------------------------------------------------------

=head2 update_single

Updates OpenERP with a single property of an object.
Expand Down Expand Up @@ -206,8 +204,6 @@ sub update_single {
return $self;
}

#-------------------------------------------------------------------------------

=head2 refresh

Reloads an object's properties from OpenERP.
Expand All @@ -230,9 +226,6 @@ sub refresh {
return $self;
}


#-------------------------------------------------------------------------------

=head2 delete

Deletes an object from OpenERP.
Expand Down Expand Up @@ -275,8 +268,6 @@ sub copy
return $clone;
}

#-------------------------------------------------------------------------------

=head2 print

This is a debug method.
Expand All @@ -289,9 +280,6 @@ sub print {
say "Print called";
}


#-------------------------------------------------------------------------------

=head2 real_create_related

This actually does the create related via OpenERP.
Expand Down Expand Up @@ -542,9 +530,6 @@ sub search_related {
croak 'Unable to search_related'; # beat up the lame programmer who did this.
}


#-------------------------------------------------------------------------------

=head2 add_related

Adds a related or linked object to a one2many, many2many, or multiple relationship.
Expand Down Expand Up @@ -579,9 +564,6 @@ sub add_related {
}
}


#-------------------------------------------------------------------------------

=head2 set_related

Like the DBIx::Class set_related. Sets up a link to a related object.
Expand Down
49 changes: 49 additions & 0 deletions lib/OpenERP/OOM/Roles/DefaultLink.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package OpenERP::OOM::Roles::DefaultLink;

# ABSTRACT: Provides properties required by the default link provider
our $VERSION = '0';

=head1 DESCRIPTION

This role should be used by anything in the C<OpenERP::OOM::Link> namespace,
except C<OpenERP::OOM::Link::Provider>, which is a special snowflake.

This is the namespace used to discover link types for the default provider.

=head1 PROPERTIES

=head2 schema

This holds the L<OpenERP::OOM::Schema> object that the link is linking to.

This is set by the schema itself, but should probably not be relied on. The
generic link does not define a schema because there is no general path to
providing a schema to the link provider.

=head2 config

The default link provider will hold config for each link type. This will be
passed in as appropriate when the link is constructed.

Its contents are entirely down to the specific link class being constructed, and
will be ultimately sourced from user-provided configuration. An example of this
is to hold to connection info for a DBIC schema - this obviously must come from
the end user somehow.

That ends up here, and the link class should document it and make use of it.

=cut

use Moose::Role;
with 'OpenERP::OOM::Roles::Link';

has 'schema' => (
is => 'ro',
);

has 'config' => (
isa => 'HashRef',
is => 'ro',
);

1;
57 changes: 57 additions & 0 deletions lib/OpenERP/OOM/Roles/Link.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package OpenERP::OOM::Roles::Link;

# ABSTRACT: Defines required interface for link objects
our $VERSION = '0';

use Moose::Role;

=head1 DESCRIPTION

See L<OpenERP::OOM::Link::Provider> for how links work. The link provider that
your schema uses must return objects that consume this role when asked to
provide a link.

Note there is no "search". This is because we search using OpenERP's weird
nested-list syntax and we would only be able to pass this exact same data to the
user's search method to search across links. Since this is likely a lot of work
to implement, we don't require you to do so.

=head1 REQUIRED METHODS

The first parameter to every method will be C<$args>.

C<$args> is arbitrary data that is provided by L<OpenERP::OOM::Object/has_link>,
and can be used to specify information such as which type of link-side object we
are dealing with. For example, when using the C<DBIC> link type, the C<$args>
parameter will be a hashref containing C<class>, and that will contain the
resultset to use for the operation.

=head2 create

B<Arguments>: C<$args>, C<$object_data>

Use C<$object_data> to create an object. See above for C<$args>. Feel free to
die if you can't do it.

=head2 retrieve

=head2 retrieve_list

B<Arguments>: C<$args>, C<$object_data>

Use C<$object_data> to locate an object. See above for C<$args>.

In the case of C<retrieve_list>, C<$object_data> will be an arrayref, but you
can treat the items the same as the single parameter to C<retrieve>.

The method may return arbitrary data, or nothing at all. The method may die if
the item doesn't exist, if that's what you want. In the list case, return an
arrayref in all situations, even if it's empty.

The consumer will define the types that will be returned.

=cut

requires qw/create retrieve retrieve_list/;

1;
Loading