Giter Site home page Giter Site logo

accessorfacade's Introduction

AccessorFacade

A Raku method trait to turn indivdual get/set subroutines into a single read/write object attribute.

Build Status

Description

This module was initially designed to reduce the boiler plate code in a native library binding that became something like:

    class Shout is repr('CPointer') {

        sub shout_set_host(Shout, Str) returns int32 is native('libshout') { * }
        sub shout_get_host(Shout) returns Str is native('libshout') { * }

        method host() is rw {
            Proxy.new(
                FETCH => sub ($) {
                    shout_get_host(self);
                },
                STORE   =>  sub ($, $host is copy ) {
                    explicitly-manage($host);
                    shout_set_host(self, $host);
                }
            );
        }

        ...

    }

That is the library API provides a sort of "object oriented" mechanism to set and get attributes on an opaque object instance that was returned by another "constructor" function. Because the object is an opaque CPointer it can only have subroutines and methods and not private data or attributes. The intent of the code is to provide fake "attributes" with rw methods (which is similar to how public rw attributes are provided.)

The above code will be reduced with the use of AccessorFacade to:

    class Shout is repr('CPointer') {

        sub shout_set_host(Shout, Str) returns int32 is native('libshout') { * } 
        sub shout_get_host(Shout) returns Str is native('libshout') { * }

        method host() is rw is accessor-facade(&shout_get_host, &shout_set_host) { }

        ...
    }

The named argument style is also supported so the method could be written as:

    method host() is rw is accessor-facade(getter => &shout_get_host, setter => &shout_set_host) { }

if that proves more suitable.

(The call to explicitly manage is omitted for simplicity but how this is achieved is described in the documentation.) Libshout has a significant number of these get/set pairs so there is a reduction of typing, copy and paste and hopefully programmer error.

Whilst this was designed primarily to work with a fixed native API, it is possible that it could be used to provide an OO facade to a plain Raku procedural library. The only requirement that there is a getter subroutine that accepts an object as its first argument and returns the attribute value and a setter subroutine that accepts the object and the value to be set (it may return a value to indicate success - how this is handled is descibed in the documentation.)

Installation

Assuming you have a working Rakudo installation you should be able to install this with zef :

# From the source directory

zef install .

# Remote installation

zef install AccessorFacade

Support

Suggestions/patches are welcomed via github at:

https://github.com/jonathanstowe/AccessorFacade/issues

Licence

This is free software.

Please see the LICENCE file in the distribution

© Jonathan Stowe 2015 - 2021

accessorfacade's People

Contributors

jonathanstowe avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

samcv melezhik

accessorfacade's Issues

aAfter and before as named arguments

Just a idea: couldn't before and after be named arguments? I mean adverbs...
I think that is accessor-facade(&bla, &ble, :&after) or something like that would be great!

Great module, thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.