Re: Coercion problem

2010-10-06 Thread Jiří Pavlovský

 On 6.10.2010 23:03, Grzegorz Dzięgielewski wrote:

Hi everyone!

Since I'm new here i just want to say hello to everyone :-)

...and the question is:
Do you have any ideas why abspath attribute is not coercing in the code
below (via code block in Path subtype is never used)?


Hello,

Have a look at  MooseX::Types::Path::Class. I think that might already 
do what you need.




Re: Coercion problem

2010-10-06 Thread Hans Dieter Pearcey
On Wed, 06 Oct 2010 23:03:10 +0200, Grzegorz Dzięgielewski  
wrote:
> Do you have any ideas why abspath attribute is not coercing in the code
> below (via code block in Path subtype is never used)?

> subtype 'Path' => as 'Str';
> coerce 'Path' => from 'Str' => via { abs_path $_ };

Nothing will ever run this coercion, since every valid Str is already a valid
Path.  You need to narrow down the definition of Path, maybe e.g.

  subtype 'Path', as 'Str', where { ! m{/..?(/|$)} }

In other words, "a Path is a Str that does not contain /./ or /../ or end with
/. or /..".

hdp.


Coercion problem

2010-10-06 Thread Grzegorz Dzięgielewski
Hi everyone!

Since I'm new here i just want to say hello to everyone :-)

...and the question is:
Do you have any ideas why abspath attribute is not coercing in the code
below (via code block in Path subtype is never used)?

package Test;

use Cwd qw/abs_path/;
use Moose;
use Moose::Util::TypeConstraints;

subtype 'Path' => as 'Str';
coerce 'Path' => from 'Str' => via { abs_path $_ };

subtype 'Path2' => as 'ArrayRef';
coerce  'Path2' => from 'Str' => via { [ abs_path $_ ] };

has 'abspath' => (
is  => 'ro',
isa => 'Path',
coerce  => 1
);

has 'abspath2' => (
is  => 'ro',
isa => 'Path2',
coerce  => 1,
);

my $t = Test->new(
abspath => "/home/jabbas/../",
abspath2=> "/home/jabbas/../"
);

say $t->abspath;# prints /home/jabbas/../
say @{$t->abspath2};# prints /home


-- 
Greg Dzięgielewski
http://jabbas.pl


Re: Coercion problem

2009-09-04 Thread Emmanuel Quevillon
Hans Dieter Pearcey wrote:
> Excerpts from Emmanuel Quevillon's message of Wed Sep 02 10:47:06 -0400 2009:
>> Attribute (cut_types) does not pass the type constraint because:
>> Validation failed for 'ValidCutTypes' failed with value { blunt => [
>> "blunt" ] } at lib/Bio/Restriction/Analysis/FrameCutters.pm line 315
>> where as it is what I specified in my subtype definition?
> 
> 
>> subtype 'ValidCutTypes'
>>   => as Dict[
>>   cohesive => ArrayRef[Str],
>>   blunt=> ArrayRef[Str],
>>  ];
> 
> This means that both 'cohesive' and 'blunt' keys are required.  See the
> Optional[] type constraint (also from Structured) if this isn't the case for
> you.
> 
> hdp.

Hi Hans,

Optional works like a charm! :)
Thanks for your help.

Regards

Emmanuel

-- 
-
Emmanuel Quevillon
Biological Software and Databases Group
Institut Pasteur
+33 1 44 38 95 98
tuco at_ pasteur dot fr
-


Re: Coercion problem

2009-09-02 Thread Emmanuel Quevillon

Hans Dieter Pearcey wrote:

Excerpts from Emmanuel Quevillon's message of Wed Sep 02 10:47:06 -0400 2009:
  

Attribute (cut_types) does not pass the type constraint because:
Validation failed for 'ValidCutTypes' failed with value { blunt => [
"blunt" ] } at lib/Bio/Restriction/Analysis/FrameCutters.pm line 315
where as it is what I specified in my subtype definition?




  

