Commits

Anonymous committed e54a9b1

Extracted a method and added a feature.

Comments (0)

Files changed (3)

modules/Text-Sprintf-Named/MANIFEST

 README
 t/00-load.t
 t/01-use.t
+t/02-override-param-retrieval.t
 t/boilerplate.t
 TODO
 t/pod-coverage.t

modules/Text-Sprintf-Named/lib/Text/Sprintf/Named.pm

     return $format;
 }
 
+=head2 $self->calc_param({%args})
+
+This method is used to calculate the parameter for the conversion. It
+can be over-rided by subclasses so it will behave differently. An example
+can be found in C<t/02-override-param-retrieval.t> where it is used to
+call the accessors of an object for values.
+
+%args contains:
+
+=over 4
+
+=item * named_params
+
+The named paramters.
+
+=item * name
+
+The name of the conversion.
+
+=back
+
+=cut
+sub calc_param
+{
+    my ($self, $args) = @_;
+
+    return $args->{named_params}->{$args->{name}};
+}
+
 sub _conversion
 {
     my ($self, $args) = @_;
     {
         return $self->_sprintf(
             ("%" . $args->{conv_prefix} . $args->{conv_letter}),
-            $args->{named_params}->{$args->{name}}
+            $self->calc_param($args),
         );
     }
 }

modules/Text-Sprintf-Named/t/02-override-param-retrieval.t

+use strict;
+use warnings;
+
+use Test::More tests => 1;
+
+package MySprintf;
+
+use base 'Text::Sprintf::Named';
+
+sub calc_param
+{
+    my ($self, $args) = @_;
+
+    my $method = $args->{name};
+
+    return $args->{named_params}->{obj}->$method();
+}
+
+package MyObj;
+
+sub new
+{
+    my $class = shift;
+    my $self = {};
+    bless $self, $class;
+    $self->_init(@_);
+    return $self
+}
+
+sub _init
+{
+    my $self = shift;
+
+    $self->{hello} = "Foo Goo";
+}
+
+sub hello
+{
+    return "Twalidoo";
+}
+
+package main;
+
+{
+    my $formatter = MySprintf->new({fmt => "{%(hello)s}"});
+
+    my $hello_obj = MyObj->new;
+
+    # TEST
+    is ($formatter->format({args => {obj => $hello_obj, hello => "Though I am here"}}),
+        "{Twalidoo}",
+        "Customizability of Text::Sprintf::Named",
+    );
+}