Created
January 8, 2019 23:13
-
-
Save 2shortplanks/c058a17758d5e543e8cff477e7f5b36c to your computer and use it in GitHub Desktop.
ojoJojo - monkey'd version of ojo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ojoJojo; | |
use Mojo::Base -strict; | |
use ojo; | |
use Mojo::Util qw(monkey_patch); | |
use Mojo::JSON::Pointer; | |
use Scalar::Util qw( blessed ); | |
sub import { | |
my $caller = caller; | |
eval "package $caller; use Mojolicious::Lite; 1" or die $@; | |
Mojo::Base->import(-strict, $] < 5.020 ? () : (-signatures)); | |
my $ua = $caller->app->ua; | |
$ua->server->app->hook(around_action => sub { local $_ = $_[1]; $_[0]() }); | |
$ua->max_redirects(10) unless defined $ENV{MOJO_MAX_REDIRECTS}; | |
$ua->proxy->detect unless defined $ENV{MOJO_PROXY}; | |
monkey_patch $caller, | |
a => \&a, | |
b => \&b, | |
c => \&myc, | |
d => \&d, | |
f => \&f, | |
g => \&g, | |
h => \&h, | |
ch => \&ch, | |
j => \&myj, | |
jp => \&jp, | |
n => \&n, | |
o => \&o, | |
p => \&p, | |
r => \&r, | |
t => \&t, | |
u => \&u, | |
x => \&x, | |
} | |
# json from a file | |
sub myj { | |
return j( | |
blessed $_[0] && $_[0]->isa('Mojo::File') ? $_[0]->slurp : | |
blessed $_[0] && $_[0]->isa('Mojo::Collection') ? $_->to_array : | |
$_[0] | |
); | |
} | |
sub jp { | |
my $datum = shift; | |
my $data = | |
blessed $datum && $datum->isa('Mojo::File') ? j($datum->slurp) : | |
blessed $datum && $datum->isa('Mojo::Collection') ? $_->to_array : | |
$datum; | |
my $pointer = Mojo::JSON::Pointer->new( $data ); | |
return @_ ? $pointer->get(shift) : $pointer; | |
} | |
sub myc { | |
return c(@_)->with_roles('+UtilsBy','ojoJojo::Collection::Role::MyRole'); | |
} | |
sub ch { | |
return myc(%{ shift() }); | |
} | |
package ojoJojo::Collection::Role::MyRole; | |
use Role::Tiny; | |
sub key { | |
die "Not a two element collection" unless $_[0]->size == 2; | |
return $_[0]->[0]; | |
} | |
sub value { | |
die "Not a two element collection" unless $_[0]->size == 2; | |
return $_[0]->[1]; | |
} | |
sub pairs ($c) { | |
die "uneven number of elements in collection" if $c->odd; | |
return $c->bundle_by( sub { $c->new(@_) }, 2); | |
} | |
sub even { | |
return $_[0]->size % 2 == 0 | |
} | |
sub odd { | |
return $c->size % 2 != 0 | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment