Skip to content

Commit

Permalink
Added a perl method and a :trace argument to BUILD
Browse files Browse the repository at this point in the history
  • Loading branch information
MARTIMM committed Feb 18, 2017
1 parent 3c2f5b4 commit 89e9261
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 20 deletions.
2 changes: 1 addition & 1 deletion META.info
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"Config::DataLang::Refine": "lib/Config/DataLang/Refine.pm6"
},
"source-url": "git://github.com/MARTIMM/config-datalang-refine.git",
"version": "0.5.0"
"version": "0.6.0"
}
3 changes: 3 additions & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ See [semantic versioning](http://semver.org/). Please note point 4. on
that page: ***Major version zero (0.y.z) is for initial development. Anything may
change at any time. The public API should not be considered stable.***

* 0.6.0
* Added a perl method to show the current data structure in a pretty print format. A Hash can be provided to show that instead. Nice to see the results of a refine call.
* Added a :trace argument to BUILD (new). When True, the files loaded are shown. This comes in handy when the search for files gets complex. Also good to see when there are any unexpected files are found and read into the configuration.
* 0.5.0
* Added type C-UNIX-OPTS-T3 to handle negated options specially used on perl6 command line when MAIN sub is defined. :filter is ignored in this case because tis filters out false booleans.
* 0.4.7
Expand Down
Binary file modified doc/Refine.pdf
Binary file not shown.
62 changes: 54 additions & 8 deletions lib/Config/DataLang/Refine.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ class Config::DataLang::Refine:auth<https://github.com/MARTIMM> {
has Str $!config-content = '';
has Hash $.config;

has Str $!p-out;
has Int $!p-lvl;

has Bool $!trace = False;

enum StrMode is export <
C-URI-OPTS-T1 C-URI-OPTS-T2 C-UNIX-OPTS-T1 C-UNIX-OPTS-T2 C-UNIX-OPTS-T3
>;

#-----------------------------------------------------------------------------
submethod BUILD (
Str :$config-name = '', Bool :$merge = False, Array :$locations = [],
Str :$data-module = 'Config::TOML', Hash :$other-config = {}
Str :$config-name = '', Bool :$!merge = False, Array :$!locations = [],
Str :$data-module = 'Config::TOML', Hash :$other-config = {},
Bool :$!trace = False
) {

$!merge = $merge;
$!locations = $locations;

# When the caller provides a configuration as a base, set that as a
# starting point and set merge to True
$!merge = True if $other-config.elems;
Expand Down Expand Up @@ -117,6 +120,8 @@ class Config::DataLang::Refine:auth<https://github.com/MARTIMM> {
#-----------------------------------------------------------------------------
method read-config ( Str $config-name ) {

note "\nSearch using name $config-name" if $!trace;

$!config-content = '';

# Get all locations and push the path when config is found and readable
Expand Down Expand Up @@ -153,7 +158,13 @@ class Config::DataLang::Refine:auth<https://github.com/MARTIMM> {

# Start with the last entry from the locations
for @$locs.reverse -> $cfg-name {
#say "CN: $cfg-name";

if $!trace {
my $p = $cfg-name;
$p ~~ s/ $*CWD '/'? //;
$p ~~ s/ $*HOME '/'? /~\//;
note "Merge file $p";
}

$!config-content = slurp($cfg-name) ~ "\n";

Expand All @@ -162,15 +173,20 @@ class Config::DataLang::Refine:auth<https://github.com/MARTIMM> {
$!config,
$!read-from-text($!config-content)
);
#say "CFG: $!config.elems()";
}
}

# no merge, pick first config we find
else {

if ?$locs[0] {
#say "l0: $locs[0]";
if $!trace {
my $p = $locs[0];
$p ~~ s/ $*CWD '/'? //;
$p ~~ s/ $*HOME '/'? /~\//;
note "Use only file $p";
}

$!config-content = slurp($locs[0]);
$!config = $!read-from-text($!config-content);
}
Expand Down Expand Up @@ -423,4 +439,34 @@ class Config::DataLang::Refine:auth<https://github.com/MARTIMM> {

$h3 // {};
}

#-----------------------------------------------------------------------------
method perl ( Hash :$h --> Str ) {

$!p-out = '';
$!p-lvl = 0;

self!dd($h // $!config);
$!p-out;
}

#-----------------------------------------------------------------------------
method !dd ( Hash $h ) {

$!p-out ~= "\n";

for $h.keys.sort -> $k {
if $h{$k} ~~ Hash {
$!p-out ~= (' ' x $!p-lvl) ~ "$k => \{";
$!p-lvl += 2;
self!dd($h{$k});
$!p-lvl -= 2;
$!p-out ~= (' ' x $!p-lvl) ~ "},\n";
}

else {
$!p-out ~= (' ' x $!p-lvl) ~ "$k => $h{$k},\n";
}
}
}
}
25 changes: 14 additions & 11 deletions lib/Config/DataLang/Refine.pod6
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ A sample config from the MongoDB project to test several server setups is
Now, to get run options to start server 1 one does the following;
my Array $opts = $c.refine-str( <mongod s1 replicate1>, :C-UNIX-OPTS-T2);
# Output
# --nojournal, --fork, --smallfiles, --oplogSize=128, --logappend,
# --logpath='./Sandbox/Server1/m.log', --pidfilepath='./Sandbox/Server1/m.pid',
Expand All @@ -106,8 +106,6 @@ configuration should always be a Hash (at this moment).
=head2 config
Defined as
has Hash $.config;
Stored configuration. Can be retrieved directly from object.
Expand All @@ -120,14 +118,13 @@ Stored configuration. Can be retrieved directly from object.
=head2 new
Defined as
submethod BUILD (
Str :$config-name,
Bool :$merge = False,
Array :$locations = [],
Str :$data-module = 'Config::TOML',
Hash :$other-config = {}
Hash :$other-config = {},
Bool :$trace = False
)
Reads configuration text from a file pointed to by C<:config-name>. The file
Expand All @@ -152,13 +149,15 @@ directory if found. Then the options from the hidden local file if found and
finishing with the visible local file found. An exception will be thrown when
the resulting config has no elements.
C<:trace>. When True, the search will show what files are loaded.
The data languages such as C<Config::TOML> might throw exceptions when it
fails to parse the configuration text.
=begin table :capture('')
Setup | Search
==============================|==============================================
==============================|==============================================
Nothing set | :config-name set to program name. Say p.pl6 so
| config will be p.toml because :data-module is
| by default Config::TOML.
Expand Down Expand Up @@ -194,8 +193,6 @@ previous loads.
=head2 refine
Defined as
method refine ( *@key-list, Bool :$filter = False --> Hash )
Processes data in the config using the keys from the @key-list. The method
Expand All @@ -221,8 +218,6 @@ are removed from the result where the value is a C<Bool> and is C<False>.
=head2 refine-str
Defined as
method refine-str (
*@key-list,
Str :$glue = ',',
Expand Down Expand Up @@ -290,4 +285,12 @@ B<Note 4> Mode C<C-UNIX-OPTS-T3> also does the same as C<C-UNIX-OPTS-T1> but
ignores the filter option. The result of False booleans will then be --/$k
instead of --no$k. This is recognized by perl6 as input to the MAIN sub.
=head2 perl
method perl ( Hash :$h --> Str )
Show the configuration stored in this class or, when defined, $h, given as a
named attribute.
=end pod
12 changes: 12 additions & 0 deletions t/300-refine-locations.t
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,34 @@ spurt( '.myCfg.cfg', Q:to/EOOPT/);
#-------------------------------------------------------------------------------
subtest {

# First file to encounter is .myCfg.cfg and stops there because :!merge
my Config::DataLang::Refine $c .= new(:config-name<t/cfg-path/myCfg.cfg>);
diag "Configuration: " ~ $c.perl;

my Hash $o = $c.refine(<app>);
diag "Refined using <app>: " ~ $c.perl(:h($o));

ok $o<workdir>:!exists, "app has no workdir";
is $o<port>, 2345, "port app $o<port>";

$o = $c.refine(<app p1>);
diag "Refined using <app p1>: " ~ $c.perl(:h($o));
is $o<workdir>, '/tmp', "workdir p1 is $o<workdir>";
is $o<port>, 2345, "port p1 $o<port>";



# Merge file t/cfg-path/myCfg.cfg followed by file .myCfg.cfg
$c .= new( :config-name<t/cfg-path/myCfg.cfg>, :merge);
diag "Configuration: " ~ $c.perl;
$o = $c.refine(<app>);
diag "Refined using <app>: " ~ $c.perl(:h($o));

is $o<workdir>, '/var/tmp', "workdir app $o<workdir>";
is $o<port>, 2345, "port app $o<port>";

$o = $c.refine(<app p1>);
diag "Refined using <app p1>: " ~ $c.perl(:h($o));
is $o<host>, 'example.com', "host p1 is $o<host>";
is $o<port>, 2345, "port p1 $o<port>";

Expand Down

0 comments on commit 89e9261

Please sign in to comment.