# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_core.ipynb.

# %% auto 0
__all__ = ['achn']

# %% ../nbs/00_core.ipynb 6
import copy

# %% ../nbs/00_core.ipynb 8
from typing import Callable

# %% ../nbs/00_core.ipynb 10
#| export


# %% ../nbs/00_core.ipynb 12
from nlit import INPLACE
from idfunc import idfunc

# %% ../nbs/00_core.ipynb 14
class achn:
    methods = {}
    @classmethod
    def able(cls, fn: Callable):
        cls.methods.update({fn.__name__: fn})
        return fn
    
    def __init__(self, obj):
        self.obj = obj
        self._chain = []
    
    def chain(self, method: str, hush: bool = False):
        func = self.methods.get(method, idfunc)
        if func is idfunc and not hush: raise AttributeError(f'{method} not registered')
        self._chain.append(func)
        return self
        
    def __getattr__(self, attr): return self.chain(attr)
    def __call__(self, *args, **kwargs): return self
    
    def call(self, *args, **kwargs):
        obj = self.obj if kwargs.get(INPLACE, True) else copy.deepcopy(self.obj)
        for f in self._chain: obj = f(obj, *args, **kwargs)
        return obj