subtype 'ValidCutTypes'
  => as Dict[
  cohesive => ArrayRef[Str],
  blunt=> ArrayRef[Str],
 ];



This means that both 'cohesive' and 'blunt' keys are required.  See the
Optional[] type constraint (also from Structured) if this isn't the case for
you.
  

Ok I'll have a look at it.
Thanks! :)


hdp.
  




Re: Coercion problem

2009-09-02 Thread Hans Dieter Pearcey
Excerpts from Emmanuel Quevillon's message of Wed Sep 02 10:47:06 -0400 2009:
> Attribute (cut_types) does not pass the type constraint because:
> Validation failed for 'ValidCutTypes' failed with value { blunt => [
> "blunt" ] } at lib/Bio/Restriction/Analysis/FrameCutters.pm line 315
> where as it is what I specified in my subtype definition?


> subtype 'ValidCutTypes'
>   => as Dict[
>   cohesive => ArrayRef[Str],
>   blunt=> ArrayRef[Str],
>  ];

This means that both 'cohesive' and 'blunt' keys are required.  See the
Optional[] type constraint (also from Structured) if this isn't the case for
you.

hdp.


Re: Coercion problem

2009-09-02 Thread Emmanuel Quevillon
Hans Dieter Pearcey wrote:
Hi Hans,

Thanks for your quick reply.
I followed your recommendations and used MX::Types::Structured and
'Dict'.
However, I get an error when I am calling my constructor ->new() :

Attribute (cut_types) does not pass the type constraint because:
Validation failed for 'ValidCutTypes' failed with value { blunt => [
"blunt" ] } at lib/Bio/Restriction/Analysis/FrameCutters.pm line 315
where as it is what I specified in my subtype definition?

and here is the code I wrote (followgin your advices):

use Moose;
use Moose::Util::TypeConstraints;
use MooseX::Types::Moose qw/Str Int ArrayRef HashRef/;
use MooseX::Types::Structured qw/Dict/;


subtype 'ValidCutTypes'
  => as Dict[
  cohesive => ArrayRef[Str],
  blunt=> ArrayRef[Str],
 ];

coerce 'ValidCutTypes'
  => from 'ArrayRef'
  => via {
  #Default value
  my $defaults = { cohesive => [qw/5' 3'/], blunt => [qw/blunt/] };
  my $hash = { };

  foreach my $type (@{$_}){
  throw Bio::Root::BadParameter(
  "Wrong type value '$type' [cohesive|blunt]!")
unless($defaults->{$type});
  $hash->{$type} = $defaults->{$type};
  }
  #[ map { $_ => $defaults->{$_} } @$_ ];
  $hash;
  }
  => from 'HashRef'
  => via { ... }

even if I use '[map {  } @$_ ]'.

The same probelm occured with passing a hash ref as :

