Giter VIP home page Giter VIP logo

boco-factory's Introduction

boco-factory

Classes and utilities for working with object factories.

BocoFactory = require 'boco-factory'
assert = require 'assert'

TypeFactory class

Provides a basic implementation of the [factory pattern] where objects are constructed based off of their type.

TypeFactory = BocoFactory.TypeFactory
factory = new TypeFactory()

Registering constructors

Each factory should encapsulate the constructors for concrete classes it maintains. Let's create a couple simple classes:

class User
  constructor: (properties = {}) ->
    @id = properties.id
    @name = properties.name

class Post
  constructor: (properties = {}) ->
    @id = properties.id
    @title = properties.title

To register them with our factory, we call the register method with an collection of key/value pairs. The key and value represent the name for our constructor and the constructor function itself, respectively.

factory.register User: User, Post: Post

Constructing objects

Now that we have registered some constructors, we can use our factory to create instances of those classes.

user = factory.construct 'User', name: "john.doe"
assert user instanceof User
assert.equal "john.doe", user.name

Unregistered constructors

ConstructorUndefined = BocoFactory.Errors.ConstructorUndefined

If you attempt to construct an object using an unregistered name, a ConstructorUndefined error will be thrown.

shouldThrow = ->
  factory.construct 'Foo'

isCorrectError = (error) ->
  error instanceof ConstructorUndefined

assert.throws shouldThrow, isCorrectError

Custom factories

You may want to extend the Factory class to perform custom construction of your objects. Let's create a new Factory class that generates an identity for each object it constructs.

The decorate method is called after the object is constructed. By default, it does nothing. We can override it to set the identity on objects we construct.

class IdentifyingFactory extends TypeFactory

  generateId: ->
    @lastId = if @lastId? then @lastId + 1 else 1

  decorate: (object) ->
    object.id = @generateId() unless object.id?
    return object

Now, let's create an instance of this factory class and register our constructors.

factory = new IdentifyingFactory()
factory.register Post: Post

You can define a single constructor by passing in the typeName and constructor:

factory.define "User", User

If we construct a User and Post, they should now have an id assigned by our factory.

post = factory.construct 'Post', title: "My first post"
user = factory.construct 'User', name: "john.doe"

assert.equal 1, post.id
assert.equal 2, user.id

boco-factory's People

Contributors

christianbradley avatar

Watchers

 avatar Bruce Hubbard avatar James Cloos avatar  avatar  avatar Stephen Hawk avatar

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.