Skip to content

Commit

Permalink
add bag support to Pantry::Model::Pantry
Browse files Browse the repository at this point in the history
  • Loading branch information
xdg committed Sep 27, 2012
1 parent 7d8770f commit c7c82c8
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions lib/Pantry/Model/Pantry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ sub _cookbook_dir {
return $path;
}

sub _bag_dir {
my ($self) = @_;
my $path = $self->path->subdir("data_bags");
$path->mkpath;
return $path;
}

# file path where environment JSON file is located
sub _environment_path {
my ($self, $env) = @_;
Expand All @@ -77,6 +84,11 @@ sub _cookbook_path {
return $self->_cookbook_dir->subdir("${cookbook_name}");
}

sub _bag_path {
my ($self, $bag_name) = @_;
return $self->_bag_dir->file("${bag_name}.json");
}

=method all_nodes
my @nodes = $pantry->all_nodes;
Expand Down Expand Up @@ -270,6 +282,61 @@ sub cookbook {
return Pantry::Model::Cookbook->new( name => $cookbook_name, _path => $path );
}

=method all_bags
my @bags = $pantry->all_bags;
In list context, returns a list of bags. In scalar context, returns
a count of bags.
=cut

sub all_bags {
my ($self, $env) = @_;
my @bags = sort map { s/\.json$//r } map { $_->basename }
$self->_bag_dir->children;
return @bags;
}

=method C<bag>
my $node = $pantry->bag("xdg");
Returns a L<Pantry::Model::DataBag> object corresponding to the given bag.
If the bag exists in the pantry, it will be loaded from the saved bag file.
Otherwise, it will be created in memory (but will not be persisted to disk).
=cut

sub bag {
my ($self, $bag_name, $options) = @_;
$bag_name = lc $bag_name;
require Pantry::Model::DataBag;
my $path = $self->_bag_path( $bag_name );
if ( -e $path ) {
return Pantry::Model::DataBag->new_from_file( $path );
}
else {
return Pantry::Model::DataBag->new( name => $bag_name, _path => $path );
}
}

=method find_bag
my @bags = $pantry->find_bag( $leading_part );
Finds one or more bag matching a leading part. For example, given bags 'web'
and 'mysql' in a pantry, use C<<$pantry->find_bag("my")>> to get 'mysql'.
Returns a list of bag objects if any are found.
=cut

sub find_bag {
my ($self, $pattern, $options) = @_;
return map { $self->bag($_) } grep { $_ =~ /^\Q$pattern\E/ } $self->all_bags;
}

1;

=head1 SYNOPSIS
Expand Down

0 comments on commit c7c82c8

Please sign in to comment.