NAME
Yancy::Util - Utilities for Yancy
VERSION
version 1.081
SYNOPSIS
use Yancy::Util qw( load_backend );
my $be = load_backend( 'test://localhost', $schema );
use Yancy::Util qw( curry );
my $helper = curry( \&_helper_sub, @args );
use Yancy::Util qw( currym );
my $sub = currym( $object, 'method_name', @args );
use Yancy::Util qw( match );
if ( match( $where, $item ) ) {
say 'Matched!';
}
use Yancy::Util qw( fill_brackets );
my $value = fill_brackets( $template, $item );
DESCRIPTION
This module contains utility functions for Yancy.
SUBROUTINES
load_backend
my $backend = load_backend( $backend_url, $schema );
my $backend = load_backend( { $backend_name => $arg }, $schema );
my $backend = load_backend( $db_object, $schema );
Get a Yancy backend from the given backend URL, or from a hash reference with a backend name and optional argument. The $schema
hash is the configured JSON schema for this backend.
A backend URL should begin with a name followed by a colon. The first letter of the name will be capitalized, and used to build a class name in the Yancy::Backend
namespace.
The $backend_name
should be the name of a module in the Yancy::Backend
namespace. The $arg
is handled by the backend module. Read your backend module's documentation for details.
The $db_object
can be one of: Mojo::Pg, Mojo::mysql, Mojo::SQLite, or a subclass of DBIx::Class::Schema. The appropriate backend object will be created.
See "Database Backend" in Yancy::Guides::Schema for information about backend URLs and Yancy::Backend for more information about backend objects.
curry
my $curried_sub = curry( $sub, @args );
Return a new subref that, when called, will call the passed-in subref with the passed-in @args
first.
For example:
my $add = sub {
my ( $lop, $rop ) = @_;
return $lop + $rop;
};
my $add_four = curry( $add, 4 );
say $add_four->( 1 ); # 5
say $add_four->( 2 ); # 6
say $add_four->( 3 ); # 7
This is more-accurately called partial application, but curry
is shorter.
currym
my $curried_sub = currym( $obj, $method, @args );
Return a subref that, when called, will call given $method
on the given $obj
with any passed-in @args
first.
See "curry" for an example.
copy_inline_refs
my $subschema = copy_inline_refs( $schema, '/user' );
Given:
- a "source" JSON schema (will not be mutated)
- a JSON Pointer into the source schema, from which to be copied
will return another, copied standalone JSON schema, with any $ref
either copied in, or if previously encountered, with a $ref
to the new location.
match
my $bool = match( $where, $item );
Test if the given $item
matches the given SQL::Abstract $where
data structure. See "WHERE CLAUSES" in SQL::Abstract for the full syntax.
Not all of SQL::Abstract's syntax is supported yet, so patches are welcome.
order_by
my $ordered_array = order_by( $order_by, $unordered_array );
Order the given arrayref by the given SQL::Abstract order-by clause.
fill_brackets
my $string = fill_brackets( $template, $item );
This routine will fill in the given template string with the values from the given $item
hashref. The template contains field names within curly braces. Values in the $item
hashref will be escaped with "xml_escape" in Mojo::Util.
my $item = {
name => 'Doug Bell',
email => 'doug@example.com',
quote => 'I <3 Perl',
};
# Doug Bell <doug@example.com>
fill_brackets( '{name} <{email}>', $item );
# I <3 Perl
fill_brackets( '{quote}', $item );
is_type
my $bool = is_type( $schema->{properties}{myprop}{type}, 'boolean' );
Returns true if the given JSON schema type value (which can be a string or an array of strings) contains the given value, allowing the given type for the property.
# true
is_type( 'boolean', 'boolean' );
is_type( [qw( boolean null )], 'boolean' );
# false
is_type( 'string', 'boolean' );
is_type( [qw( string null )], 'boolean' );
is_format
my $bool = is_format( $schema->{properties}{myprop}{format}, 'date-time' );
Returns true if the given JSON schema format value (a string) is the given value.
# true
is_format( 'date-time', 'date-time' );
# false
is_format( 'email', 'date-time' );
derp
derp "This feature is deprecated in file '%s'", $file;
Print out a deprecation message as a warning. A message will only be printed once for each set of arguments from each caller.
json_validator
my $json_validator = json_validator( $schema );
Build a JSON::Validator object for the given schema, adding all the necessary attributes.
SEE ALSO
AUTHOR
Doug Bell <preaction@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2021 by Doug Bell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.