New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mouse_organ

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mouse_organ

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Mouse Organ

Ladies and gentlemen, the Marvelous -- Mechanical -- Mouse Organ!

This is a very small mixin that turns a class into a basic state machine.

Installation

Just download lib/mouse_organ.rb into your code. Or you could just use it as a pattern of sorts, and roll your own in the same vein.

I mean, sure, you can also gem install mouse_organ. But honestly, it might be better to have the class where you can see it, for documentation purposes? Your call.

Example

class Foo
  include MouseOrgan

  def initialize
    machine_start(:one)
  end

  def state_one
    transition_to :two
    sleep 1
  end

  def state_two
    transition_to :three
    sleep 1
  end

  def state_three
    throw :machine_stop
  end

  def transition_to(state)
    puts state
    super
  end

end

What You Get

Each of your methods that start state_ define a state of the state machine. To start the machine, pass #machine_start the initial state name (e.g., :two for the state defined by #state_two).

Inside a state method, you can change to a new state using transition_to(:statename). You can stop the machine using throw :machine_stop, or you can override #machine_stop? to return true if the machine should stop.

Passing an invalid state to #machine_start or #transition_to raises ArgumentError.

The attribute @state holds the current state.

It might be useful to override #transition_to as I have above -- for example, in order to log every change of state.

FAQs

Package last updated on 31 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc