| 1 | package GraphViz2::Marpa::Config; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | ||
| 6 | |||
| 7 | use Config::Tiny; | ||
| 8 | |||
| 9 | use File::HomeDir; | ||
| 10 | use File::Spec; | ||
| 11 | |||
| 12 | use Moo; | ||
| 13 | |||
| 14 | has config => | ||
| 15 | ( | ||
| 16 | default => sub{return {} }, | ||
| 17 | is => 'rw', | ||
| 18 | # isa => 'HashRef', | ||
| 19 | required => 0, | ||
| 20 | ); | ||
| 21 | |||
| 22 | has config_file_path => | ||
| 23 | ( | ||
| 24 | default => sub{return ''}, | ||
| 25 | is => 'rw', | ||
| 26 | # isa => 'Str', | ||
| 27 | required => 0, | ||
| 28 | ); | ||
| 29 | |||
| 30 | has section => | ||
| 31 | ( | ||
| 32 | default => sub{return ''}, | ||
| 33 | is => 'rw', | ||
| 34 | # isa => 'Str', | ||
| 35 | required => 0, | ||
| 36 | ); | ||
| 37 | |||
| 38 | our $VERSION = '2.12'; | ||
| 39 | |||
| 40 | # ----------------------------------------------- | ||
| 41 | |||
| 42 | sub BUILD | ||
| 43 | { | ||
| 44 | my($self) = @_; | ||
| 45 | my($path) = File::Spec -> catfile(File::HomeDir -> my_dist_config('GraphViz2-Marpa'), '.htgraphviz2.marpa.conf'); | ||
| 46 | |||
| 47 | $self -> read($path); | ||
| 48 | |||
| 49 | } # End of BUILD. | ||
| 50 | |||
| 51 | # ----------------------------------------------- | ||
| 52 | |||
| 53 | sub read | ||
| 54 | { | ||
| 55 | my($self, $path) = @_; | ||
| 56 | |||
| 57 | $self -> config_file_path($path); | ||
| 58 | |||
| 59 | # Check [global]. | ||
| 60 | |||
| 61 | $self -> config(Config::Tiny -> read($path) ); | ||
| 62 | |||
| 63 | if (Config::Tiny -> errstr) | ||
| 64 | { | ||
| 65 | die Config::Tiny -> errstr; | ||
| 66 | } | ||
| 67 | |||
| 68 | $self -> section('global'); | ||
| 69 | |||
| 70 | if (! ${$self -> config}{$self -> section}) | ||
| 71 | { | ||
| 72 | die "Config file '$path' does not contain the section [@{[$self -> section]}]\n"; | ||
| 73 | } | ||
| 74 | |||
| 75 | # Check [x] where x is host=x within [global]. | ||
| 76 | |||
| 77 | $self -> section(${$self -> config}{$self -> section}{'host'}); | ||
| 78 | |||
| 79 | if (! ${$self -> config}{$self -> section}) | ||
| 80 | { | ||
| 81 | die "Config file '$path' does not contain the section [@{[$self -> section]}]\n"; | ||
| 82 | } | ||
| 83 | |||
| 84 | # Move desired section into config, so caller can just use $self -> config to get a hashref. | ||
| 85 | |||
| 86 | $self -> config(${$self -> config}{$self -> section}); | ||
| 87 | |||
| 88 | } # End of read. | ||
| 89 | |||
| 90 | # -------------------------------------------------- | ||
| 91 | |||
| 92 | 1; | ||
| 93 | |||
| 94 | =pod | ||
| 95 | |||
| 96 | =head1 NAME | ||
| 97 | |||
| 98 | C<GraphViz2::Marpa::Config> - A config file helper for C<GraphViz2::Marpa> | ||
| 99 | |||
| 100 | =head1 Synopsis | ||
| 101 | |||
| 102 | See L<GraphViz2::Marpa>. | ||
| 103 | |||
| 104 | =head1 Description | ||
| 105 | |||
| 106 | L<GraphViz2::Marpa> provides a Marpa-based parser for Graphviz C<dot> files, | ||
| 107 | and this module helps finding and loading the config file, which in turn | ||
| 108 | helps generate the demo page. | ||
| 109 | |||
| 110 | This module is really only of interest to the author. | ||
| 111 | |||
| 112 | =head1 Constructor and Initialization | ||
| 113 | |||
| 114 | =head2 Calling new() | ||
| 115 | |||
| 116 | C<new()> is called as C<< my($obj) = GraphViz2::Marpa::Config -> new(k1 => v1, k2 => v2, ...) >>. | ||
| 117 | |||
| 118 | It returns a new object of type C<GraphViz2::Marpa::Config>. | ||
| 119 | |||
| 120 | Key-value pairs accepted in the parameter list: | ||
| 121 | |||
| 122 | =over 4 | ||
| 123 | |||
| 124 | =item o (none) | ||
| 125 | |||
| 126 | =back | ||
| 127 | |||
| 128 | =head1 Methods | ||
| 129 | |||
| 130 | =head2 read($path) | ||
| 131 | |||
| 132 | Uses $path to find and read the config file into a hashref. By default it assumes | ||
| 133 | scripts/copy.config.pl has been run, and loads (effectively from config/), | ||
| 134 | .htgraphviz2.marpa.conf. | ||
| 135 | |||
| 136 | If the file can't be read, die is called. | ||
| 137 | |||
| 138 | See also scripts/copy.config.pl and scripts/find.config.pl. | ||
| 139 | |||
| 140 | =head1 Machine-Readable Change Log | ||
| 141 | |||
| 142 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 143 | |||
| 144 | =head1 Version Numbers | ||
| 145 | |||
| 146 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 147 | |||
| 148 | =head1 Repository | ||
| 149 | |||
| 150 | L<https://github.com/ronsavage/GraphViz2-Marpa> | ||
| 151 | |||
| 152 | =head1 Support | ||
| 153 | |||
| 154 | Email the author, or log a bug on RT: | ||
| 155 | |||
| 156 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=GraphViz2::Marpa>. | ||
| 157 | |||
| 158 | =head1 Author | ||
| 159 | |||
| 160 | L<GraphViz2::Marpa> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2012. | ||
| 161 | |||
| 162 | Home page: L<http://savage.net.au/index.html>. | ||
| 163 | |||
| 164 | =head1 Copyright | ||
| 165 | |||
| 166 | Australian copyright (c) 2012, Ron Savage. | ||
| 167 | |||
| 168 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 169 | you can redistribute them and/or modify them under the terms of | ||
| 170 | The Perl License, a copy of which is available at: | ||
| 171 | http://dev.perl.org/licenses/ | ||
| 172 | |||
| 173 | =cut |