Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add somewhat-working mapper

  • Loading branch information...
commit e22389f48489bb5322a5b4c9dabe7166c69ea68e 1 parent ead538c
@jrockway authored
View
4 lib/Ai/Dependency/Image.pm
@@ -1,7 +1,7 @@
use MooseX::Declare;
# | An image
-role Ai::Dependency::Image {
+role Ai::Dependency::Image with Ai::Linkable {
has 'name' => (
is => 'ro',
isa => 'Str',
@@ -17,3 +17,5 @@ role Ai::Dependency::Image {
requires 'image_format';
requires 'image_data';
}
+
+1;
View
2  lib/Ai/Link.pm
@@ -1,6 +1,6 @@
use MooseX::Declare;
-role Ai::Link with Ai::Dependency {
+role Ai::Link {
requires 'type';
requires 'namespace';
requires 'name';
View
20 lib/Ai/Link/Page.pm
@@ -0,0 +1,20 @@
+use MooseX::Declare;
+
+class Ai::Link::Page extends Ai::Link::Resolved {
+ use Ai::Types qw(Page);
+
+ has 'content' => (
+ is => 'ro',
+ isa => 'Str',
+ default => 'html',
+ );
+
+ has '+target' => (
+ isa => Page,
+ );
+
+ method BUILD ($) {
+ confess "the target does not produce content called ". $self->content
+ unless $self->content ~~ [$self->target->list_content];
+ }
+}
View
2  lib/Ai/Link/Resolved.pm
@@ -1,6 +1,6 @@
use MooseX::Declare;
-class Ai::Link::Resolved with Ai::Link {
+class Ai::Link::Resolved with (Ai::Link, Ai::Linkable) {
use Ai::Types qw(Dependency);
has [qw/type name namespace/] => (
View
5 lib/Ai/Linkable.pm
@@ -0,0 +1,5 @@
+use MooseX::Declare;
+
+role Ai::Linkable {}
+
+1;
View
3  lib/Ai/Mapper.pm
@@ -2,6 +2,5 @@ use MooseX::Declare;
# | Map pages / dependencies to a URL
role Ai::Mapper {
- requires 'url_for';
- requires 'storage_for';
+ requires 'path_for';
}
View
41 lib/Ai/Mapper/Blog.pm
@@ -1,12 +1,43 @@
use MooseX::Declare;
-class Ai::Mapper::Blog with Ai::Role::WithSite {
+class Ai::Mapper::Blog with (Ai::Mapper, Ai::Role::WithSite) {
use MooseX::MultiMethods;
- use Ai::Types qw(Page Link);
+ use KiokuDB::Util qw(set);
+ use Ai::Types qw(Page Linkable Dependency Set);
use URI;
- method url_for(Page $page){
- return URI->new('urn:page:index.html')
- if $page->isa('Ai::Page::Index');
+ # has [qw/objects addresses/] => (
+ # is => 'ro',
+ # init_arg => undef,
+ # isa => Set,
+ # lazy => 1,
+ # default => sub { set() },
+ # );
+
+ multi method path_for (Linkable $dep) {
+ if($dep == $self->site->index_page){
+ return 'index.html';
+ }
+ else {
+ return 'static/'. $dep->name;
+ }
+ }
+
+ sub fix_name {
+ my $name = shift;
+ $name = lc $name;
+ $name =~ s/\W/-/g;
+ $name =~ s/-+/-/g;
+ $name =~ s{/}{}g;
+ return $name;
+ }
+
+ multi method path_for (Page $page, Str $part) {
+ if($part eq 'article' && $page->does('Ai::Page::Article')){
+ return fix_name($page->name). '.html';
+ }
+ else {
+ return fix_name($page->name) . ".$part";
+ }
}
}
View
3  lib/Ai/Page.pm
@@ -2,7 +2,8 @@ use MooseX::Declare;
role Ai::Page with (Ai::Dependency, Ai::Role::WithDependencies) {
requires 'name';
- requires 'content'; # hashref of filename -> data mapping
+ requires 'list_content'; # keys of content
+ requires 'content'; # hashref of name -> data mapping
}
1;
View
4 lib/Ai/Page/Article.pm
@@ -12,6 +12,10 @@ role Ai::Page::Article with Ai::Page {
return $self->article->title;
}
+ method list_content {
+ return 'article';
+ }
+
around list_dependencies {
return $self->$orig, $self->article->list_dependencies;
}
View
2  lib/Ai/Page/Blog/Article.pm
@@ -3,7 +3,7 @@ use MooseX::Declare;
class Ai::Page::Blog::Article with (Ai::Page::Blog, Ai::Page::Article) {
method content {
return {
- html => 'brought to you by "'. $self->site->title. '": '.
+ article => 'brought to you by "'. $self->site->title. '": '.
$self->article->content_as_html,
};
}
View
6 lib/Ai/Page/Blog/Index.pm
@@ -1,13 +1,9 @@
use MooseX::Declare;
class Ai::Page::Blog::Index with (Ai::Page::Blog, Ai::Page::Index) {
- method name {
- 'Index';
- }
-
method content {
return {
- html =>
+ index =>
'index for "'. $self->site->title. '": '. join '',
map { $_->name } $self->site->pages->members,
}
View
5 lib/Ai/Page/Index.pm
@@ -1,5 +1,8 @@
use MooseX::Declare;
-role Ai::Page::Index with Ai::Page {}
+role Ai::Page::Index with (Ai::Linkable, Ai::Page) {
+ method name { 'index' }
+ method list_content { 'index' }
+}
1;
View
3  lib/Ai/Site.pm
@@ -3,7 +3,8 @@ use MooseX::Declare;
role Ai::Site {
requires 'add_page';
requires 'list_pages';
- requires 'url_mapper';
+ requires 'base_uri';
+ requires 'mapper';
}
1;
View
5 lib/Ai/Site/Blog.pm
@@ -1,9 +1,10 @@
use MooseX::Declare;
-class Ai::Site::Blog with (Ai::Site::Simple, Ai::Site::WithIndex) {
+class Ai::Site::Blog with Ai::Site::Simple {
use Ai::Types qw(Article);
use Ai::Page::Blog::Article;
use Ai::Page::Blog::Index;
+ use Ai::Mapper::Blog;
has 'title' => (
is => 'ro',
@@ -17,7 +18,7 @@ class Ai::Site::Blog with (Ai::Site::Simple, Ai::Site::WithIndex) {
);
}
- method _build_url_mapper {
+ method _build_mapper {
Ai::Mapper::Blog->new(
site => $self,
);
View
24 lib/Ai/Site/Simple.pm
@@ -1,7 +1,9 @@
use MooseX::Declare;
-role Ai::Site::Simple with (Ai::Site, Ai::Role::WithDependencies::Set) {
- use Ai::Types qw(Set Page);
+role Ai::Site::Simple with (Ai::Site::WithIndex, Ai::Role::WithDependencies::Set) {
+ use Ai::Types qw(Set Page Mapper);
+ use MooseX::Types::URI qw(Uri);
+
use KiokuDB::Util qw(set);
has 'pages' => (
@@ -15,16 +17,22 @@ role Ai::Site::Simple with (Ai::Site, Ai::Role::WithDependencies::Set) {
},
);
- has 'url_mapper' => (
- reader => '_moose_roles_suck',
+ method base_uri {}
+ method mapper {}
+
+ has 'base_uri' => (
+ is => 'ro',
+ isa => Uri,
+ coerce => 1,
+ required => 1,
+ );
+
+ has 'mapper' => (
is => 'ro',
+ isa => Mapper,
lazy_build => 1,
);
- method url_mapper { $self->_moose_roles_suck }
-
- requires '_build_url_mapper';
-
method list_pages {
return $self->index_page, $self->pages->members;
}
View
8 lib/Ai/Types.pm
@@ -11,14 +11,12 @@ use MooseX::Types -declare => [qw{
Page
Dependency
Link
-
+ Linkable
Set
-
Model
-
IndexPage
-
WithDependencies
+ Mapper
}];
role_type Site, { role => 'Ai::Site' };
@@ -29,6 +27,7 @@ role_type IndexPage, { role => 'Ai::Page::Index' };
role_type Dependency, { role => 'Ai::Dependency' };
role_type Link, { role => 'Ai::Link' };
+role_type Linkable, { role => 'Ai::Linkable' };
role_type Set, { role => 'KiokuDB::Set' };
coerce Set, from ArrayRef, via {
@@ -41,5 +40,6 @@ coerce Set, from ArrayRef, via {
class_type Model, { class => 'Ai::Model' };
role_type WithDependencies, { role => 'Ai::Role::WithDependencies' };
+role_type Mapper, { role => 'Ai::Mapper' };
1;
View
4 t/small-blog.t
@@ -19,6 +19,7 @@ class Template with (Ai::Dependency) {
my $blog = Ai::Site::Blog->new(
title => 'I CAN HAZ LOLCATS',
+ base_uri => 'http://lolcat.blog/',
dependencies => [
Image->new(
name => 'cats.jpg',
@@ -58,4 +59,7 @@ ok $ap->does('Ai::Page'), 'got an article page';
is_deeply [ sort map { $_->name } $ap->list_dependencies ],
[sort qw/foo.tt nibbler.jpg cats.jpg/], 'got dependencies for article page';
+is $blog->mapper->path_for($ip), 'index.html', 'got path for index';
+is $blog->mapper->path_for($ap, 'article'), 'oh-hai-here-is-a-kitteh.html';
+
done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.