{"id":13484593,"url":"https://github.com/soveran/micromachine","last_synced_at":"2025-05-15T18:04:27.544Z","repository":{"id":516952,"uuid":"145003","full_name":"soveran/micromachine","owner":"soveran","description":"Minimal Finite State Machine","archived":false,"fork":false,"pushed_at":"2017-08-20T15:04:51.000Z","size":46,"stargazers_count":528,"open_issues_count":2,"forks_count":40,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-03-31T22:17:51.184Z","etag":null,"topics":["finite-state-machine","lesscode","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":"Unmaintained","scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soveran.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2009-03-07T04:21:33.000Z","updated_at":"2025-03-31T08:13:49.000Z","dependencies_parsed_at":"2022-08-03T03:15:44.388Z","dependency_job_id":null,"html_url":"https://github.com/soveran/micromachine","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soveran%2Fmicromachine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soveran%2Fmicromachine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soveran%2Fmicromachine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soveran%2Fmicromachine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soveran","download_url":"https://codeload.github.com/soveran/micromachine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247744332,"owners_count":20988783,"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":["finite-state-machine","lesscode","ruby"],"created_at":"2024-07-31T17:01:26.769Z","updated_at":"2025-04-07T23:07:26.625Z","avatar_url":"https://github.com/soveran.png","language":"Ruby","readme":"MicroMachine\n============\n\nMinimal Finite State Machine.\n\nDescription\n-----------\n\nThere are many finite state machine implementations for Ruby, and they\nall provide a nice DSL for declaring events, exceptions, callbacks,\nand all kinds of niceties in general.\n\nBut if all you want is a finite state machine, look no further: this\nhas less than 50 lines of code and provides everything a finite state\nmachine must have, and nothing more.\n\nUsage\n-----\n\n``` ruby\nrequire 'micromachine'\n\nmachine = MicroMachine.new(:new) # Initial state.\n\n# Define the possible transitions for each event.\nmachine.when(:confirm, :new =\u003e :confirmed)\nmachine.when(:ignore, :new =\u003e :ignored)\nmachine.when(:reset, :confirmed =\u003e :new, :ignored =\u003e :new)\n\nmachine.trigger(:confirm)  #=\u003e true\nmachine.state              #=\u003e :confirmed\n\nmachine.trigger(:ignore)   #=\u003e false\nmachine.state              #=\u003e :confirmed\n\nmachine.trigger(:reset)    #=\u003e true\nmachine.state              #=\u003e :new\n\nmachine.trigger(:ignore)   #=\u003e true\nmachine.state              #=\u003e :ignored\n```\n\nThe `when` helper is syntactic sugar for assigning to the\n`transitions_for` hash. This code is equivalent:\n\n``` ruby\nmachine.transitions_for[:confirm] = { :new =\u003e :confirmed }\nmachine.transitions_for[:ignore]  = { :new =\u003e :ignored }\nmachine.transitions_for[:reset]   = { :confirmed =\u003e :new, :ignored =\u003e :new }\n```\n\nYou can also ask if an event will trigger a change in state. Following\nthe example above:\n\n``` ruby\nmachine.state              #=\u003e :ignored\n\nmachine.trigger?(:ignore)  #=\u003e false\nmachine.trigger?(:reset)   #=\u003e true\n\n# And the state is preserved, because you were only asking.\nmachine.state              #=\u003e :ignored\n```\n\nIf you want to force an Exception when trying to trigger a event from a\nnon compatible state use the `trigger!` method:\n\n``` ruby\nmachine.trigger?(:ignore)  #=\u003e false\nmachine.trigger!(:ignore)  #=\u003e MicroMachine::InvalidState raised\n```\n\nIt can also have callbacks when entering some state:\n\n``` ruby\nmachine.on(:confirmed) do\n  puts \"Confirmed\"\nend\n```\n\nOr callbacks on any transition:\n\n``` ruby\nmachine.on(:any) do\n  puts \"Transitioned...\"\nend\n```\n\nNote that `:any` is a special key. Using it as a state when declaring\ntransitions will give you unexpected results.\n\nYou can also pass any data as the second argument for `trigger` and\n`trigger!` which will be passed to every callback as the second\nargument too:\n\n``` ruby\nmachine.on(:any) do |_status, payload|\n  puts payload.inspect\nend\n\nmachine.trigger(:cancel, from: :user)\n```\n\nFinally, you can list possible events or states:\n\n``` ruby\n# All possible events\nmachine.events #=\u003e [:confirm, :ignore, :reset]\n\n# All events triggerable from the current state\nmachine.triggerable_events #=\u003e [:confirm, :ignore]\n\n# All possible states\nmachine.states #=\u003e [:new, :confirmed, :ignored]\n```\n\nCheck the examples directory for more information.\n\nAdding MicroMachine to your models\n----------------------------------\n\nThe most popular pattern among Ruby libraries that tackle this problem\nis to extend the model and transform it into a finite state machine.\nInstead of working as a mixin, MicroMachine's implementation is by\ncomposition: you instantiate a finite state machine (or many!) inside\nyour model and you are in charge of querying and persisting the state.\nHere's an example of how to use it with an ActiveRecord model:\n\n``` ruby\nclass Event \u003c ActiveRecord::Base\n  before_save :persist_confirmation\n\n  def confirm!\n    confirmation.trigger(:confirm)\n  end\n\n  def cancel!\n    confirmation.trigger(:cancel)\n  end\n\n  def reset!\n    confirmation.trigger(:reset)\n  end\n\n  def confirmation\n    @confirmation ||= begin\n      fsm = MicroMachine.new(confirmation_state || \"pending\")\n\n      fsm.when(:confirm, \"pending\" =\u003e \"confirmed\")\n      fsm.when(:cancel, \"confirmed\" =\u003e \"cancelled\")\n      fsm.when(:reset, \"confirmed\" =\u003e \"pending\", \"cancelled\" =\u003e \"pending\")\n\n      fsm\n    end\n  end\n\nprivate\n\n  def persist_confirmation\n    self.confirmation_state = confirmation.state\n  end\nend\n```\n\nThis example asumes you have a `:confirmation_state` attribute in your\nmodel. This may look like a very verbose implementation, but you gain a\nlot in flexibility.\n\nAn alternative approach, using callbacks:\n\n``` ruby\nclass Event \u003c ActiveRecord::Base\n  def confirm!\n    confirmation.trigger(:confirm)\n  end\n\n  def cancel!\n    confirmation.trigger(:cancel)\n  end\n\n  def reset!\n    confirmation.trigger(:reset)\n  end\n\n  def confirmation\n    @confirmation ||= begin\n      fsm = MicroMachine.new(confirmation_state || \"pending\")\n\n      fsm.when(:confirm, \"pending\" =\u003e \"confirmed\")\n      fsm.when(:cancel, \"confirmed\" =\u003e \"cancelled\")\n      fsm.when(:reset, \"confirmed\" =\u003e \"pending\", \"cancelled\" =\u003e \"pending\")\n\n      fsm.on(:any) { self.confirmation_state = confirmation.state }\n\n      fsm\n    end\n  end\nend\n```\n\nNow, on any transition the `confirmation_state` attribute in the model\nwill be updated.\n\nInstallation\n------------\n\n    $ sudo gem install micromachine\n\nLicense\n-------\n\nCopyright (c) 2009 Michel Martens\n\nPermission is hereby granted, free of charge, to any person\nobtaining a copy of this software and associated documentation\nfiles (the \"Software\"), to deal in the Software without\nrestriction, including without limitation the rights to use,\ncopy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\nHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\nWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n","funding_links":[],"categories":["Libraries","Ruby","Gems","State Machines"],"sub_categories":["Ruby","State Machines"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoveran%2Fmicromachine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoveran%2Fmicromachine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoveran%2Fmicromachine/lists"}