Giter Club home page Giter Club logo

aquam's Introduction

aquam

Gem Version Build Status Code Climate Dependency Status

A Ruby DSL for writing Finite State Machines and validate its transitions'

Dependencies

aquam requires Ruby 2.1.x or later. No more dependencies.

Installation

$ gem install aquam

Getting started

aquam helps you to define Finite State Machines with a very simple DSL which also will validate events, states and the transition between them.

First of all, you must know that a State Machine should be a different object, where you specify the valid states and the transitions fired by the events.

That being said, lets take a look how it works.

Machine

Basically a Machine consists on

  • states
  • events
  • transitions

There are three key words in our DSL that will help you to write your own Finite State Machine, plus the attribute method.

Example

class DoorStateMachine < Aquam::Machine
  state :opened, OpenedDoorState
  state :closed, ClosedDoorState

  event :open do
    transition from: :closed, to: :opened
  end

  event :close do
    transition from: :opened, to: :closed
  end

  event :knock do
    transition from: :opened, to: :opened
    transition from: :closed, to: :closed
  end
end

NOTE: OpenedDoorState and ClosedDoorState definitions are missing but we will cover States definition later.

state

A state maps a symbol to a State class. It tells to the machine it is a valid state and which class represents it.

state :opened, OpenedDoorState

event

An event is a method which triggers the transition from one state to another. Each state object must define only the events that are specified here.

event :open do
  ...
end

transition

A transition moves the state machine from state A to state B. It can only be defined inside an event and you can define multiple transitions.

transition from: :a_valid_state, to: :other_valid_state

attribute

The attribute holds the name of the accessor in your own class where the state name (string or symbol) will be stored.

By default uses :state as method accessor.

attribute :state

Extra

Being a subclass of Aquam::Machine also gives you some helpful class methods:

Class Method Description Example (ruby)
states Hash Valid states mapped to a class { opened: OpenedDoorState }
events Hash Valid events with all its transitions { open: { closed: :opened } }
valid_state? Boolean Check if it is a valid state true
valid_event? Boolean Check if it is a valid event true

And for instance methods it defines:

Class Method Description Example (ruby)
current_state Aquam::State Instance of current state #<ClosedDoorState:0x007...>
trigger Aquam::State Instance of the new state #<ClosedDoorState:0x008...>
valid_state? Boolean Check if it is a valid state true
valid_event? Boolean Check if it is a valid event true
valid_transition? Boolean Check if it is a valid event true

State

For each state, we define a class that implements the corresponding events. Every bit of behavior that is state-dependent should become a method in the class. aquam uses metaprogramming to define methods for every single event listed in the state machine used.

Example

class OpenedDoorState < Aquam::State
  use_machine DoorStateMachine

  def close
    # Do something

    @object.state = :closed
  end
end

class ClosedDoorState < Aquam::State
  use_machine DoorStateMachine

  def open
    # Do something

    @object.state = :opened
  end
end

use_machine

This is the only method that you must call from every State class, in order to define the interface according to the state machine. Basically, it defines a method for every event defined in the state machine.

use_machine DoorStateMachine

NOTE: You can not change its value and it is accessible from all subclasses.

aquam's People

Watchers

Kazuki MATSUMOTO avatar James Cloos 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.