->new(-cut_types => {cohesive => [qw/5'/]})

There's something I am not getting here.

Any clue?

Thanks

> Some notes:
> 
> * ArrayRef[Str] for the values in ValidCutTypes is just a guess based on your
>   existing defaults -- I don't know anything about your real problem domain.

What I would like is an attribute supporting as argument:

- arrayref which is transformed as a hashref of arrayref { $t =>
[qw/$r $g/] }
- hashref which is just check for keys and values and returned as a
hashref of array ref.

> * You don't need to check the keys returned from the ArrayRef coercion, 
> because
>   the Dict[] type constraint will already do that.
> * required => 1 is pretty much meaningless when you provide a default.
> 
> 
> hdp.


-- 
-
Emmanuel Quevillon
Biological Software and Databases Group
Institut Pasteur
+33 1 44 38 95 98
tuco at_ pasteur dot fr
-


Re: Coercion problem

2009-09-01 Thread Hans Dieter Pearcey
Excerpts from Emmanuel Quevillon's message of Tue Sep 01 08:29:06 -0400 2009:
> I am quite new to Moose, at least I don't use all the power of it.
> Anyway, I tried to create a subtype called 'ArrayRefOrHashRef' to be
> able to have an attribute that could accept and array ref or a hash
> ref. My idea is to :
> 
> 1) If the parameter passed to my attribute is an array ref, work
> with it and then create a hash ref at the end
> 2) It the parameter passed to my attribute is a hash ref, check some
> stuff then return it modified or not.

You don't want an attribute that accepts an arrayref or hashref.  You want an
attribute that accepts a hashref with very specific keys.

Type constraints should specify the value you want to end up with, not the
values you could start with.  (That's what coercions are for.)

> However,if I passed an array ref to 'cut_types'
> it does not get transform to a hash ref.

That's because all you said in your constraint was that it needed to be an
arrayref, so as far as Moose knows, there's no reason to run the coercion.

You probably want something like this (using MooseX::Types::Structured):

  subtype ValidCutTypes,
as Dict[
  cohesive => ArrayRef[Str],
  blunt=> ArrayRef[Str],
];

  coerce ValidCutTypes,
from ArrayRef,
via { 
  my %defaults = (cohesive => [qw/5' 3'/], blunt => ['blunt']);
  [ map { $_ => $defaults{$_} } @$_ ];
},
from HashRef,
via { ... whatever you need to do to a hashref ... };

  # later on
  
  has cut_types => (
is => 'ro',
isa => ValidCutTypes,
coerce => 1,
default => sub { ... },
  );

Some notes:

* ArrayRef[Str] for the values in ValidCutTypes is just a guess based on your
  existing defaults -- I don't know anything about your real problem domain.
* You don't need to check the keys returned from the ArrayRef coercion, because
  the Dict[] type constraint will already do that.
* required => 1 is pretty much meaningless when you provide a default.


hdp.


Coercion problem

2009-09-01 Thread Emmanuel Quevillon
Hi Moosers,

I am quite new to Moose, at least I don't use all the power of it.
Anyway, I tried to create a subtype called 'ArrayRefOrHashRef' to be
able to have an attribute that could accept and array ref or a hash
ref. My idea is to :

1) If the parameter passed to my attribute is an array ref, work
with it and then create a hash ref at the end
2) It the parameter passed to my attribute is a hash ref, check some
stuff then return it modified or not.

Here is my code :

 subtype 'ArrayRefOrHashRef'
  => as 'Ref'
  => where { ref($_) eq 'ARRAY' or ref($_) eq 'HASH' };

coerce 'ArrayRefOrHashRef'
  => from 'ArrayRef'
  => via {
  my $hash = { };

  foreach my $type (@{$_}){
  throw Bio::Root::BadParameter(
  "Wrong type value '$type' [cohesive|blunt]!")
unless($type =~ /^[cohesive|blunt]$/o);
  $hash->{$type} = $type eq 'cohesive' ? [qw/5' 3'/] :
[qw/blunt/];
  }
  return $_ = $hash;
   }
  => from 'HashRef'
  => via {
 #do some stuff with hash ref.
  };

later on:

has 'cut_types' => (
is=> 'ro',
isa   => 'ArrayRefOrHashRef',
coerce=> 1,
required  => 1,
default   => sub { {'cohesive' => [qw/5' 3'/], 'blunt' =>
[qw/blunt/]} },
);

However,if I passed an array ref to 'cut_types'
it does not get transform to a hash ref.

my $t = Object->new(-cut_types => ['foo']);
#then $t->cut_types() returns an array ref :(

So can somebody tell me what I am doing wrong?
Also, is there a way to get some value or text printed from inside
the coercion code ?

Thanks in advance

Regards

Emmanuel

-- 
-
Emmanuel Quevillon
Biological Software and Databases Group
Institut Pasteur
+33 1 44 38 95 98
tuco at_ pasteur dot fr
-