| 1 | # Copyright Infomation | ||||||||||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||||||||||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||||||||||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||||||||||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||||||||||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 8 | package Nile::Var; | ||||||||||
| 9 | |||||||||||
| 10 | 1 | our $VERSION = '0.55'; | |||||||||
| 11 | 1 | our $AUTHORITY = 'cpan:MEWSOFT'; | |||||||||
| 12 | |||||||||||
| 13 | =pod | ||||||||||
| 14 | |||||||||||
| 15 | =encoding utf8 | ||||||||||
| 16 | |||||||||||
| 17 | =head1 NAME | ||||||||||
| 18 | |||||||||||
| 19 | Nile::Var - Application Shared variables. | ||||||||||
| 20 | |||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||
| 22 | |||||||||||
| 23 | # get a reference to the the shared var object | ||||||||||
| 24 | $var = $self->app->var; | ||||||||||
| 25 | |||||||||||
| 26 | # set some variables | ||||||||||
| 27 | $var->set('email', 'ahmed@mewsoft.com'); | ||||||||||
| 28 | $var->set('path', '/var/home/public_html/app'); | ||||||||||
| 29 | $var->set('lang', 'en-US'); | ||||||||||
| 30 | |||||||||||
| 31 | # auto setters | ||||||||||
| 32 | $var->email('ahmed@mewsoft.com'); | ||||||||||
| 33 | $var->lang('en-US'); | ||||||||||
| 34 | |||||||||||
| 35 | # get some variables | ||||||||||
| 36 | $mail = $var->get('email'); | ||||||||||
| 37 | $path = $var->get('path'); | ||||||||||
| 38 | $lang = $var->get('lang'); | ||||||||||
| 39 | |||||||||||
| 40 | # auto getters | ||||||||||
| 41 | $mail = $var->email; | ||||||||||
| 42 | $lang = $var->lang; | ||||||||||
| 43 | |||||||||||
| 44 | # get variables or default values | ||||||||||
| 45 | $value = $var->get('name', 'default'); | ||||||||||
| 46 | $path = $var->get('email', 'root@localhost'); | ||||||||||
| 47 | |||||||||||
| 48 | # set a list of variables | ||||||||||
| 49 | $var->set(%vars); | ||||||||||
| 50 | $var->set(fname=>'Ahmed', lname=>'Elsheshtawy', email=>'ahmed@mewsoft.com'); | ||||||||||
| 51 | |||||||||||
| 52 | # get a list of variables | ||||||||||
| 53 | @values = $var->list(@vars); | ||||||||||
| 54 | |||||||||||
| 55 | # get a list of variables names | ||||||||||
| 56 | @names = $var->keys; | ||||||||||
| 57 | |||||||||||
| 58 | # get a hash reference to the variables | ||||||||||
| 59 | $vars = $var->vars; | ||||||||||
| 60 | $vars->{path} = '/app/path'; | ||||||||||
| 61 | say $vars->{theme}; | ||||||||||
| 62 | |||||||||||
| 63 | # check if variables exist | ||||||||||
| 64 | $found = $self->exists($name); | ||||||||||
| 65 | |||||||||||
| 66 | # delete some vars | ||||||||||
| 67 | $self->delete(@vars); | ||||||||||
| 68 | |||||||||||
| 69 | # clear all vars | ||||||||||
| 70 | $self->clear; | ||||||||||
| 71 | |||||||||||
| 72 | =head1 DESCRIPTION | ||||||||||
| 73 | |||||||||||
| 74 | Nile::Var - Application Shared variables. | ||||||||||
| 75 | |||||||||||
| 76 | =cut | ||||||||||
| 77 | |||||||||||
| 78 | 1 | ▶ | use Nile::Base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 79 | |||||||||||
| 80 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 81 | 0 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: not called CC=3 SCAR=24.8
| |||||||||||
| 82 | 0 | my ($self) = shift; | |||||||||
| 83 | |||||||||||
| 84 | 0 | my ($class, $method) = our $AUTOLOAD =~ /^(.*)::(\w+)$/; | |||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | if ($self->can($method)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | return $self->$method(@_); | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 0 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 0 | $self->{vars}->{$method} = $_[0]; | |||||||||
| 92 | } | ||||||||||
| 93 | else { | ||||||||||
| 94 | 0 | return $self->{vars}->{$method}; | |||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 98 | =head2 clear() | ||||||||||
| 99 | |||||||||||
| 100 | # delete entire xml object data. | ||||||||||
| 101 | $xml->clear(); | ||||||||||
| 102 | |||||||||||
| 103 | Completely clears all loaded xml data from memory. This does not apply to the file until file is | ||||||||||
| 104 | updated or saved. | ||||||||||
| 105 | |||||||||||
| 106 | =cut | ||||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | sub clear { | ||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 109 | 0 | my ($self) = @_; | |||||||||
| 110 | 0 | $self->{vars} = +{}; | |||||||||
| 111 | } | ||||||||||
| 112 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 113 | =head2 vars() | ||||||||||
| 114 | |||||||||||
| 115 | # get all variables as a hash or a hash ref. | ||||||||||
| 116 | %vars = $var->vars(); | ||||||||||
| 117 | $vars = $var->vars(); | ||||||||||
| 118 | |||||||||||
| 119 | Returns all variables as a hash or a hash reference. | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | 0 | ▶ | sub vars { | ||||||||
|
Subroutine
vars: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 124 | 0 | my ($self) = @_; | |||||||||
| 125 | 0 | ▶ | return wantarray? %{$self->{vars}} : $self->{vars}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | } | ||||||||||
| 127 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 128 | =head2 set() | ||||||||||
| 129 | |||||||||||
| 130 | # set variables. | ||||||||||
| 131 | $var->set("users_count", $count); | ||||||||||
| 132 | $var->set(%vars); | ||||||||||
| 133 | |||||||||||
| 134 | # automatic setter support | ||||||||||
| 135 | $var->email('ahmed@mewsoft.com'); # same as $var->set('email', 'ahmed@mewsoft.com'); | ||||||||||
| 136 | |||||||||||
| 137 | Set shared variables. | ||||||||||
| 138 | |||||||||||
| 139 | =cut | ||||||||||
| 140 | |||||||||||
| 141 | 0 | ▶ | sub set { | ||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 142 | 0 | my ($self, %vars) = @_; | |||||||||
| 143 | 0 | map { $self->{vars}->{$_} = $vars{$_}; } keys %vars; | |||||||||
| 144 | 0 | $self; | |||||||||
| 145 | } | ||||||||||
| 146 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 147 | =head2 get() | ||||||||||
| 148 | |||||||||||
| 149 | # get variables | ||||||||||
| 150 | say $var->get("email"); | ||||||||||
| 151 | say $var->get("website", "default value"); | ||||||||||
| 152 | |||||||||||
| 153 | # automatic getter support | ||||||||||
| 154 | say $var->email; # same as $var->get('email'); | ||||||||||
| 155 | |||||||||||
| 156 | Returns shared variables. | ||||||||||
| 157 | |||||||||||
| 158 | =cut | ||||||||||
| 159 | |||||||||||
| 160 | 0 | ▶ | sub get { | ||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 161 | 0 | my ($self, $name, $default) = @_; | |||||||||
| 162 | 0 | ▶ | exists $self->{vars}->{$name}? $self->{vars}->{$name} : $default; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | } | ||||||||||
| 164 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 165 | =head2 list() | ||||||||||
| 166 | |||||||||||
| 167 | # get a list of shared variables. | ||||||||||
| 168 | @vars = $var->list(@names); | ||||||||||
| 169 | |||||||||||
| 170 | Returns a list of shared variables. | ||||||||||
| 171 | |||||||||||
| 172 | =cut | ||||||||||
| 173 | |||||||||||
| 174 | 0 | ▶ | sub list { | ||||||||
|
Subroutine
list: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 175 | 0 | my ($self, @n) = @_; | |||||||||
| 176 | 0 | @{$self->{vars}}{@n}; | |||||||||
| 177 | } | ||||||||||
| 178 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 179 | =head2 keys() | ||||||||||
| 180 | |||||||||||
| 181 | # returns all variables names. | ||||||||||
| 182 | @names = $var->keys($); | ||||||||||
| 183 | |||||||||||
| 184 | Returns all shared variables names. | ||||||||||
| 185 | |||||||||||
| 186 | =cut | ||||||||||
| 187 | |||||||||||
| 188 | 0 | ▶ | sub keys { | ||||||||
|
Subroutine
keys: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 189 | 0 | my ($self) = @_; | |||||||||
| 190 | 0 | (keys %{$self->{vars}}); | |||||||||
| 191 | } | ||||||||||
| 192 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 193 | =head2 exists() | ||||||||||
| 194 | |||||||||||
| 195 | # check if a variable exist or not. | ||||||||||
| 196 | $found = $var->exists($name); | ||||||||||
| 197 | |||||||||||
| 198 | Check if a shared variable exist or not. | ||||||||||
| 199 | |||||||||||
| 200 | =cut | ||||||||||
| 201 | |||||||||||
| 202 | 0 | ▶ | sub exists { | ||||||||
|
Subroutine
exists: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 203 | 0 | my ($self, $name) = @_; | |||||||||
| 204 | 0 | exists $self->{vars}->{$name}; | |||||||||
| 205 | } | ||||||||||
| 206 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 207 | =head2 delete() | ||||||||||
| 208 | |||||||||||
| 209 | # delete shared variables. | ||||||||||
| 210 | $var->delete(@names); | ||||||||||
| 211 | |||||||||||
| 212 | Delete a list of shared variables. | ||||||||||
| 213 | |||||||||||
| 214 | =cut | ||||||||||
| 215 | |||||||||||
| 216 | 0 | ▶ | sub delete { | ||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 217 | 0 | my ($self, @n) = @_; | |||||||||
| 218 | 0 | delete $self->{vars}->{$_} for @n; | |||||||||
| 219 | } | ||||||||||
| 220 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 221 | 0 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||
| 222 | } | ||||||||||
| 223 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
| 224 | |||||||||||
| 225 | =pod | ||||||||||
| 226 | |||||||||||
| 227 | =head1 Bugs | ||||||||||
| 228 | |||||||||||
| 229 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||||||||||
| 230 | |||||||||||
| 231 | =head1 HOMEPAGE | ||||||||||
| 232 | |||||||||||
| 233 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||||||||||
| 234 | |||||||||||
| 235 | =head1 SOURCE | ||||||||||
| 236 | |||||||||||
| 237 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||||||||||
| 238 | |||||||||||
| 239 | =head1 SEE ALSO | ||||||||||
| 240 | |||||||||||
| 241 | See L<Nile> for details about the complete framework. | ||||||||||
| 242 | |||||||||||
| 243 | =head1 AUTHOR | ||||||||||
| 244 | |||||||||||
| 245 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||||||||||
| 246 | Website: http://www.mewsoft.com | ||||||||||
| 247 | |||||||||||
| 248 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 249 | |||||||||||
| 250 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||||||||||
| 251 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||||||||||
| 252 | |||||||||||
| 253 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||
| 254 | |||||||||||
| 255 | =cut | ||||||||||
| 256 | |||||||||||
| 257 | 1 | 1; | |||||||||