File Coverage

blib/lib/Jaipo/Config.pm
Criterion Covered Total %
statement 42 110 38.1
branch 1 14 7.1
condition 2 12 16.6
subroutine 12 20 60.0
pod 4 12 33.3
total 61 168 36.3


line stmt bran cond sub pod time code
1             package Jaipo::Config;
2 3     3   9 use warnings;
  3         3  
  3         73  
3 3     3   9 use strict;
  3         4  
  3         51  
4 3     3   1246 use Hash::Merge;
  3         5884  
  3         117  
5 3     3   1350 use YAML::Syck;
  3         4614  
  3         172  
6             Hash::Merge::set_behavior ('RIGHT_PRECEDENT');
7 3     3   1400 use Number::RecordLocator;
  3         97090  
  3         118  
8              
9 3     3   20 use base qw/Class::Accessor::Fast/;
  3         4  
  3         1750  
10             __PACKAGE__->mk_accessors (qw/stash last_sp_cnt/);
11              
12 3     3   8697 use vars qw/$CONFIG/;
  3         4  
  3         2409  
13              
14             sub new {
15 1     1 1 96766 my $class = shift;
16 1         2 my %args = @_;
17              
18 1         1 my $self = {};
19 1         3 bless $self , $class;
20 1         5 $self->stash( {} );
21 1         60 $self->last_sp_cnt ( 500000 + int( rand 100 ) );
22 1         7 $self->load;
23 0         0 return $self;
24             }
25              
26             sub app_config_path {
27 1     1 0 1 my $self = shift;
28              
29             # XXX: get application config path for different platform
30             # windows , unix or ...
31 1         9 return $ENV{HOME} . '/.jaipo.yml';
32             }
33              
34 0     0 0 0 sub app { return shift->_get( application => @_ ) }
35              
36 0     0 0 0 sub user { return shift->_get( user => @_ ) }
37              
38             # A teeny helper
39 0     0   0 sub _get { return $_[0]->stash->{ $_[1] }{ $_[2] } }
40              
41              
42             sub save {
43 1     1 0 1 my $self = shift;
44 1   33     5 my $config_filepath = shift || $self->app_config_path;
45              
46 1         107 open CONFIG_FH , ">" , $config_filepath;
47 0         0 print CONFIG_FH Dump( $self->stash );
48 0         0 close CONFIG_FH;
49             }
50              
51              
52             sub set_service_option {
53 0     0 0 0 my $self = shift;
54 0         0 my $sp_name = shift;
55 0         0 my $opt = shift;
56              
57 0         0 my $new_config = $self->stash;
58 0         0 my @sps = @{ $self->app('Services') };
  0         0  
59 0         0 for( my $i=0; my $sp = $sps[ $i ] ; $i++ ) {
60 0         0 my $c_spname = join q{},keys %{ $sps[$i] } ;
  0         0  
61              
62 0 0 0     0 if( $c_spname eq $sp_name
63             and $sp->{ $c_spname }->{sp_id} eq $opt->{sp_id} )
64             {
65 0         0 $new_config->{application}{Services}->[$i]->{ $c_spname } = $opt
66             }
67             }
68 0         0 $self->stash( $new_config );
69             }
70              
71             =head2 find_service_option_by_name
72              
73             Returns a config hash
74              
75             =cut
76              
77             sub find_service_option_by_name {
78 0     0 1 0 my $self = shift;
79 0         0 my $name = shift;
80 0         0 my @services = @{ $self->app ('Services') };
  0         0  
81              
82             # @services = grep { $name eq shift keys $_ }, @services;
83              
84 0         0 my @configs = ();
85 0         0 map { my ($p)=keys %$_;
  0         0  
86 0 0       0 push @configs,values %$_
87             if $p =~ m/\Q$name/ } @services;
88 0 0       0 return wantarray ? @configs : $configs[0];
89             }
90              
91              
92             =head2 find_service_option_by_trigger
93              
94             in list context , returns an array of one or more service plugin
95             config hash.
96             in scalar contxt , returnes a config hash
97              
98             =cut
99              
100             sub find_service_option_by_trigger {
101 0     0 1 0 my $self = shift;
102 0         0 my $trigger = shift;
103 0         0 my @services = @{ $self->app ('Services') };
  0         0  
104              
105             # @services = grep { $name eq shift keys $_ }, @services;
106              
107 0         0 my @configs = ();
108             map {
109 0         0 my ($v)=values %$_;
  0         0  
110             push @configs,$v
111 0 0       0 if $v->{trigger_name} eq $trigger;
112             } @services;
113 0 0       0 return wantarray ? @configs : $configs[0];
114             }
115              
116              
117              
118             =head2 load
119              
120             =cut
121              
122             sub load {
123 1     1 1 1 my $self = shift;
124 1   33     6 my $config_filepath = shift || $self->app_config_path;
125              
126             # if we can not find yml config file
127             # load from default function , then write back to config file
128 1         2 my $config;
129              
130 1 50       57 if ( -e $config_filepath ) { # XXX: check config version
131 0         0 $config = LoadFile( $config_filepath );
132 0         0 $self->stash ($config);
133             }
134             else {
135 1         5 $config = $self->load_default_config;
136 1         100 $self->stash ($config);
137             # write back
138 1         6 $self->save( $config_filepath );
139             }
140              
141             # Canicalization
142 0         0 $self->set_sp_id;
143 0         0 $self->set_default_trigger_name;
144              
145             # load user jaipo yaml config from file here
146 0         0 return $config;
147             }
148              
149             sub set_default_trigger_name {
150 0     0 0 0 my $self = shift;
151 0         0 my $new_config = $self->stash;
152 0         0 my @sps = @{ $self->app('Services') };
  0         0  
153              
154             # set default trigger name for each service plugin that has no trigger name
155 0         0 my %triggers;
156 0         0 for( my $i=0; my $sp = $sps[$i] ; $i++ ) {
157 0         0 my $c_spname = join q{},keys %{ $sp } ;
  0         0  
158              
159 0         0 my $tn = lc $c_spname;
160 0         0 $tn .= '_' while( exists $triggers{ $tn } );
161              
162 0   0     0 $new_config->{application}{Services}->[$i]->{ $c_spname }->{trigger_name} ||= $tn;
163 0         0 $triggers{ $new_config->{application}{Services}->[$i]->{ $c_spname }->{trigger_name} } = 1;
164             }
165              
166 0         0 $self->stash( $new_config );
167             }
168              
169             sub set_sp_id {
170 0     0 0 0 my $self = shift;
171 0         0 my $sp_cnt = $self->last_sp_cnt;
172 0         0 my $new_config = $self->stash;
173 0         0 my @sps = @{ $self->app('Services') };
  0         0  
174 0         0 my $num_rec = Number::RecordLocator->new;
175 0         0 for( my $i=0; $i < scalar @sps ; $i++ ) {
176 0         0 my $c_spname = join q{},keys %{ $sps[$i] } ;
  0         0  
177              
178 0 0       0 if( ! defined $new_config->{application}{Services}->[$i]->{ $c_spname }->{sp_id} ) {
179 0         0 my $sp_id = $num_rec->encode( $sp_cnt ++ );
180 0         0 $new_config->{application}{Services}->[$i]->{ $c_spname }->{sp_id} = $sp_id;
181 0         0 $self->last_sp_cnt( $sp_cnt );
182             }
183             }
184 0         0 $self->stash( $new_config );
185             }
186              
187             sub load_default_config {
188              
189             # move this to a file later
190 1     1 0 1 my $config = <<YAML;
191             ---
192             application:
193             Verbose: 1
194             SavePassword: 0
195             Services:
196             - Twitter:
197             enable: 1
198             Plugins: {}
199             user: {}
200              
201             YAML
202 1         5 return Load( $config );
203              
204             }
205              
206             1;