Hoc classes inflators

From openreachtech/hora-skills-ort-core · View interactive version

Hoc classes inflators is an AI agent skill from the GitHub repository openreachtech/hora-skills-ort-core. Convention for class inflator methods (binding methods): static methods that bind a value to a class and return a derived subclass. Use when writing, naming or reviewing an inflator, or when a class needs a variant configured by another class. Where inflators sit in the class body belongs to the cla

How to use this skill

Skill file

Classes: Inflators

This summarizes conventions related to inflator methods (binding methods).

What is an inflator method

export default class UnionScalar extends BaseScalar {
  // Inflator method: binds the passed schemas and returns a memoized derived subclass
  static of (...schemas) {
    const registry = BoundCtorRegistry.create({
      BaseCtor: this,
    })

    return registry.ensureBoundCtor({
      bindings: schemas,
      deriver: ({ Ctor }) => class extends Ctor {
        /** @override */
        static get boundSchemas () {
          return schemas
        }
      },
    })
  }

  // from delegates to of
  static from (schemas) {
    return this.of(...schemas)
  }

  // abstract member overridden by the inflator
  /** @abstract */
  static get boundSchemas () {
    throw new Error(`${this.name}.get:boundSchemas must be inherited`)
  }
}

Define the inflator as a static method on the target class being bound itself

// NG: hardcoding the class name in BaseCtor
static of (...schemas) {
  const registry = BoundCtorRegistry.create({
    BaseCtor: UnionScalar, // ❌️ anchors to UnionScalar even when called from a subclass
  })
  // ...
}

// OK: this for BaseCtor
static of (...schemas) {
  const registry = BoundCtorRegistry.create({
    BaseCtor: this,
  })
  // ...
}

Values bound must be objects

Memoization semantics

UnionScalar.of(A, B) === UnionScalar.of(A, B) // true (same binding → same class)
UnionScalar.of(A, B) === UnionScalar.of(A, C) // false (different binding → different class)
const Bound = OriginalCtor.use(FirstConstraint)

Bound.prototype instanceof OriginalCtor // true
Bound.name // 'OriginalCtor'

inflator's return value in a variable to cache it.

// OK: it is fine to inflate on every call (memoization prevents wasteful creation)
inflateMessageCtor () {
  return this.MessageCtor
    .use(this.MessageDeserializerCtor)
    .toKey(this.MessageKeyCtor)
    .toValue(this.MessageValueCtor)
}

Override abstract members in inflators

// OK: overriding an abstract static getter within deriver ([TargetClassName]Ctor naming)
deriver: ({ Ctor }) => class extends Ctor {
  /** @override */
  static get MessageKeyCtor () {
    return MessageKeyCtor
  }
}

Pass arguments flat

// NG: wrapping in a named-argument object
static use ({ ConstraintCtor }) { /* ... */ }

// OK: flat single argument
static use (ConstraintCtor) { /* ... */ }

// OK: variadic / array argument
static of (...Ctors) { /* ... */ }
static from (Ctors) {
  return this.of(...Ctors)
}

// OK (exception): binding of config/argument can take a named-argument object as its value
static by (argument) { /* argument: Record<string, *> */ }

Naming convention (short, preposition-like names)

Inflator methods are given short, preposition-like names so that Receiver.word(binding) reads declaratively. Each word expresses "the relationship between the receiver and the binding." The vocabulary is classified into three families.

Absorption family (forward direction, result is receiver family)

The receiver absorbs / becomes / uses the binding. BaseCtor: this, result is receiver family.

| Method | Semantics | Arguments | Example |

| :-- | :-- | :-- | :-- |

| .as() | The binding matches the identity of the whole class (becomes that thing itself) | Single | NodeScalar.as(Constraint) / ResponseBody.as(schema) |

| .use() | Binds an auxiliary used internally by the receiver (Deserializer / Constraint, etc.) — broad sense | Single | ConsumerMessage.use(JsonMessageDeserializer) |

| .for() | A narrow sense of .use() — the binding is the target/purpose of specialization (works "for" something) | Single | Logger.for(BaseResolverCtor) |

| .on() | A narrow sense of .use() — the binding is the target/trigger of action (works "on" something) | Single | Handler.on(ClickEvent) |

| .by() | Binds input-side config / argument (acts before the main behavior) | Single (object allowed) | Converter.by({ precision: 2 }) |

| .to() | Binds a Converter class (the counterpart of conversion) | Single | ConsumerMessage.to(Converter) |

| .of() | Binds multiple Ctors with an ordering (sequence) | Variadic | UnionScalar.of(A, B) |

| .from() | Syntactic sugar for .of() (accepts a single array and delegates to .of()) | Single array | UnionScalar.from([A, B]) |

| .via() | Applies the binding to part of the whole schema | Single | — |

| .each() | Applies one definition to multiple locations (each element) (a variant of .via()) | Single | RecordScalar.each(valueSchema) |

| .with() | Output-side — binds an accompanying object that manipulates the output (acts after the main behavior) | Single | HttpClient.with(RetryPolicy) |

Graft family (reverse direction, result is argument family)

With Alpha.word(Beta), the receiver (Alpha) is not inflated; instead, the receiver is grafted/injected onto the argument (Beta) side. The roles of BaseCtor and bindings are swapped, and the result becomes the argument family.

| Method | Semantics | Example |

| :-- | :-- | :-- |

| .onto() | Alpha.onto(Beta) — places Alpha onto Beta (graft). The result is Beta family | — |

| .into() | Alpha.into(Beta) — injects Alpha into Beta (inject / embed). The result is Beta family | — |

// Graft family: argument for BaseCtor, this for bindings (roles reversed)
static onto (TargetCtor) {
  const registry = BoundCtorRegistry.create({
    BaseCtor: TargetCtor, // ← inflate based on the argument side
  })

  const OwnCtor = this // ← the receiver itself becomes the binding

  return registry.ensureBoundCtor({
    bindings: [
      OwnCtor,
    ],
    deriver: ({ Ctor }) => class extends Ctor {
      /** @override */
      static get boundSource () {
        return OwnCtor
      }
    },
  })
}

How to choose a word

1. Is the result you want receiver family or argument family? If argument family (you want to place the receiver onto the argument), use the graft family (.onto() / .into()).

2. If forward direction, choose a word from the absorption family based on absorb/become/use for the binding.

Chaining

// OK: chaining use → toKey → toValue to obtain a derived class with three bindings bound together
const MessageCtor = ConsumerMessage
  .use(JsonMessageDeserializer)
  .toKey(SampleMessageKey)
  .toValue(SampleMessageValue)
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.