项目作者: raku-community-modules

项目描述 :
Flat, JSON-backed read-write configuration
高级语言: Raku
项目地址: git://github.com/raku-community-modules/Config-JSON.git
创建时间: 2018-01-07T01:25:09Z
项目社区:https://github.com/raku-community-modules/Config-JSON

开源协议:Artistic License 2.0

下载


Build Status

NAME

Config::JSON - flat, JSON-backed read-write configuration

TABLE OF CONTENTS

SYNOPSIS

  1. use Config::JSON; # uses './config.json' by default
  2. say jconf('foo')//'no such option'; # "no such option"
  3. say jconf-write('foo', 'bar');
  4. say jconf 'foo'; # "bar"
  5. say jconf-write('foo', {bar => [<a b c>]});
  6. say jconf('foo').perl; # ${:bar($["a", "b", "c"])}

Custom config files:

  1. use Config::JSON 'meow.json';
  2. say jconf 'foo';
  1. use Config::JSON ''; # <-- empty string is required
  2. say jconf 'meow.json'.IO, 'foo'; # specify file during calls

DESCRIPTION

Simple read-write configuration, using JSON saved in a file. By design, the
API provides flat key/value structure only, but you’re free to save nested
structures under the keys.

SINGLE CONFIG FILE MODE

The configuration file to use is specified on the use line:

  1. use Config::JSON; # default './config.json'
  2. use Config::JSON 'foo.json'; # custom './foo.json'
  3. use Config::JSON 'foo.json'.IO; # also OK

If the file doesn’t exist, it will be automatically created. If you wish to
create it manually, its outer data structure must be a JSON object:

  1. constant $file = 'foo.json'.IO;
  2. BEGIN $file.spurt: '{ "meow": 42 }';
  3. use Config::JSON $file;
  4. say jconf 'meow'; # 42

EXPORTED SUBROUTINES

jconf

  1. multi jconf (Whatever --> Mu);
  2. multi jconf (Str:D $key --> Mu);

Reads config file and looks up $key in the config hash. Returns its value
if it :exists, otherwise, fails
with Config::JSON::X::NoSuchKey exception. If $key is Whatever, returns
the entire config hash.

If config file could not be read, fails
with Config::JSON::X::Open exception.

jconf-write

  1. sub jconf-write (Str:D $key, Mu $value --> Nil);

Saves $value under $key, possibly overwriting previously-existing value.
Reads the config from file before writing.

If config file could not be read or written,
fails
with Config::JSON::X::Open exception.

PER-CALL CONFIG FILE MODE

  1. use Config::JSON ''; # no auto config file; pass filename to each call
  2. # of config read/write routines instead

EXPORTED SUBROUTINES

Note that these are not available in single-config-file mode.

jconf

  1. multi jconf (IO::Path:D $file, Whatever --> Mu);
  2. multi jconf (IO::Path:D $file, Str:D $key --> Mu);

Same as jconf for single-config-file version, except takes the
name of the config file as the first argument.

jconf-write

  1. sub jconf-write (IO::Path:D $file, Str:D $key, Mu $value --> Nil);

Same as jconf-write for single-config-file mode, except takes the
name of the config file as the first argument.

EXCEPTIONS

Config::JSON::X::NoSuchKey

  1. has Str:D $.key is required;
  2. has IO::Path:D $.file is required;
  3. method message {
  4. "Key `$!key` is not present in the config file `$!file.absolute()`"
  5. }

Config::JSON::X::Open

  1. has Exception:D $.e is required;
  2. has IO::Path:D $.file is required;
  3. method message {
  4. "Received $!e.^name() with message `$!e.message()` while trying to"
  5. ~ " open config file `$!file.absolute()`"
  6. }

MULTI-THREAD/PROCESS SAFETY

The routines perform advisory locking of the config file on each read and write.

CAVEATS

Rakudo’s bug R#1370 prevents
use of this module in precompiled modules when using default config file
or specifying one on the use line. Use no precompilation
pragma to work-around it.

Using empty string on use line and specifying config file’s name to each
routine does not trigger this bug.


REPOSITORY

Fork this module on GitHub:
https://github.com/raku-community-modules/Config-JSON

BUGS

To report bugs or request features, please use
https://github.com/raku-community-modules/Config-JSON/issues

AUTHOR

Zoffix Znet (http://perl6.party/)

LICENSE

You can use and distribute this module under the terms of the
The Artistic License 2.0. See the LICENSE file included in this
distribution for complete details.

The META6.json file of this distribution may be distributed and modified
without restrictions or attribution.