NAME

DSL::Tiny::InstanceEval - Add DSL features to your class.

VERSION

version 0.001

SYNOPSIS

    use Test::More;
    use Test::Deep;

    # put together class with a simple dsl
    {
      package MyClassWithDSL;
      use Moo;                      # or Moose
      with qw(DSL::Tiny::Role DSL::Tiny::InstanceEval);

      sub _build_dsl_keywords { [ qw(add_values) ] };

      has values => (is => 'ro',
                     default => sub { [] },
                    );

      sub add_values {
          my $self = shift;
          push @{$self->values}, @_;
      }
    }

    # make a new instance
    my $dsl = MyClassWithDSL->new();

    my $code = <<EOC;
    add_values(qw(2 1));
    add_values(qw(3));
    EOC

    my $return_value = $dsl->instance_eval($code);
    cmp_deeply($dsl->values, bag(qw(1 2 3)), "Values were added");

    done_testing;

DESCRIPTION

This is an initial release. It's all subject to rethinking. Comments welcome.

This package provides a simple interface, "instance_eval", for evaluating snippets of a DSL (implemented with DSL::Tiny::Role) with respect to a particular instance of a class that consumes the role.

ATTRIBUTES

_anon_pkg_name

Private attribute, used to set up a package to stash private stuff.

_instance_evalator

PRIVATE

There is no 'u' in _instance_evalator. That means there should be no you in there either....

Returns a coderef that is used by the instance_eval() method.

METHODS

instance_eval

Something kind-a-similar to Ruby's instance_eval. Takes a string and evaluates it using eval(), The evaluation happens in a package that has been populated with a set of functions that map to methods in this class with the instance curried out.

See the synopsis for an example.

REQUIRES

_build_dsl_keywords

Requires _build_dsl_keywords, as a proxy for being used in a class that consumes DSL::Tiny::Role.

AUTHOR

George Hartzell <hartzell@alerce.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by George Hartzell.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.