Skip to content

Commit

Permalink
added some feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Craftworks committed Feb 24, 2011
1 parent 54ef2d2 commit 5daab38
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 59 deletions.
4 changes: 4 additions & 0 deletions Makefile.PL
Expand Up @@ -6,7 +6,11 @@ author 'Craftworks <craftwork at cpan org>';
license 'perl';

requires 'parent';
requires 'UNIVERSAL::require';
requires 'Class::Inspector';
requires 'Hash::Merge::Simple';
requires 'Hash::MoreUtils';
requires 'Exporter::Lite';
requires 'Log::Dispatch' => 2.26;
requires 'DBIx::Connector';
requires 'SQL::Abstract::Limit';
Expand Down
11 changes: 10 additions & 1 deletion lib/Airy.pm
Expand Up @@ -2,6 +2,7 @@ package Airy;

use strict;
use warnings;
use UNIVERSAL::require;
use Airy::Object;
use Airy::Util;
use Airy::Container;
Expand All @@ -16,8 +17,10 @@ sub import {
my $class = shift;
my $caller = caller 0;

my @warnings = split /[=,]/o, $ENV{'AIRY_WARNINGS'} || '';

strict->import;
warnings->import;
warnings->import(@warnings);

if ( 0 < @_ && ( $_[0] eq '-base' || $_[0] eq '-app' ) ) {
no strict 'refs';
Expand All @@ -44,6 +47,12 @@ sub import {
my ($self, $name) = @_;
Airy::Container->get("$caller\::API::$name");
};
*{"$caller\::handler"} = sub {
my ($self, $name) = @_;
my $web_class = "$caller\::Web::$name";
$web_class->use or die $@;
$web_class->setup($name)->handler;
};

my %config = @_;
Airy::Config->load;
Expand Down
26 changes: 26 additions & 0 deletions lib/Airy/Attribute/Container.pm
@@ -0,0 +1,26 @@
package Airy::Attribute::Container;

use strict;
use warnings;
use parent 'Class::Data::Inheritable';

__PACKAGE__->mk_classdata('attributes');
__PACKAGE__->mk_classdata('classes');
__PACKAGE__->attributes({});
__PACKAGE__->classes({});

sub set {
my ($class, $package, $code, @attrs) = @_;

push @{ $class->attributes->{ $package } }, +{
'code' => $code,
'attrs' => \@attrs,
};

for my $attr ( @attrs ) {
$attr =~ /^(\w+)/o;
$class->classes->{ $1 }++;
}
}

1;
1 change: 1 addition & 0 deletions lib/Airy/DAO/Plugin/DBI.pm
Expand Up @@ -15,6 +15,7 @@ sub import {

*{"$caller\::placeholders"} = sub {
shift if ref $_[0];
@_ = @{$_[0]} if ref $_[0] eq 'ARRAY';
croak 'not enough arguments' unless @_;
join q{, }, (('?') x @_);
};
Expand Down
7 changes: 7 additions & 0 deletions lib/Airy/Object.pm
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;
use Airy::Config;
use Airy::Log;
use Airy::Attribute::Container;

sub new {
my $class = shift;
Expand All @@ -29,4 +30,10 @@ sub log {
Airy::Log->logger;
}

sub MODIFY_CODE_ATTRIBUTES {
my ($pkg, $ref, @attrs) = @_;
Airy::Attribute::Container->set($pkg, $ref, @attrs);
return;
}

1;
4 changes: 2 additions & 2 deletions lib/Airy/Util.pm
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;
use Carp;
use File::Spec;
use UNIVERSAL::require;

my $app_class;

Expand Down Expand Up @@ -41,8 +42,7 @@ sub is_class_loaded {
sub load_class {
my $class = shift;
return $class if $loaded->{ $class }++;
eval "require $class" or confess $@;
$class->import;
$class->use or confess $@;
}
}

Expand Down
50 changes: 50 additions & 0 deletions lib/Airy/View/JSON.pm
@@ -0,0 +1,50 @@
package Airy::View::JSON;

use Airy;
use base 'Airy::View';
use JSON;

sub render {
my ($self, $vars) = @_;

my $json = encode_json($vars);

return $json;
}

sub render_web {
my ($self, $stash) = @_;
no warnings 'once';
my $c = $Airy::CONTEXT;

# TODO support callback switching
my $cb_param = ( 1 || 'allow_callback' )
? ( 'callback' ) : undef;
my $cb = $cb_param ? $c->req->param($cb_param) : undef;

my $encoding = $c->encoding->mime_name;
my $body;

# add UTF-8 BOM if the client is Safari
if ( $encoding eq 'UTF-8' ) {
my $user_agent = $c->req->user_agent || '';
if ( $user_agent =~ m/\bSafari\b/ && $user_agent !~ m/\bChrome\b/ ) {
$body = "\xEF\xBB\xBF";
}
}

$body .= "$cb(" if $cb;
$body .= $self->render($stash->{'json'});
$body .= ");" if $cb;

$c->res->body($body);

if ( ($c->req->user_agent || '') =~ /Opera/ ) {
$c->res->content_type("application/x-javascript; charset=$encoding");
}
else {
$c->res->content_type("application/json; charset=$encoding");
}
}

1;
46 changes: 42 additions & 4 deletions lib/Airy/View/Xslate.pm
Expand Up @@ -2,8 +2,9 @@ package Airy::View::Xslate;

use Airy;
use base 'Airy::View';
use Text::Xslate;
use Text::Xslate 'html_builder';
use Class::Inspector;
use HTML::FillInForm::Lite;

sub initialize {
my $self = shift;
Expand Down Expand Up @@ -40,14 +41,51 @@ sub render {
my ($self, $template, $vars) = @_;

my $suffix = $self->{'tx'}{'suffix'};
unless ( $template =~ /$suffix$/o ) {
$template .= $suffix;
}

$self->{'tx'}->render($template, $vars);
}

sub render_web {
my ($self, $stash) = @_;
no warnings 'once';
my $c = $Airy::CONTEXT;

my $template = $stash->{'template'};

my $suffix = $self->{'tx'}{'suffix'};
unless ( $template =~ /$suffix$/o ) {
$template = $template . $suffix;
$template .= $suffix;
}

$vars->{'content'} = $template;
$self->log->info(qq{Rendering template "$template"});

unless ( $stash->{'nowrapper'} ) {
$stash->{'content'} = $template;
$template = 'wrapper';
}

$stash->{'env'} = $c->req->env;
$stash->{'param'} = $c->req->parameters->as_hashref_mixed;

my $body = $self->render($template, $stash);
$body = $c->encoding->encode($body) if utf8::is_utf8($body);
$c->res->body($body);

if ( $body =~ /^\s*<(?:!DOCTYPE|html)/io ) {
$c->res->content_type('text/html');
}
}

$self->{'tx'}->render("wrapper$suffix", $vars);
sub __function_fillinform {
my @vars = @_;
my $fif = HTML::FillInForm::Lite->new;
return html_builder {
my ($html) = @_;
$fif->fill(\$html, \@vars);
};
}

1;

0 comments on commit 5daab38

Please sign in to comment.