David Golden

NAME

Pantry::Role::Serializable - A role to save/load data to/from JSON files

VERSION

version 0.012

SYNOPSIS

  with 'Pantry::Role::Serializable' => {
    freezer => '_freeze',
    thawer => '_thaw',
  };

DESCRIPTION

This parameterizable Moose role provides methods for saving/loading Pantry objects as JSON.

METHODS

new_from_file

  my $obj = $class->new_from_file( $path );

Constructs a new object from JSON data found in the given file.

save_as

  $obj->save_as( $path );

Stores object data as JSON in a file at the given path location. Attributes with leading underscores are considered "private" and are omitted.

USAGE

Customizing the serialization behavior can be done with the optional freezer and thawer role parameters. If either freezer or thawer is omitted, data will not be modified during saving/loading (respectively), except that private attributes are always omitted when saving.

freezer

This role parameter takes the name of a method to use for modifying the object data during freezing. It takes a hashref of data representing the object's attributes (excluding any private attributes) and must return a hashref of data which will be serialized as JSON.

thawer

This role parameter takes the name of a method to use for modifying the object data during thawing. It takes a hashref of data deserialized from JSON and must return a hashref of data suitable for passing to the object constructor.

SEE ALSO

  • MooseX::Storage -- this didn't quite meet my needs as it

    mandates a __CLASS__ key which causes problems when Chef consumes saved data

AUTHOR

David Golden <dagolden@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2011 by David Golden.

This is free software, licensed under:

  The Apache License, Version 2.0, January 2004