| 1 | package andel; | ||
| 2 | use Mojo::Base -strict; | ||
| 3 | |||
| 4 | my $mandel; | ||
| 5 | |||
| 6 | sub c { | ||
| 7 | my $name = shift or die "Usage: c('collection_name')"; | ||
| 8 | my $collection = $mandel->collection($name); | ||
| 9 | |||
| 10 | if (@_ and ref $_[0] eq 'HASH') { | ||
| 11 | return $collection->create($_[0])->save; | ||
| 12 | } | ||
| 13 | |||
| 14 | return $collection; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub import { | ||
| 18 | my $class = shift; | ||
| 19 | my $caller = caller; | ||
| 20 | |||
| 21 | strict->import; | ||
| 22 | warnings->import; | ||
| 23 | |||
| 24 | $class->_setup(@_) if @_; | ||
| 25 | |||
| 26 | no strict 'refs'; | ||
| 27 | *{"$caller\::c"} = \&c; | ||
| 28 | *{"$caller\::j"} = \&Mojo::JSON::j; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _setup { | ||
| 32 | my ($class, $model, @connect) = @_; | ||
| 33 | eval "use $model; 1" or die "use $model: $@"; | ||
| 34 | $connect[0] = "mongodb://localhost/$connect[0]" if @connect and $connect[0] !~ /^mongodb:/; | ||
| 35 | $mandel = $model->connect(@connect); | ||
| 36 | } | ||
| 37 | |||
| 38 | 1; | ||
| 39 | |||
| 40 | =encoding utf8 | ||
| 41 | |||
| 42 | =head1 NAME | ||
| 43 | |||
| 44 | andel - Oneliner Mandel magic | ||
| 45 | |||
| 46 | =head1 SYNOPSIS | ||
| 47 | |||
| 48 | perl -Mandel=MyModel -E'say c("users", j shift)->in_storage' '{"name":"joe"}' | ||
| 49 | perl -Mandel=MyModel -E'say c("users", { name => "joe" })->in_storage' | ||
| 50 | perl -Mandel=MyModel,db_name -E'say c("users")->count' | ||
| 51 | perl -Mandel=MyModel,mongodb://hostname/db_name -E'say c("users")->count' | ||
| 52 | |||
| 53 | =head1 EXPORTED FUNCTIONS | ||
| 54 | |||
| 55 | =head2 c | ||
| 56 | |||
| 57 | $collection = c("collection_name"); | ||
| 58 | $obj = c("collection_name", \%create_args); | ||
| 59 | |||
| 60 | Alias for | ||
| 61 | |||
| 62 | $mandel->collection("collection_name"); | ||
| 63 | |||
| 64 | Or | ||
| 65 | |||
| 66 | $mandel->collection("collection_name")->create(\%create_args)->save; | ||
| 67 | |||
| 68 | =head2 j | ||
| 69 | |||
| 70 | See L<Mojo::JSON/j>. | ||
| 71 | |||
| 72 | =head1 METHODS | ||
| 73 | |||
| 74 | =head2 import | ||
| 75 | |||
| 76 | See L</SYNOPSIS>. | ||
| 77 | |||
| 78 | =head1 AUTHOR | ||
| 79 | |||
| 80 | Jan Henning Thorsen - C<jhthorsen@cpan.org> | ||
| 81 | |||
| 82 | =cut |