Skip to content

Commit

Permalink
move _build_chi to init
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Simões committed Feb 26, 2013
1 parent 73be34a commit 0da1058
Showing 1 changed file with 34 additions and 39 deletions.
73 changes: 34 additions & 39 deletions lib/Dancer/Session/CHI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,42 @@ use base "Dancer::Session::Abstract";

# Class methods:

my $chi;
sub init {
my ($self) = @_;
$self->SUPER::init(@_);

my $options = setting("session_CHI");
if ( ref($options) ne "HASH" ) {
raise core_session => "CHI session options not found";
}

# Don't let CHI determine the absolute path:
if ( exists $options->{root_dir} ) {
$options->{root_dir} = rel2abs($options->{root_dir});
}

my $use_plugin = delete $options->{use_plugin};
my $is_loaded = exists setting("plugins")->{"Cache::CHI"};
if ( $use_plugin && !$is_loaded ) {
raise core_session => "CHI plugin requested but not loaded";
}

$chi = $use_plugin
? do {
my $plugin = "Dancer::Plugin::Cache::CHI";
unless ( Dancer::ModuleLoader->load($plugin) ) {
raise core_session => "$plugin is needed and is not installed";
}
Dancer::Plugin::Cache::CHI::cache()
}
: CHI->new( %{$options} );
return;
}

sub create {
my ($class) = @_;
my $self = $class->new;
my $self = __PACKAGE__->new;

$self->flush;
Dancer::Logger->debug("Session (id: " . $self->id . " created.");
Expand All @@ -29,23 +62,20 @@ sub create {

sub retrieve {
my (undef, $session_id) = @_;
my $chi = _build_chi();
return $chi->get("session_$session_id")
}

# Object methods:

sub flush {
my ($self) = @_;
my $chi = _build_chi();
my $session_id = $self->id;
$chi->set( "session_$session_id" => $self );
return $self;
}

sub purge {
my ($class) = @_;
my $chi = _build_chi();
$chi->purge;
return;
}
Expand All @@ -55,45 +85,10 @@ sub reset :method { goto &purge }
sub destroy {
my ($self) = @_;
my $session_id = $self->id;
my $chi = _build_chi();
$chi->remove("session_$session_id");
cookie setting("session_name") => undef;
Dancer::Logger->debug("Session (id: $session_id) destroyed.");
return $self;
}

my $chi;
sub _build_chi {

return $chi if blessed($chi) && $chi->isa("CHI");

my $options = setting("session_CHI");
if ( ref($options) ne "HASH" ) {
raise core_session => "CHI session options not found";
}

# Don't let CHI determine the absolute path:
if ( exists $options->{root_dir} ) {
$options->{root_dir} = rel2abs($options->{root_dir});
}

my $use_plugin = delete $options->{use_plugin};
my $is_loaded = exists setting("plugins")->{"Cache::CHI"};
if ( $use_plugin && !$is_loaded ) {
raise core_session => "CHI plugin requested but not loaded";
}

$chi = $use_plugin
? do {
my $plugin = "Dancer::Plugin::Cache::CHI";
unless ( Dancer::ModuleLoader->load($plugin) ) {
raise core_session => "$plugin is needed and is not installed";
}
Dancer::Plugin::Cache::CHI::cache()
}
: CHI->new( %{$options}
);
return $chi;
}

1;

0 comments on commit 0da1058

Please sign in to comment.