Bot-ChatBots

 view release on metacpan or  search on metacpan

lib/Bot/ChatBots/Role/Source.pm  view on Meta::CPAN

package Bot::ChatBots::Role::Source;
use strict;
use warnings;
{ our $VERSION = '0.014'; }

use Ouch;
use Log::Any qw< $log >;
use Bot::ChatBots::Weak;
use Try::Tiny;
use 5.010;

use Moo::Role;

requires 'normalize_record';

has custom_pairs => (
   is      => 'rw',
   default => sub { return {} },
);

has processor => (
   is      => 'ro',
   lazy    => 1,
   builder => 'BUILD_processor',
);

has typename => (
   is      => 'ro',
   lazy    => 1,
   builder => 'BUILD_typename',
);

sub BUILD_processor {
   ouch 500, 'no processor defined!';
}

sub BUILD_typename {
   my $self = shift;
   my @chunks = split /::/, lc ref $self;
   return (
      ((@chunks == 1) || ($chunks[-1] ne 'webhook'))
      ? $chunks[-1]
      : $chunks[-2]
   );
} ## end sub BUILD_typename

sub pack_source {
   my $self = shift;
   my $args = (@_ && ref($_[0])) ? $_[0] : {@_};

   my %class_custom_pairs =
       $self->can('class_custom_pairs')
     ? $self->class_custom_pairs
     : ();

   my $refs = Bot::ChatBots::Weak->new;
   $refs->set(self => $self);
   while (my ($k, $v) = each %{$args->{refs} // {}}) {
      $refs->set($k, $v);
   }

   my $source = {
      class => ref($self),
      refs  => $refs,
      type  => $self->typename,
      %class_custom_pairs,
      %{$self->custom_pairs},
      %{$args->{source_pairs} // {}},
   };



( run in 0.552 second using v1.01-cache-2.11-cpan-39bf76dae61 )