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

OP::Name - A unique secondary key

SYNOPSIS

  use OP;

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

    # ...
  };

DESCRIPTION

OP 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 OP.).

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 OP 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 OP, may be may be keyed in combination with multiple attributes via the ::unique OP::Subtype argument, which adds InnoDB reference options to the schema. Provide the names of the attributes which you are uniquely keying with as arguments to the ::unique subtype constructor.

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 OP does).

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

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

    # ...
  };

name's typing rules may be altered in the class prototype to use OP classes other than OP::Name. Subtyping rules for uniqueness are not provided by default for other OP 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 => OP::Domain->assert(
      subtype(
        unique => true
      ),
    ),

    # ...
  };

SEE ALSO

This file is part of OP.