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

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

πŸ’– UR::Object::Type::AccessorWriter πŸ’–

okay bestie, listen. this page is all about UR::Object::Type::AccessorWriter, and it's living in the Perl world as UR::Object::Type::AccessorWriter(3pm). it's the little helper module that hangs out with UR::Object::Type and is literally responsible for creating accessors for properties. yes, the whole glow-up of "how do i get/set this thing" is handled here. πŸ’…


✨ NAME ✨

UR::Object::Type::AccessorWriter β€” a helper module for UR::Object::Type whose whole job is making accessors for properties. that's it, that's the gig. πŸŽ€


🌸 DESCRIPTION 🌸

so here's the tea: the subroutines inside this module actually live in the UR::Object::Type namespace. this module is just a cute little gathering spot where they all get collected. like, the function names are lying to you a tiny bit β€” they're not living here, they just visit. πŸ™ˆ

the class initializer calls these subroutines when it's time to create accessor methods for a newly defined class. and the way each accessor gets built? it's implemented by a closure, which then gets assigned a name by Sub::Name, and then gets inserted into the defined class's namespace by Sub::Install. closure β†’ name β†’ namespace. the full makeover pipeline. ✨


πŸ‘‘ METHODS πŸ‘‘

πŸ’œ initialize_direct_accessors

$classobj->initialize_direct_accessors();

this is the entry point into the accessor writing system, the main character fr fr. it goes and inspects every single item in the 'has' key of the class object's hashref, and creates methods for each property. every last one. no skipping. πŸ”₯

πŸ¦‹ mk_rw_accessor

$classobj->mk_rw_accessor($class_name, $accessor_name, $column_name, $property_name, $is_transient);

makes a mutable accessor named $accessor_name, and it stores its value in the $property_name key of the object's hashref. read and write, we love a flexible icon. πŸ’…

πŸ“ mk_ro_accessor

$classobj->mk_ro_accessor($class_name, $accessor_name, $column_name, $property_name);

creates a read-only accessor named $accessor_name that retrieves its value from the $property_name key of the object's hashref. look but don't touch, bestie.

⚠️ but here's the thing: if you try to use it as a mutator by passing a value into the method, it will throw an exception with Carp::croak. it will not be nice about it. do not test it. 😭

🌈 mk_id_based_object_accessor

$classobj->mk_id_based_object_accessor($class_name, $accessor_name, $id_by,
                                       $r_class_name, $where);

creates an object accessor named $accessor_name. it returns objects of type $r_class_name, id-ed by the parameters named in the $id_by arrayref. $where is an optional listref of extra filters to apply when retrieving objects. 🫢

and the behavior of the created accessor depends on how many params you pass it, so pay attention:

  • 0 params β€” it retrieves the object pointed to by $r_class_name and $id_by.
  • 1 param β€” it looks up the ID param values of the passed-in object-parameter, and reassigns the value stored in the $id_by properties of the acted-upon object, effectively acting as a mutator.
  • more than 1 param β€” the additional parameters are taken as properties/values to filter the returned objects on.

three moods, one accessor. it's giving versatility. ⭐

🧚 mk_indirect_ro_accessor

$classobj->mk_indirect_ro_accessor($class_name, $accessor_name, $via, $to, $where);

creates a read-only via accessor named $accessor_name. its value comes from calling the object accessor named $via, and then calling the method $to on that object. the optional $where listref gets used as additional filters when calling $via. indirect, but make it chic. ✨

πŸ’– mk_indirect_rw_accessor

$classobj->mk_indirect_rw_accessor($class_name, $accessor_name, $via, $to,
                                   $where, $singular_name);

creates a via accessor named $accessor_name that can actually change the property it points to with $to when it's called as a mutator. so it's got range. πŸ’…

now the details, no cap:

  • if the $to property on the remote object is an ID property of its class, it deletes the refered-to object and creates a new one with the appropriate properties. yeah. delete and rebuild. dramatic but effective.
  • otherwise, it just updates the $to property on the refered-to object.

🌟 mk_calculation_accessor

$classobj->mk_calculation_accessor($class_name, $accessor_name, $calculation_src,
                                   $calculate_from, $params, $is_constant, $column_name);

creates a calculated accessor called $accessor_name.

if the $is_constant flag is true, then the accessor runs the calculation once, caches the result, and returns that same result for all subsequent calls to the accessor. one and done, iconic behavior honestly. πŸ”₯

$calculation_src can be one of these three things:

  1. a coderef
  2. a string containing Perl code
  3. the name of a module under UR::Object::Type::AccessorWriter which has a method called "calculate"

and if $calculation_src is empty, then $accessor_name must be the name of an already-existing subroutine in the class's namespace. no building required, it just borrows. πŸŽ€

🐚 mk_dimension_delegate_accessors

🐚 mk_dimension_identifying_accessor

these two create accessors for dealing with dimension tables in OLAP-type schemas. that's genuinely all we're told, bestie β€” they need more documentation. not me exposing the docs like this. πŸ’€

πŸ’« mk_rw_class_accessor

$classobj->mk_rw_class_accessor($class_name, $accessor_name, $column_name, $is_transient, $variable_value);

creates a read-write accessor called $accessor_name that stores its value in a scalar captured by the accessor's closure.

and here's the iconic part: since the closure gets inserted into the class's namespace, all instances of the class share the same closure (and therefore the same scalar), so the property effectively acts as a class-wide property. one value to rule them all. πŸ‘‘

🌸 mk_ro_class_accessor

$classobj->mk_ro_class_accessor($class_name, $accessor_name, $column_name, $variable_value);

creates a read-only accessor called $accessor_name that retrieves its value from a scalar captured by the accessor's closure. that value is initialized to $variable_value. cute, locked, done.

⚠️ and if you call it as a mutator? it throws an exception through Carp::croak. same energy as its ro sibling. do not. πŸ™ˆ

πŸ” SEE ALSO πŸ”

  • UR::Object::Type::AccessorWriter
  • UR::Object::Type

(yes, it sees itself. self-love, we support it. πŸ’œ)


and that's the whole page, bestie!! this one's from perl v5.32.1, dated 2022-01-17 β€” so go forth, build those accessors, and remember: the closures share the scalar, the ro accessors will croak at you, and $calculation_src can be a coderef, a string of Perl, or a module name with a "calculate" method. mwah πŸ’‹ 🫢 ✨

πŸ“¦ where this page comes from