NAME
Devel::Ladybug::Type - Devel::Ladybug::Object data type assertions
SYNOPSIS
Typing rules for instance variables are asserted in class prototypes, as inline keys:
use Devel::Ladybug qw| :all |;
create "YourApp::Example" => {
#
# Instance variable "foo" will contain optional string data:
#
foo => Devel::Ladybug::Str->assert(
subtype(
optional => true
)
),
...
};
More examples can be found in the Devel::Ladybug::Class and Devel::Ladybug::Subtype modules, and in the documentation for specific object classes.
DESCRIPTION
Devel::Ladybug::Type subclasses describe rules which Devel::Ladybug::Object instance variables must conform to.
If a caller tries to do something contrary to a Type assertion, Devel::Ladybug will throw an exception, which causes an exit unless caught using eval
/$@
or try
/catch
(see Error).
Dynamic Subclasses
The "built-in" subclasses derived from Devel::Ladybug::Type and Devel::Ladybug::Subtype are auto-generated, and have no physical modules.
Types may be modified by Subtypes via the subtype
function. See Devel::Ladybug::Subtype for more details and a list of these rule types. Subtype subclasses are allocated from the definitions found in the %Devel::Ladybug::Type::RULES package variable.
TEST SUBS
The remainder of this doc contains information which is generally only useful if hacking on core Devel::Ladybug internals.
These subs are defined as package constants, and are used internally by Devel::Ladybug. These subs are not for general usage, but for coding Devel::Ladybug internals.
Each returns a CODE block which may be used to validate data types.
When executed, the test subs throw a Devel::Ladybug::AssertFailed
exception on validation failure, and return true
on success.
insist($value, Code $test);
Runs the received value against a test sub.
# # Devel::Ladybug will agree, $str is string-like. # my $str = "Hello"; insist($str, Devel::Ladybug::Type::isStr); # # This will throw an exception: # my $notStr = { }; insist($notStr, Devel::Ladybug::Type::isStr);
isStr(Str $value)
Returns a CODE ref which tests the received value for string-ness.
isFloat(Num $value)
Returns a CODE ref which tests the received value for float-ness.
isInt(Num $value)
Returns a CODE ref which tests the received value for int-ness.
isArray(Array $value)
Returns a CODE ref which tests the received value for array-ness.
isBool(Bool $value)
Returns a CODE ref which tests the received value for bool-ness.
isCode(Code $code)
Returns a CODE ref which tests the received value for CODE-ness.
isHash(Hash $value)
Returns a CODE ref which tests the received value for hash-ness.
isIO(IO $io)
Returns a CODE ref which tests the received value for IO-ness.
isRef(Ref $ref)
Returns a CODE ref which tests the received value for ref-ness.
isRule(Rule $rule)
Returns a CODE ref which tests the received value for regex-ness.
isScalar(Str $scalar)
Returns a CODE ref which tests the received value for scalar-ness.
PUBLIC CLASS METHODS
These methods are used internally by Devel::Ladybug at a low level, and normally won't be accessed directly.
If creating a new Type subclass from scratch, its constructors and methods would need to implement this interface.
$class->new(%args)
Instantiate a new Devel::Ladybug::Type object.
Consumed args are as follows:
my $type = Devel::Ladybug::Type::MyType->new( code => ..., # CODE ref to test with allowed => ..., # ARRAY ref of allowed vals default => ..., # Literal default value columnType => ..., # Override column type string sqlValue => ..., # Override SQL insert value unique => ..., # true|false optional => ..., # true|false serial => ..., # true|false min => ..., # min numeric value max => ..., # max numeric value size => ..., # fixed length or scalar size minSize => ..., # min length or scalar size maxSize => ..., # max length or scalar size regex => ..., # optional regex which value must match memberType => ..., # Sub-assertion for arrays memberClass => ..., # Name of inline or external class uom => ..., # String label for human reference descript => ..., # Human-readable description example => ..., # An example value for human reference deleteRefOpt => ..., # MySQL foreign constraint reference option updateRefOpt => ..., # MySQL foreign constraint reference option );
READ-ONLY ATTRIBUTES
Although these are public-ish, there normally should not be a need to access them directly.
$type->allowed($value)
Returns true if the received value is allowed, otherwise throws an exception.
$type->code()
Returns the CODE ref used to test this attribute's value for correctness. The code ref is a sub{ } block which takes the value as an argument, and returns a true or false value.
$type->memberType()
Used for Arrays only. Returns a "sub-assertion" (another Devel::Ladybug::Type object) which is unrolled for array elements.
$type->memberClass()
Used for Devel::Ladybug::ExtID assertions only. Returns the name of the class which this attribute is a pointer to.
$type->externalClass()
Convenience wrapper for
memberClass
, but also works for Arrays of ExtIDs. One-to-one foreign keys are asserted as ExtID, but one-to-many keys are an ExtID assertion wrapped in an Array assertion. This means a lot of double-checking in code later, so this method exists to handle both cases without fuss.Used for Devel::Ladybug::ExtID assertions (one-to-one) and Devel::Ladybug::Array assertions encapsulating an ExtID, to return the name of the class which the current attribute is a pointer to.
$type->objectClass()
Returns the concrete object class which this type is for.
PUBLIC INSTANCE METHODS
$self->class()
Object wrapper for Perl's built-in ref() function
$type->test($key, $value)
Send the received value to the code reference returned by $type->code(). Warns and returns a false value on test failure, otherwise returns true.
$key
is included so the caller may know what the warning was for!XXX TODO The individual tests need moved out of this monolithic sub, and into the assertion code tests. Will make things cleaner and faster.
SEE ALSO
Devel::Ladybug::Class, Devel::Ladybug::Subtype
This file is part of Devel::Ladybug.
REVISION
$Id: $