aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/LayoutSet.pm
blob: 639af3b537ce4e3edda90a44ab39739ee61664de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package Reaction::UI::LayoutSet;

use Reaction::Class;
use File::Spec;

class LayoutSet which {

  has 'fragments' => (is => 'ro', default => sub { {} });

  has 'name' => (is => 'ro', required => 1);

  has 'source_file' => (is => 'rw', lazy_fail => 1);
  has 'file_extension'=> (isa => 'Str', is => 'rw', lazy_build => 1);

  implements _build_file_extension => as { 'html' };

  implements 'BUILD' => as {
    my ($self, $args) = @_;
    my @path = @{$args->{search_path}||[]};
    confess "No search_path provided" unless @path;
    my $found;
    my $ext = $self->file_extension;
    SEARCH: foreach my $path (@path) {
      my $cand = $path->file($self->name . ".${ext}");
      #print STDERR $cand,"\n";
      if ($cand->stat) {
        $self->_load_file($cand);
        $found = 1;
        last SEARCH;
      }
    }
    confess "Unable to load file for LayoutSet ".$self->name unless $found;
  };

  implements '_load_file' => as {
    my ($self, $file) = @_;
    my $data = $file->slurp;
    my $fragments = $self->fragments;
    # cheesy match for "=for layout fragmentname ... =something"
    # final split group also handles last in file, (?==) is lookahead
    # assertion for '=' so "=for layout fragment1 ... =for layout fragment2"
    # doesn't have the match pos go past the latter = and lose fragment2
    while ($data =~ m/=for layout (.*?)\n(.+?)(?:\n(?==)|$)/sg) {
      my ($fname, $text) = ($1, $2);
      $fragments->{$fname} = $text;
    }
    $self->source_file($file);
  };

  implements 'widget_type' => as {
    my ($self) = @_;
    my $widget = join('',   map { ucfirst($_) } split('_', $self->name));
    $widget    = join('::', map { ucfirst($_) } split('/', $widget));

    #print STDERR "--- ", $self->name, " maps to widget $widget \n";

    return $widget;
  };

};

1;