Skip to content

Commit

Permalink
move all actual logic to Data::Perl::Role, split from consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Phillips committed Feb 11, 2013
1 parent c49bc2e commit 9e0e38c
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 16 deletions.
31 changes: 31 additions & 0 deletions lib/Data/Perl/Bool.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Data::Perl::Bool;

# ABSTRACT: Wrapping class for boolean values.

use strictures 1;

use Role::Tiny::With;

with 'Data::Perl::Role::Bool';

1;
__END__
==pod
=head1 SYNOPSIS
use Data::Perl qw/bool/;
my $bool = bool(0);
$bool->toggle; # 1
$bool->unset; # 0
=head1 DESCRIPTION
This class is a simple consumer of the L<Data::Perl::Role::Bool> role, which
provides all functionality. You probably want to look there instead.
=cut
29 changes: 29 additions & 0 deletions lib/Data/Perl/Code.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Data::Perl::Code;

# ABSTRACT: Wrapping class for Perl coderefs.

use strictures 1;

use Role::Tiny::With;

with 'Data::Perl::Role::Code';

1;

__END__
==pod
=head1 SYNOPSIS
use Data::Perl qw/code/;
my $code = code(sub { 'Foo'} );
$code->execute(); # returns 'Foo';
=head1 DESCRIPTION
This class is a simple consumer of the L<Data::Perl::Role::Code> role, which
provides all functionality. You probably want to look there instead.
=cut
30 changes: 30 additions & 0 deletions lib/Data/Perl/Counter.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package Data::Perl::Counter;

# ABSTRACT: Wrapping class for a simple numeric counter.

use strictures 1;

use Role::Tiny::With;

with 'Data::Perl::Role::Counter';

1;
__END__
==pod
=head1 SYNOPSIS
use Data::Perl qw/counter/;
my $c = counter(4);
$c->inc; # $c == 5
$c->reset; # $c == 0
=head1 DESCRIPTION
This class is a simple consumer of the L<Data::Perl::Role::Counter> role, which
provides all functionality. You probably want to look there instead.
=cut
30 changes: 30 additions & 0 deletions lib/Data/Perl/Number.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package Data::Perl::Number;

# ABSTRACT: Wrapping class for Perl scalar numbers.

use strictures 1;

use Role::Tiny::With;

with 'Data::Perl::Role::Number';

1;
__END__
==pod
=head1 SYNOPSIS
use Data::Perl qw/number/;
my $num = number(123);
$num->add(5); # $num == 128
$num->div(2); # $num == 64
=head1 DESCRIPTION
This class is a simple consumer of the L<Data::Perl::Role::Number> role, which
provides all functionality. You probably want to look there instead.
=cut
4 changes: 3 additions & 1 deletion lib/Data/Perl/Role/Bool.pm
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package Data::Perl::Bool;
package Data::Perl::Role::Bool;

# ABSTRACT: Wrapping class for boolean values.

use strictures 1;

use Role::Tiny;

sub new { my $bool = $_[1] ? 1 : 0; bless(\$bool, $_[0]) }

sub set { ${$_[0]} = 1 }
Expand Down
4 changes: 3 additions & 1 deletion lib/Data/Perl/Role/Code.pm
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package Data::Perl::Code;
package Data::Perl::Role::Code;

# ABSTRACT: Wrapping class for Perl coderefs.

use strictures 1;

use Role::Tiny;

sub new { my $cl = shift; bless $_[0], $cl }

