The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Devel::Ladybug::Name - A unique secondary key

SYNOPSIS

  use Devel::Ladybug qw| :all |;

  #
  # Permit NULL values for "name":
  #
  create "YourApp::Example" => {
    name => Devel::Ladybug::Name->assert(
      subtype(
        optional => true
      )
    ),

    # ...
  };

DESCRIPTION

Devel::Ladybug uses "named objects". By default, name is a human-readable unique secondary key. It's the name of the object being saved. Like all attributes, name must be defined when saved, unless asserted as optional (see "undef Requires Assertion" in Devel::Ladybug.).

The value for name may be changed (as opposed to id, which should not be tinkered with), as long as the new name does not conflict with any objects in the same class when saved. Since Devel::Ladybug objects refer to one another by GUID, names can be changed freely without impacting dependent objects.

Objects may be loaded by name using the loadByName class method.

  #
  # Rename an object
  #
  my $person = YourApp::Person->loadByName("Bob");

  $person->setName("Jim");

  $person->save(); # Bob is now Jim.

name, or any attribute type in Devel::Ladybug, may be may be keyed in combination with multiple attributes via the unique subtype argument, which adds InnoDB reference options to the schema. Provide the names of the attributes which you are uniquely keying with as values to the unique subtype arg.

At the time of this writing, total column length for keys in InnoDB (regardless of if you're using singular or combinatorial keys) may not exceed 255 bytes (when using UTF8 encoding, as Devel::Ladybug does).

  #
  # Key using a combination of name + other attributes
  #
  create "YourApp::Example" => {
    name => Devel::Ladybug::Name->assert(
      subtype(
        unique => "parentId"
      )
    ),

    parentId => Devel::Ladybug::ExtID->assert("YourApp::Example"),

    # ...
  };

name's typing rules may be altered in the class prototype to use Devel::Ladybug classes other than Devel::Ladybug::Name. Subtyping rules for uniqueness are not provided by default for other Devel::Ladybug classes, though, so this should be included by the developer when implementing the class, for example:

  #
  # Make sure that all names are unique, fully qualified hostnames:
  #
  create "YourApp::Example" => {
    name => Devel::Ladybug::Domain->assert(
      subtype(
        unique => true
      ),
    ),

    # ...
  };

SEE ALSO

This file is part of Devel::Ladybug.