{"id":19137956,"url":"https://github.com/nulldef/statum","last_synced_at":"2025-05-06T20:41:39.804Z","repository":{"id":59156423,"uuid":"112116661","full_name":"nulldef/statum","owner":"nulldef","description":"Ruby state machine ","archived":false,"fork":false,"pushed_at":"2017-12-10T16:09:36.000Z","size":34,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-16T13:15:05.115Z","etag":null,"topics":["declarative","fsm","ruby","state-machine"],"latest_commit_sha":null,"homepage":"http://www.rubydoc.info/github/nulldef/statum","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nulldef.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-26T21:06:12.000Z","updated_at":"2019-01-09T14:05:20.000Z","dependencies_parsed_at":"2022-09-13T18:13:22.314Z","dependency_job_id":null,"html_url":"https://github.com/nulldef/statum","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nulldef%2Fstatum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nulldef%2Fstatum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nulldef%2Fstatum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nulldef%2Fstatum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nulldef","download_url":"https://codeload.github.com/nulldef/statum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252768263,"owners_count":21801366,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["declarative","fsm","ruby","state-machine"],"created_at":"2024-11-09T06:41:14.013Z","updated_at":"2025-05-06T20:41:39.763Z","avatar_url":"https://github.com/nulldef.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Statum\n\n[![Build Status](https://travis-ci.org/nulldef/statum.svg?branch=feature%2Fhooks)](https://travis-ci.org/nulldef/statum)\n[![Coverage Status](https://coveralls.io/repos/github/nulldef/statum/badge.svg?branch=master)](https://coveralls.io/github/nulldef/statum?branch=master\u0026v=0.3.1)\n[![Gem Version](https://badge.fury.io/rb/statum.svg)](https://badge.fury.io/rb/statum)\n\n Finite state machine for your objects \n \nSupported Ruby 2.2.0+\n\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Basic usage](#basic-usage)\n  - [Multiple state machines](#multiple-state-machines)\n  - [Hooks](#hooks)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'statum'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install statum\n\n## Usage\n\n### Basic usage\nStatum provides a DSL for defining state machine for your class\n\n```ruby\nclass Car\n  include Statum\n  \n  attr_accessor :state\n  \n  statum :state, initial: :idle do\n    state :idle\n    state :riding\n    \n    event :ride, idle: :riding\n  end\nend\n```\n\nThen `Car` object will receive following methods:\n```ruby\ncar = Car.new\ncar.idle? # =\u003e true\ncar.riding? # =\u003e false\ncar.ride! # changes idle state to riding\n```\n\nYou can define an array of states which will be able to fire event:\n```ruby\nclass Car\n  include Statum\n  \n  attr_accessor :state\n  \n  statum :state, initial: :idle do\n    state :idle\n    state :parked\n    state :riding\n    \n    event :ride, %i[idle parked] =\u003e :riding\n  end\nend\n```\n\nAlso you can use `any_state` helper to say, that event can be fired from any of defined states\n```ruby\nclass Car\n  include Statum\n  \n  attr_accessor :state\n  \n  statum :state, initial: :idle do\n    state :idle\n    state :parked\n    state :riding\n    \n    event :ride, any_state =\u003e :riding\n  end\nend\n```\n\n### Multiple state machines\nYou can define more than one state machine on your object.\n\n**IMPORTANT** use unique fields to work with two or more states\n```ruby\nclass Car\n  include Statum\n\n  attr_accessor :state, :engine\n\n  statum :state do\n    state :riding\n    state :idle\n\n    event :ride, idle: :riding\n  end\n\n  statum :engine do\n    state :stopped\n    state :started\n\n    event :start, stopped: :started\n  end\nend\n```\n\n```ruby\ncar = Car.new\ncar.start! # changes engine to started\ncar.ride! # changes state to riding\n``` \n\n### Hooks\nYou can be able to execute some procs before and after event will be fired\n\n```ruby\nclass Car\n  include Statum\n  \n  attr_accessor :state, :started\n  \n  statum :state, initial: :idle do\n    state :idle\n    state :riding\n    \n    event :ride, idle: :riding,\n                 before: -\u003e { self.started = true },\n                 after: :stop_engine\n  end\n  \n  def stop_engine\n    self.started = false\n  end\nend\n```\n\nAnd then before state changes will be executed `before` proc, and after\nchanging - `after` proc (in instance context).\n\nIf you will wait for argument in hook - the instance will be passed.\n```ruby\n...\nevent :ride, idle: :riding, \n             before: -\u003e (instance) { instance.started = true}\n...\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/nulldef/statum.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnulldef%2Fstatum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnulldef%2Fstatum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnulldef%2Fstatum/lists"}