Code Monkey home page Code Monkey logo

big_machine's Introduction

Big machine Build Status Dependency Status Code Climate

Big machine is a Gem which give state to your object

Resources

Bugs

Development

Testing

Source

  • git://github.com/alaibe/big_machine.git

Install

Add this to your Gemfile

  gem 'big_machine'

Usage

Create your first state

  class Draft < BigMachine::State
    def publish
      transition_to Online
    end
  end

  class Online < BigMachine::State
  end

All methods you override in you state can takes args and block

  class Draft < BigMachine::State
    def publish(now = Time.now)
      transition_to Online, now
    end
  end

  class Online < BigMachine::State
    def enter(*args)
      stateful.publish_at args.first
    end
  end

Make your object stateful

  class Car
    include BigMachine

    big_machine initial_state: :draft
  end

Now it's possible to publish your car object:

  car = Car.new
  car.current_state # => Draft
  car.publish
  car.current_state # => Online

Of course your object can be an ActiveRecord::Base object. In this case, the object must have a state column. ( if not, see the next section )

Big machine options

big_machine method can take several options:

  • initial_state is the only one necessary option
  • state_attribute is available only if the object is an active record object
  • workflow if you want to change the normal worklow It's possible to call workflow_is method in your different states

Example

  big_machine initial_state: :dradt, state_attribute: :big_state, workflow: small

  class Draft < BigMachine::State

    def publish
      return if workflow_is :small

      transition_to Online
    end

  end

Lock module

A state can include lock module, it will lock the state of your object when your enter in it. The unlock method should be call to unlock the module

  class Draft < BigMachine::State
    include BigMachine::Lock

    def publish
      transition_to Online
    end

  end

  class Article
    include BigMachine

    big_machine initial_state: :dradt
  end

  article = Article.new
  article.publish
  article.current_state # => Draft
  article.unlock
  article.publish
  article.current_state # => Online

Contributors

*Anthony Laibe

Bitdeli Badge

big_machine's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  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.