sub execute { $_[0]->(@_[1..$#_]) }
Expand Down
4 changes: 2 additions & 2 deletions lib/Data/Perl/Role/Collection/Array.pm
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package Data::Perl::Collection::Array;
package Data::Perl::Role::Collection::Array;

# ABSTRACT: Wrapping class for Perl's built in array structure.

use strictures 1;


use Role::Tiny;
use List::Util;
use List::MoreUtils;
use Scalar::Util qw/blessed/;
Expand Down
17 changes: 9 additions & 8 deletions lib/Data/Perl/Role/Collection/Hash.pm
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package Data::Perl::Collection::Hash;
package Data::Perl::Role::Collection::Hash;

# ABSTRACT: Wrapping class for Perl's built in hash structure.

use strictures 1;

use Role::Tiny;
use Scalar::Util qw/blessed/;
use Module::Runtime qw/use_module/;
use Module::Runtime qw/use_package_optimistically/;

sub new { my $cl = shift; bless({ @_ }, $cl) }

Expand All @@ -17,7 +18,7 @@ sub get {
if (@_ > 1) {
my @res = @{$self}{@_};

blessed($self) ? use_module($self->_array_class)->new(@res) : @res;
blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
}
else {
$self->{$_[0]};
Expand All @@ -33,22 +34,22 @@ sub set {

my @res = @{$self}{@_[@keys_idx]};

blessed($self) ? use_module($self->_array_class)->new(@res) : @res;
blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
}

sub delete {
my $self = shift;
my @res = CORE::delete @{$self}{@_};

blessed($self) ? use_module($self->_array_class)->new(@res) : @res;
blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
}

sub keys {
my ($self) = @_;

my @res = keys %{$self};

blessed($self) ? use_module($self->_array_class)->new(@res) : @res;
blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
}

sub exists { CORE::exists $_[0]->{$_[1]} }
Expand All @@ -60,15 +61,15 @@ sub values {

my @res = CORE::values %{$_[0]};

blessed($self) ? use_module($self->_array_class)->new(@res) : @res;
blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
}

sub kv {
my ($self) = @_;

my @res = CORE::map { [ $_, $self->{$_} ] } CORE::keys %{$self};

blessed($self) ? use_module($self->_array_class)->new(@res) : @res;
blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
}


Expand Down
4 changes: 3 additions & 1 deletion lib/Data/Perl/Role/Counter.pm
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package Data::Perl::Counter;
package Data::Perl::Role::Counter;

# ABSTRACT: Wrapping class for a simple numeric counter.

use strictures 1;

use Role::Tiny;

sub new { bless \(my $n = $_[1]), $_[0] }

sub inc { ${$_[0]} += ($_[1] ? $_[1] : 1) }
Expand Down
4 changes: 3 additions & 1 deletion lib/Data/Perl/Role/Number.pm
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package Data::Perl::Number;
package Data::Perl::Role::Number;

# ABSTRACT: Wrapping class for Perl scalar numbers.

use strictures 1;

use Role::Tiny;

sub new { bless \(my $n = $_[1]), $_[0] }

sub add { ${$_[0]} = ${$_[0]} + $_[1] }
Expand Down
6 changes: 4 additions & 2 deletions lib/Data/Perl/Role/String.pm
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package Data::Perl::String;
package Data::Perl::Role::String;

# ABSTRACT: Wrapping class for Perl scalar strings.

use strictures 1;

use Role::Tiny;

sub new { bless \(my $s = $_[1]), $_[0] }

sub inc { ${$_[0]}++ }
Expand Down Expand Up @@ -59,7 +61,7 @@ __END__
=head1 DESCRIPTION
This class provides a wrapper and methods for interacting with scalar strings.
This class provides a wrapper and methods for interacting with scalar strings.
=head1 PROVIDED METHODS
Expand Down
29 changes: 29 additions & 0 deletions lib/Data/Perl/String.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Data::Perl::String;

# ABSTRACT: Wrapping class for Perl scalar strings.

use strictures 1;

use Role::Tiny::With;

with 'Data::Perl::Role::String';

1;

__END__
==pod
=head1 SYNOPSIS
use Data::Perl qw/string/;
my $string = string("foo\n");
$string->chomp; # returns 1, $string == "foo"
=head1 DESCRIPTION
This class is a simple consumer of the L<Data::Perl::Role::String> role, which
provides all functionality. You probably want to look there instead.
=cut

0 comments on commit 9e0e38c

Please sign in to comment.