1package Data::Remember::Util;
2{
3  $Data::Remember::Util::VERSION = '0.140490';
4}
5use strict;
6use warnings;
7
8use Carp;
9use Sub::Exporter -setup => {
10    exports => [ qw( process_que init_brain ) ],
11};
12
13# ABSTRACT: common helper utilities
14
15
16sub process_que {
17    my $que = shift;
18
19    my @ques;
20    if (ref $que eq 'ARRAY') {
21        @ques = @$que;
22    }
23
24    elsif (ref $que eq 'HASH') {
25        for my $key (sort keys %$que) {
26            push @ques, $key, $que->{$key};
27        }
28    }
29
30    else {
31        @ques = ($que);
32    }
33
34    for my $que (@ques) {
35        return undef unless defined $que;
36    }
37
38    return \@ques;
39}
40
41
42sub init_brain {
43    my $brain = shift;
44
45    $brain = 'Data::Remember::' . $brain
46        unless $brain =~ /::/;
47
48    $brain =~ /^[\w:]+$/
49        or croak qq{This does not look like a valid brain: $brain};
50
51    Class::Load::load_class($brain)
52        or carp qq{The brain $brain may not have loaded correctly: $@};
53
54    my $gray_matter = $brain->new(@_);
55
56    # Duck typing!
57    $gray_matter->can('remember')
58        or croak qq{Your brain cannot remember facts: $brain};
59    $gray_matter->can('recall')
60        or croak qq{Your brain cannot recall facts: $brain};
61    $gray_matter->can('forget')
62        or croak qq{Your brain cannot forget facts: $brain};
63
64    return $gray_matter;
65}
66
671;
68
69__END__
70
71=pod
72
73=head1 NAME
74
75Data::Remember::Util - common helper utilities
76
77=head1 VERSION
78
79version 0.140490
80
81=head1 SYNOPSIS
82
83  use Data::Remember::Util qw( process_que init_brain );
84
85  my $clean_que = process_que($handy_que);
86  my $brain = init_brain($name, @args);
87
88=head1 DESCRIPTION
89
90These are some common helper utilities used by L<Data::Remember::Class> and some of the brain implementations. Unless you are building a custom brain, you probably don't need these.
91
92=head1 SUBROUTINES
93
94=head2 process_que
95
96  my $clean_que = process_que($handy_que);
97
98The format defined in L<Data::Remember::Class/QUE> is very flexible. That section describes how all the flexible que formats are mapped into a canonical form. This is utility subroutine that does that clean-up process.
99
100This is performed automatically on behalf of each brain by L<Data::Remember::Class>, so you do not normally need this if you are just implementing the usual brain functions. However, if you have custom methods that require additional features, you may want this helper.
101
102=head2 init_brain
103
104  my $brain = init_brain($module, @args);
105
106This is a helper that checks the arguments given, loads the brain class for
107the given module name, constructs a brain, and returns it.
108
109=head1 AUTHOR
110
111Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
112
113=head1 COPYRIGHT AND LICENSE
114
115This software is copyright (c) 2014 by Qubling Software LLC.
116
117This is free software; you can redistribute it and/or modify it under
118the same terms as the Perl 5 programming language system itself.
119
120=cut
121