| 1 | use v5.10; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package Meerkat; | ||
| 6 | # ABSTRACT: Manage MongoDB documents as Moose objects | ||
| 7 | |||
| 8 | our $VERSION = '0.016'; | ||
| 9 | |||
| 10 | # Dependencies | ||
| 11 | use Moose 2; | ||
| 12 | use MooseX::AttributeShortcuts; | ||
| 13 | |||
| 14 | use Meerkat::Collection; | ||
| 15 | use Module::Runtime qw/require_module compose_module_name/; | ||
| 16 | use MongoDB 2; | ||
| 17 | use Try::Tiny; | ||
| 18 | use Type::Params qw/compile/; | ||
| 19 | use Types::Standard qw/:types/; | ||
| 20 | |||
| 21 | use namespace::autoclean; | ||
| 22 | |||
| 23 | with 'MooseX::Role::Logger', 'MooseX::Role::MongoDB' => { -version => 0.010 }; | ||
| 24 | |||
| 25 | #pod =attr model_namespace (required) | ||
| 26 | #pod | ||
| 27 | #pod A perl module namespace that will be prepended to class names requested | ||
| 28 | #pod via the L</collection> method. If C<model_namespace> is "My::Model", then | ||
| 29 | #pod C<< $meerkat->collection("Baz") >> will load and associate the | ||
| 30 | #pod C<My::Model::Baz> class in the returned collection object. | ||
| 31 | #pod | ||
| 32 | #pod =cut | ||
| 33 | |||
| 34 | has model_namespace => ( | ||
| 35 | is => 'ro', | ||
| 36 | isa => 'Str', | ||
| 37 | required => 1, | ||
| 38 | ); | ||
| 39 | |||
| 40 | #pod =attr database_name (required) | ||
| 41 | #pod | ||
| 42 | #pod A MongoDB database name used to store all collections generated via the Meerkat | ||
| 43 | #pod object and its collection factories. Unless a C<db_name> is provided in the | ||
| 44 | #pod C<client_options> attribute, this database will be the default for | ||
| 45 | #pod authentication. | ||
| 46 | #pod | ||
| 47 | #pod =cut | ||
| 48 | |||
| 49 | has database_name => ( | ||
| 50 | is => 'ro', | ||
| 51 | isa => 'Str', | ||
| 52 | required => 1, | ||
| 53 | ); | ||
| 54 | |||
| 55 | #pod =attr client_options | ||
| 56 | #pod | ||
| 57 | #pod A hash reference of L<MongoDB::MongoClient> options that will be passed to its | ||
| 58 | #pod C<connect> method. | ||
| 59 | #pod | ||
| 60 | #pod Note: The C<dt_type> will be forced to C<undef> so that the MongoDB client will | ||
| 61 | #pod provide time values as epoch seconds. See the L<Meerkat::Cookbook> for more on | ||
| 62 | #pod dealing with dates and times. | ||
| 63 | #pod | ||
| 64 | #pod =cut | ||
| 65 | |||
| 66 | has client_options => ( | ||
| 67 | is => 'ro', | ||
| 68 | isa => 'HashRef', # hashlike? | ||
| 69 | default => sub { {} }, | ||
| 70 | ); | ||
| 71 | |||
| 72 | #pod =attr collection_namespace | ||
| 73 | #pod | ||
| 74 | #pod A perl module namespace that will be be used to search for custom collection | ||
| 75 | #pod classes. The C<collection_namespace> will be prepended to class names | ||
| 76 | #pod requested via the L</collection> method. If C<collection_namespace> is | ||
| 77 | #pod "My::Collection", then C<< $meerkat->collection("Baz") >> will load and use | ||
| 78 | #pod C<My::Collection::Baz> for constructing a collection object. If | ||
| 79 | #pod C<collection_namespace> is not provided or if no class is found under the | ||
| 80 | #pod namespace (or if it fails to load), then collection objects will be constructed | ||
| 81 | #pod using L<Meerkat::Collection>. | ||
| 82 | #pod | ||
| 83 | #pod =cut | ||
| 84 | |||
| 85 | has collection_namespace => ( | ||
| 86 | is => 'ro', | ||
| 87 | isa => 'Str', | ||
| 88 | ); | ||
| 89 | |||
| 90 | sub BUILD { | ||
| 91 | my ($self) = @_; | ||
| 92 | |||
| 93 | # We force MongoDB to convert its internal datetimes to epoch values so we | ||
| 94 | # can proxy them with Meerkat::DateTime objects; storage of DateTime or | ||
| 95 | # DateTime::Tiny are automatically converted regardless of this setting. | ||
| 96 | $self->client_options->{dt_type} = undef; | ||
| 97 | } | ||
| 98 | |||
| 99 | # configure MooseX::Role::MongodB | ||
| 100 | sub _build__mongo_client_options { $_[0]->client_options } | ||
| 101 | sub _build__mongo_default_database { $_[0]->database_name } | ||
| 102 | |||
| 103 | #--------------------------------------------------------------------------# | ||
| 104 | # Methods | ||
| 105 | #--------------------------------------------------------------------------# | ||
| 106 | |||
| 107 | #pod =method new | ||
| 108 | #pod | ||
| 109 | #pod my $meerkat = Meerkat->new( | ||
| 110 | #pod model_namespace => "My::Model", | ||
| 111 | #pod database_name => "test", | ||
| 112 | #pod client_options => { | ||
| 113 | #pod host => "mongodb://example.net:27017", | ||
| 114 | #pod username => "willywonka", | ||
| 115 | #pod password => "ilovechocolate", | ||
| 116 | #pod }, | ||
| 117 | #pod ); | ||
| 118 | #pod | ||
| 119 | #pod Generates and returns a new Meerkat object. The C<model_namespace> and | ||
| 120 | #pod C<database_name> attributes are required. | ||
| 121 | #pod | ||
| 122 | #pod =method collection | ||
| 123 | #pod | ||
| 124 | #pod my $person = $meerkat->collection("Person"); # My::Model::Person | ||
| 125 | #pod | ||
| 126 | #pod Returns a L<Meerkat::Collection> factory object or possibly a subclass if a | ||
| 127 | #pod C<collection_namespace> attribute has been provided. A single parameter is | ||
| 128 | #pod required and is used as the suffix of a class name provided to the | ||
| 129 | #pod Meerkat::Collection C<class> attribute. | ||
| 130 | #pod | ||
| 131 | #pod =cut | ||
| 132 | |||
| 133 | sub collection { | ||
| 134 | state $check = compile( Object, Str ); | ||
| 135 | my ( $self, $suffix ) = $check->(@_); | ||
| 136 | my $model = compose_module_name( $self->model_namespace, $suffix ); | ||
| 137 | my $class; | ||
| 138 | if ( my $prefix = $self->collection_namespace ) { | ||
| 139 | $class = compose_module_name( $prefix, $suffix ); | ||
| 140 | try { require_module($class) } catch { $class = "Meerkat::Collection" }; | ||
| 141 | } | ||
| 142 | else { | ||
| 143 | $class = "Meerkat::Collection"; | ||
| 144 | } | ||
| 145 | return $class->new( class => $model, meerkat => $self ); | ||
| 146 | } | ||
| 147 | |||
| 148 | #pod =method mongo_collection | ||
| 149 | #pod | ||
| 150 | #pod my $coll = $meerkat->mongo_collection("My_Model_Person"); | ||
| 151 | #pod | ||
| 152 | #pod Returns a raw L<MongoDB::Collection> object from the associated database. | ||
| 153 | #pod This is used internally by L<Meerkat::Collection> and is not intended for | ||
| 154 | #pod general use. | ||
| 155 | #pod | ||
| 156 | #pod =cut | ||
| 157 | |||
| 158 | # alias _mongo_collection provided by MooseX::Role::MongoDB | ||
| 159 | *mongo_collection = *_mongo_collection; | ||
| 160 | |||
| 161 | __PACKAGE__->meta->make_immutable; | ||
| 162 | |||
| 163 | 1; | ||
| 164 | |||
| 165 | |||
| 166 | # vim: ts=4 sts=4 sw=4 et: | ||
| 167 | |||
| 168 | __END__ |