From 8abee86bd5ac48c9885813b3d1a6a56d42062afb Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 3 Oct 2011 03:24:34 -0500 Subject: initial implementation --- dist.ini | 5 ++++ lib/OXx/Encoding.pm | 40 ++++++++++++++++++++++++++++ lib/OXx/Encoding/Role/Request.pm | 55 +++++++++++++++++++++++++++++++++++++++ lib/OXx/Encoding/Role/Response.pm | 24 +++++++++++++++++ lib/OXx/Encoding/Types.pm | 10 +++++++ 5 files changed, 134 insertions(+) create mode 100644 lib/OXx/Encoding/Role/Request.pm create mode 100644 lib/OXx/Encoding/Role/Response.pm create mode 100644 lib/OXx/Encoding/Types.pm diff --git a/dist.ini b/dist.ini index bf2bee5..2e2a1e5 100644 --- a/dist.ini +++ b/dist.ini @@ -7,3 +7,8 @@ copyright_holder = Jesse Luehrs dist = OXx-Encoding [Prereqs] +Encode = 0 +Moose = 2.0200 +MooseX::Role::Parameterized = 0 +namespace::autoclean = 0 +OX = 0 diff --git a/lib/OXx/Encoding.pm b/lib/OXx/Encoding.pm index e69de29..729c5a3 100644 --- a/lib/OXx/Encoding.pm +++ b/lib/OXx/Encoding.pm @@ -0,0 +1,40 @@ +package OXx::Encoding; +use MooseX::Role::Parameterized; +use namespace::autoclean; + +use OXx::Encoding::Types; + +parameter encoding => ( + isa => 'Encoding', + required => 1, +); + +parameter html_encoding => ( + isa => 'Str', + lazy => 1, + default => sub { shift->encoding }, +); + +role { + my $p = shift; + + around request_class => sub { + my $orig = shift; + my $self = shift; + + my $super = $self->$orig(@_); + + return Moose::Meta::Class->create_anon_class( + superclasses => [$super], + roles => [ + 'OXx::Encoding::Role::Request' => { + encoding => $p->encoding, + html_encoding => $p->html_encoding, + }, + ], + cache => 1, + )->name; + }; +} + +1; diff --git a/lib/OXx/Encoding/Role/Request.pm b/lib/OXx/Encoding/Role/Request.pm new file mode 100644 index 0000000..a41c74b --- /dev/null +++ b/lib/OXx/Encoding/Role/Request.pm @@ -0,0 +1,55 @@ +package OXx::Encoding::Role::Request; +use MooseX::Role::Parameterized; +use namespace::autoclean; + +use Encode (); +use OXx::Encoding::Types; + +parameter encoding => ( + isa => 'Encoding', + required => 1, +); + +parameter html_encoding => ( + isa => 'Str', + lazy => 1, + default => sub { shift->encoding }, +); + +role { + my $p = shift; + + my $encoding = Encode::find_encoding($p->encoding); + + sub decode { + my $self = shift; + my ($data) = @_; + return $encoding->decode($data); + } + + sub encode { + my $self = shift; + my ($data) = @_; + return $encoding->encode($data); + } + + around response_class => sub { + my $orig = shift; + my $self = shift; + + my $super = $self->$orig(@_); + + return Moose::Meta::Class->create_anon_class( + superclasses => [$super], + roles => [ + 'OXx::Encoding::Role::Response' => { + encoding => $p->encoding, + html_encoding => $p->html_encoding, + }, + ], + cache => 1, + )->name; + }; +} + +1; diff --git a/lib/OXx/Encoding/Role/Response.pm b/lib/OXx/Encoding/Role/Response.pm new file mode 100644 index 0000000..71d186b --- /dev/null +++ b/lib/OXx/Encoding/Role/Response.pm @@ -0,0 +1,24 @@ +package OXx::Encoding::Role::Response; +use MooseX::Role::Parameterized; +use namespace::autoclean; + +use OXx::Encoding::Types; + +parameter encoding => ( + isa => 'Encoding', + required => 1, +); + +parameter html_encoding => ( + isa => 'Str', + lazy => 1, + default => sub { shift->encoding }, +); + +role { + my $p = shift; + + # XXX: set the html encoding here... (need to support this in ox first) +} + +1; diff --git a/lib/OXx/Encoding/Types.pm b/lib/OXx/Encoding/Types.pm new file mode 100644 index 0000000..691f46e --- /dev/null +++ b/lib/OXx/Encoding/Types.pm @@ -0,0 +1,10 @@ +package OXx::Encoding::Types; +use strict; +use warnings; +use Moose::Util::TypeConstraints; + +use Encode (); + +subtype 'Encoding', as 'Str', where { Encode::find_encoding($_) }; + +1; -- cgit v1.2.3-54-g00ecf