ProductPromotion
Logo

Ruby

made by https://0x3d.site

GitHub - svenfuchs/simple_states: A super-slim statemachine-like support library
A super-slim statemachine-like support library. Contribute to svenfuchs/simple_states development by creating an account on GitHub.
Visit Site

GitHub - svenfuchs/simple_states: A super-slim statemachine-like support library

GitHub - svenfuchs/simple_states: A super-slim statemachine-like support library

simple_states Build Status

A super-slim (~200 loc) statemachine-like support library focussed on use in Travis CI.

Note that the current version behaves slightly differently, and comes with reduced features compared to the original version. If you are looking for the original version see the tag v1.1.0.rc11.

Usage

Define states and events like this:

class Foo
  include SimpleStates

  event :start,  if: :start?
  event :finish, to: [:passed, :failed], after: :notify, unless: :finished?

  attr_accessor :state, :started_at, :finished_at

  def start
    # start foo
  end

  def start?
    true
  end

  def notify(event)
    # notify about event on foo
  end
end

SimpleStates expects your model to support attribute accessors for :state.

Event options have the following well-known meanings:

:to     # allowed target states to transition to, deferred from the event name if not given
:if     # only proceed if the given method returns true
:unless # only proceed if the given method returns false
:before # run the given method before running `super` and setting the new state
:after  # run the given method at the very end

All of these options except can be given as a single symbol or string or as an Array of symbols or strings.

Calling event will effectively add methods to a proxy module which is prepended to your class (included to the singleton class of your class' instances on 1.9). E.g. declaring event :start in the example above will add methods start and start! to a module included to the singleton class of instances of Foo.

This method will

  1. check if :if/:unless conditions apply (if given) and just return from the method otherwise
  2. run :before callbacks (if given)
  3. set the object's state to the target state
  4. set the object's [state]_at attribute to Time.now if the object defines a writer for it
  5. call super if Foo defines the current method (i.e. call start but not finish in the example above)
  6. run :after callbacks (if given)

You can define options for all events like so:

event :finish, after: :cleanup
event :all,    after: :notify

This will call :cleanup first and then :notify on :finish.

If no target state was given for an event then SimpleStates will try to derive it from the event name. I.e. for an event start it will check the states list for a state started and use it. If it can not find a target state this way then it will raise an exception.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory