NAME
Data::Focus::Lens::Dynamic - a lens that dynamically creates an appropriate lens for the target
SYNOPSIS
package Blessed::Data;
sub new {
my ($class) = @_;
return bless {
secret_data => "hoge",
accessible_by_lens => {
a => "a for Blessed::Data"
},
}, $class;
}
sub Lens {
my ($self, $param) = @_;
require Data::Focus::Lens::HashArray::Index;
return (
Data::Focus::Lens::HashArray::Index->new(index => "accessible_by_lens", allow_blessed => 1)
. Data::Focus::Lens::HashArray::Index->new(index => $param)
);
}
package main;
use Data::Focus qw(focus);
use Data::Focus::Lens::Dynamic;
my $plain_data = { a => "a for plain_data" };
my $blessed_data = Blessed::Data->new;
my $lens = Data::Focus::Lens::Dynamic->new("a");
focus($plain_data)->get($lens); ## => "a for plain_data";
focus($blessed_data)->get($lens); ## => "a for Blessed::Data";
$plain_data->{a} = $blessed_data;
focus($plain_data)->get($lens, $lens); ## => "a for Blessed::Data";
DESCRIPTION
This is an implementation of Data::Focus::Lens, which dynamically creates an appropriate lens for the given target. The actual focusing job is delegated to the dynamically created lens.
CLASS METHOD
$lens = Data::Focus::Lens::Dynamic->new($param)
The constructor.
The $lens
keeps the given $param
. The $param
is then passed to the dynamically created lens.
DELEGATION RULES
Here's how Data::Focus::Lens::Dynamic creates the lens object appropriate for the $target
.
If the
$target
is a blessed object and hasLens()
method, it calls theLens()
method to obtain the appropriate lens.$appropriate_lens = $target->Lens($param)
If
Lens()
method doesn't return a Data::Focus::Lens object, it throws an exception.If the
$target
is a non-blessed hash-ref or array-ref orundef
, it creates a Data::Focus::Lens::HashArray::Index.$appropriate_lens = Data::Focus::Lens::HashArray::Index->new(index => $param)
Otherwise, it does nothing. It creates no focal points.
OBJECT METHODS
apply_lens
See Data::Focus::Lens.
AUTHOR
Toshio Ito <debug.ito at gmail.com>