| 1 | package Gapp::Form::Context::Node; | ||
| 2 | { | ||
| 3 | $Gapp::Form::Context::Node::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use MooseX::StrictConstructor; | ||
| 8 | use MooseX::SemiAffordanceAccessor; | ||
| 9 | |||
| 10 | use MooseX::Types::Moose qw( CodeRef ); | ||
| 11 | |||
| 12 | has 'content' => ( | ||
| 13 | is => 'rw', | ||
| 14 | isa => 'Maybe[Any]', | ||
| 15 | default => undef, | ||
| 16 | ); | ||
| 17 | |||
| 18 | has 'accessor' => ( | ||
| 19 | is => 'rw', | ||
| 20 | isa => 'CodeRef|Undef', | ||
| 21 | default => undef, | ||
| 22 | ); | ||
| 23 | |||
| 24 | has 'reader_prefix' => ( | ||
| 25 | is => 'rw', | ||
| 26 | isa => 'Str', | ||
| 27 | default => '', | ||
| 28 | ); | ||
| 29 | |||
| 30 | has 'writer_prefix' => ( | ||
| 31 | is => 'rw', | ||
| 32 | isa => 'Str', | ||
| 33 | default => '', | ||
| 34 | ); | ||
| 35 | |||
| 36 | |||
| 37 | sub lookup { | ||
| 38 | my ( $self, $attr ) = @_; | ||
| 39 | $self->meta->throw( 'you did not supply an attribute to lookup' ) if ! defined $attr; | ||
| 40 | |||
| 41 | my $content = $self->content; | ||
| 42 | $content = $content->() if is_CodeRef( $content ); | ||
| 43 | |||
| 44 | if ( $self->accessor ) { | ||
| 45 | return $self->accessor->( $content, $attr ); | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | my $method = $self->reader_prefix . $attr; | ||
| 49 | return $content->$method; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | sub modify { | ||
| 54 | my ( $self, $attr, $value ) = @_; | ||
| 55 | $self->meta->throw_error( 'you did not supply an attribute to lookup' ) if ! defined $attr; | ||
| 56 | $self->meta->throw_error( 'you must supply a value' ) if @_ <= 2; | ||
| 57 | |||
| 58 | my $content = $self->content; | ||
| 59 | $content = $content->() if is_CodeRef( $content ); | ||
| 60 | |||
| 61 | if ( $self->accessor ) { | ||
| 62 | return $self->accessor->( $content, $attr, $value ); | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | my $method = $self->writer_prefix . $attr; | ||
| 66 | return $content->$method( $value ); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; | ||
| 71 | |||
| 72 | |||
| 73 | __END__ |