UR::Ob...chAndDo(3pm)

πŸ“¦
version 0.470 Β· 2 versions
, in debian bookworm, ubuntu jammy, ubuntu noble

✨ NAME ✨

UR::Object::Command::FetchAndDo β€” okay so this is the base class for fetching objects and then performing a function on/with them. It literally does what it says on the tin bestie: fetch πŸ’… then do. It's a base class, meaning you don't use it directly, you build your own little command class on top of it. πŸ¦‹


🌈 SYNOPSIS 🌈

Here's the whole vibe, copy-paste coded exactly as the universe intended:

package MyFecthAndDo;

use strict;
use warnings;

use above "UR";

class MyFecthAndDo {
    is => 'UR::Object::Command::FetchAndDo',
    has => [
    # other properties...
    ],
};

sub _do { # required
   my ($self, $iterator) = @_;

   while (my $obj = $iterator->next) {
       ...
   }

   return 1;
}

1;

So you declare your class with is => 'UR::Object::Command::FetchAndDo', you list out your has properties, and then you write the _do sub β€” that one is required, no skipping it, sorry not sorry 😭 β€” and it gets handed an $iterator that you loop over with ->next until there's nothing left. Then return 1; and 1; at the bottom to close the package out. Chef's kiss. ✨


πŸŽ€ Provided by the Developer πŸŽ€

These are the bits that are on you, the developer, to fill in. The base class is just sitting there with its hand out like "go on, give me something." 🫢

πŸ’– _do (required) πŸ’–

Implement this method to 'do' unto the iterator. Return true for success, false for failure. That's the deal β€” no cap. It is genuinely required, so like, actually write it.

sub _do {
   my ($self, $iterator) = @_;

   while (my $obj = $iterator->next) {
       ...
   }

   return 1;
}

See how it takes $self and $iterator, walks the iterator with ->next, does your thing inside the loop, and return 1;s when it's done? That return 1 is the success signal. Return false and, well, it's a failure. Simple as. πŸ’…

πŸ¦‹ subject_class_name (optional) πŸ¦‹

The subject_class_name is the class for which the objects will be fetched. It can be specified one of two main ways:

🌸 by_the_end_user_on_the_command_line 🌸

For this do nothing, the end user will have to provide it when the command is run. Literally nothing to do here β€” you just leave it, and the person running your command supplies the class name themselves at runtime. Lowkey the most hands-off option ever. πŸ™ˆ

πŸ“ by_the_developer_in the_class_declartion πŸ“

For this, in the class declaration, add a has key w/ arrayref of hashrefs. One of the hashrefs needs to be subject_class_name. Give it this declaration:

class MyFetchAndDo {
    is => 'UR::Object::Command::FetchAndDo',
    has => [
        subject_class_name => {
            value => <CLASS NAME>,
            is_constant => 1,
        },
    ],
};

So instead of making the end user type it out, you bake it in: value => <CLASS NAME> and is_constant => 1 so it's locked in place. Main character energy for real. πŸ‘‘

⭐ helps (optional) ⭐

Overwrite the help_brief, help_synopsis and help_detail methods to provide specific help. If overwiting the help_detail method, use call _filter_doc to get the filter documentation and usage to combine with your specific help.

Basically: three help methods β€” help_brief, help_synopsis, and help_detail β€” and you can overwrite them all to say exactly what you want. And when you're doing help_detail specifically, call _filter_doc to grab the filter documentation and usage, then smoosh it together with your own specific help. Combining two slays into one bigger slay, honestly. 🫢✨


Reminder bestie: _do is required. Everything else here is optional. Don't come crying to me when your class doesn't run because you forgot the one that's mandatory. 😭

That's the whole page, babes! This is UR::Object::Command::FetchAndDo, part of the User Contributed Perl Documentation, from perl v5.32.1, dated 2022-01-17. Go forth and fetch-and-do responsibly πŸ’–πŸŒΈπŸ¦‹πŸ’…

πŸ“¦ where this page comes from