File Coverage

blib/lib/Reply/Plugin/ORM.pm
Criterion Covered Total %
statement 24 68 35.2
branch 0 24 0.0
condition 0 2 0.0
subroutine 8 16 50.0
pod 1 2 50.0
total 33 112 29.4


line stmt bran cond sub pod time code
1             package Reply::Plugin::ORM;
2 1     1   768 use 5.008005;
  1         3  
  1         42  
3 1     1   5 use strict;
  1         2  
  1         115  
4 1     1   10 use warnings;
  1         10  
  1         38  
5 1     1   1024 use parent qw/ Reply::Plugin /;
  1         368  
  1         5  
6              
7 1     1   27164 use Module::Load;
  1         1315  
  1         7  
8 1     1   1482 use Path::Tiny;
  1         25390  
  1         258  
9              
10             our $VERSION = "0.01";
11             my $ORM;
12              
13             sub new {
14 0     0 0   my ($class, %opts) = @_;
15              
16 0           my $db_name = $ENV{PERL_REPLY_PLUGIN_ORM};
17 0 0         return $class->SUPER::new(%opts) unless defined $db_name;
18            
19 0 0         my $config_path = delete $opts{config}
20             or Carp::croak "[Error] Please set config file's path at .replyrc";
21 0           my $config = $class->_config($db_name, $config_path);
22 0           $class->_config_validate($config);
23              
24 0           my $orm_module = "Reply::Plugin::ORM::$config->{orm}";
25 0           eval "require $orm_module";
26 0 0         Carp::croak "[Error] Module '$orm_module' not found." if $@;
27              
28 0           load $orm_module;
29 0           $ORM = $orm_module->new($db_name => $config, %opts);
30 0           my @methods = (@{$ORM->{methods}}, qw/ Show_dbname Show_methods /);
  0            
31              
32 1     1   9 no strict 'refs';
  1         3  
  1         136  
33 0           for my $method (@{$ORM->{methods}}) {
  0            
34 0     0     *{"main::$method"} = sub { _command(lc $method, @_ ) };
  0            
  0            
35             }
36 0     0     *main::Show_dbname = sub { return $db_name };
  0            
37 0     0     *main::Show_methods = sub { return @methods };
  0            
38 1     1   5 use strict 'refs';
  1         2  
  1         1214  
39              
40 0           printf "Connect database : %s (using %s)\n", $db_name, $config->{orm};
41              
42 0           return $class->SUPER::new(%opts, methods => \@methods);
43             }
44              
45             sub tab_handler {
46 0     0 1   my $self = shift;
47 0           my ($line) = @_;
48              
49 0 0         return if length $line <= 0;
50 0 0         return if $line =~ /^#/; # command
51 0 0         return if $line =~ /->\s*$/; # method call
52 0 0         return if $line =~ /[\$\@\%\&\*]\s*$/;
53              
54 0           return sort grep {
55 0           index ($_, $line) == 0
56 0           } @{$self->{methods}};
57             }
58              
59             sub _config {
60 0     0     my ($class, $db_name, $config_path) = @_;
61              
62 0           my $config_fullpath = path($config_path);
63 0 0         Carp::croak "[Error] Config file not found: $config_fullpath" unless -f $config_fullpath;
64 0 0         my $config = do $config_fullpath
65             or Carp::croak "[Error] Failed to load config file: $config_path";
66              
67 0 0         Carp::croak "[Error] Setting of '$db_name' not found at config file" unless $config->{$db_name};
68 0           return $config->{$db_name}
69             }
70              
71             sub _config_validate {
72 0     0     my ($class, $config) = @_;
73 0 0         Carp::croak "[Error] Please set 'orm' at config file." unless $config->{orm};
74 0 0         Carp::croak "[Error] Please set 'connect_info' at config file." unless $config->{connect_info};
75             }
76              
77             sub _command {
78 0   0 0     my $command = shift || '';
79 0           return $ORM->{orm}->$command(@_);
80             }
81              
82             1;
83             __END__
84              
85             =encoding utf-8
86              
87             =head1 NAME
88              
89             Reply::Plugin::ORM - Reply + O/R Mapper
90              
91             =head1 SYNOPSIS
92              
93             ; .replyrc
94             ...
95             [ORM]
96             config = ~/.reply-plugin-orm
97             otogiri_plugins = DeleteCascade ; You can use O/R Mapper plugin (in this case, 'Otogiri::Plugin::DeleteCascade').
98             teng_plugins = Count,SearchJoined ; You can use multiple plugins, like this.
99              
100             ; .reply-plugin-orm
101             +{
102             sandbox => {
103             orm => 'Otogiri', # or 'Teng'
104             connect_info => ["dbi:SQLite:dbname=...", '', '', { ... }],
105             }
106             }
107            
108             $ PERL_REPLY_PLUGIN_ORM=sandbox reply
109              
110             =head1 DESCRIPTION
111              
112             Reply::Plugin::ORM is Reply's plugin for operation of database using O/R Mapper.
113             In this version, we have support for Otogiri and Teng.
114              
115             =head1 METHODS
116              
117             Using this module, you can use O/R Mapper's method at Reply shell.
118             If you set loading of O/R Mapper's plugin in config file, you can use method that provided by plugin on shell.
119              
120             In order to prevent the redefined of function, these method's initials are upper case.
121             You can call Teng's C<single> method, like this:
122            
123             1> Single 'table_name';
124              
125             In addition, this module provides two additional methods.
126              
127             =over 4
128              
129             =item * C<Show_methods>
130              
131             This method outputs a list of methods provided by this module.
132              
133             =item * C<Show_dbname>
134              
135             This method outputs the name of database which you are connecting.
136              
137             =back
138              
139             =head1 LICENSE
140              
141             Copyright (C) papix.
142              
143             This library is free software; you can redistribute it and/or modify
144             it under the same terms as Perl itself.
145              
146             =head1 AUTHOR
147              
148             papix E<lt>mail@papix.netE<gt>
149              
150             =head1 SEE ALSO
151              
152             L<Reply>
153              
154             L<Otogiri>
155              
156             L<Teng>
157              
158             =cut
159