Commits

Shlomi Fish  committed 2fe0c0f

Finish adding the code from the homepage.

  • Participants
  • Parent commits eb6742a

Comments (0)

Files changed (4)

File HTML-Widgets-NavMenu-ToJSON/Build.PL

         'Carp' => 0,
         'HTML::Widgets::NavMenu::Object' => 0,
         'JSON' => 0,
+        'YAML::XS' => 0,
         'parent' => 0,
         'strict' => 0,
         'warnings' => 0,

File HTML-Widgets-NavMenu-ToJSON/lib/HTML/Widgets/NavMenu/ToJSON.pm

 sub _get_id_for_url
 {
     my ($self, $url) = @_;
-    return $self->_data_persistence_store->get_id_for_path($url);
+    return $self->_data_persistence_store->get_id_for_url($url);
 }
 
 =head2 $self->output_as_json()

File HTML-Widgets-NavMenu-ToJSON/lib/HTML/Widgets/NavMenu/ToJSON/Data_Persistence.pm

 use strict;
 use warnings FATAL => 'all';
 
+use parent 'HTML::Widgets::NavMenu::Object';
+
+__PACKAGE__->mk_acc_ref(
+    [
+        qw(
+            _data
+        ),
+    ]
+);
+
 =head1 NAME
 
 HTML::Widgets::NavMenu::ToJSON::Data_Persistence - Data persistence base class.
     use HTML::Widgets::NavMenu::ToJSON::Data_Persistence;
 
     my $foo = HTML::Widgets::NavMenu::ToJSON::Data_Persistence->new();
-    ...
 
 =head1 SUBROUTINES/METHODS
 
+=head2 $self->get_id_for_url($url)
+
+Returns the id (an integer) for the url fragment $url . If an ID does not
+exist, a new ID will be assigned and the old one incremented.
+
+=cut
+
+sub _get_id_persistence
+{
+    my $self = shift;
+
+    return $self->_data->{id_persistence};
+}
+
+
+sub get_id_for_url
+{
+    my ($self, $url) = @_;
+
+    my $ptr = $self->_get_id_persistence;
+
+    if (! exists($ptr->{paths_ids}->{$url}))
+    {
+        $ptr->{paths_ids}->{$url} = ($ptr->{next_id}++);
+    }
+
+    return $ptr->{paths_ids}->{$url};
+}
+
 =head1 AUTHOR
 
 Shlomi Fish, C<< <shlomif at cpan.org> >>

File HTML-Widgets-NavMenu-ToJSON/lib/HTML/Widgets/NavMenu/ToJSON/Data_Persistence/YAML.pm

 use strict;
 use warnings FATAL => 'all';
 
+use parent 'HTML::Widgets::NavMenu::ToJSON::Data_Persistence';
+
+use YAML::XS ();
+
 =head1 NAME
 
 HTML::Widgets::NavMenu::ToJSON::Data_Persistence::YAML - YAML-based persistence
 
 our $VERSION = '0.0.1';
 
+__PACKAGE__->mk_acc_ref([ qw( _filename ) ]);
+
+sub _init
+{
+    my ($self, $args) = @_;
+
+    $self->_filename($args->{filename});
+
+    return;
+}
 
 =head1 SYNOPSIS
 
-    TODO: Write something.
+See HTML::Widgets::NavMenu::ToJSON .
+
+=head1 DESCRIPTION
+
+This is a sub-class of L<HTML::Widgets::NavMenu::ToJSON::Data_Persistence>
+for providing coarse-grained persistence using a serialised YAML store as
+storage.
 
 =head1 SUBROUTINES/METHODS
 
+=head2 HTML::Widgets::NavMenu::ToJSON::Data_Persistence::YAML->new({ filename => '/path/to/filename.yml' });
+
+Initializes the persistence store with the YAML file in $args->{filename} .
+
+=cut
+
+=head2 $self->load()
+
+Loads the data from the file.
+
+=cut
+
+sub load
+{
+    my $self = shift;
+
+    my ($data) = YAML::XS::LoadFile($self->_filename());
+
+    $self->_data(
+        $data
+    );
+
+    return;
+}
+
+=head2 $self->save()
+
+Saves the data from the file.
+
+=cut
+
+sub save
+{
+    my $self = shift;
+
+    YAML::XS::DumpFile(
+        $self->_filename,
+        $self->_data
+    );
+
+    return;
+}
+
 =head1 AUTHOR
 
 Shlomi Fish, C<< <shlomif at cpan.org> >>