{"id":15591249,"url":"https://github.com/hopsoft/hero","last_synced_at":"2025-07-15T16:12:02.324Z","repository":{"id":4351810,"uuid":"5487905","full_name":"hopsoft/hero","owner":"hopsoft","description":"Business process modeling for the Rubyist","archived":false,"fork":false,"pushed_at":"2017-08-16T22:46:47.000Z","size":114,"stargazers_count":39,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-20T00:41:31.200Z","etag":null,"topics":["business-process","modularization","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":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hopsoft.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":"2012-08-20T23:01:11.000Z","updated_at":"2022-03-05T09:17:13.000Z","dependencies_parsed_at":"2022-08-20T11:30:45.034Z","dependency_job_id":null,"html_url":"https://github.com/hopsoft/hero","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/hopsoft/hero","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hopsoft%2Fhero","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hopsoft%2Fhero/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hopsoft%2Fhero/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hopsoft%2Fhero/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hopsoft","download_url":"https://codeload.github.com/hopsoft/hero/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hopsoft%2Fhero/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265444938,"owners_count":23766438,"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":["business-process","modularization","ruby"],"created_at":"2024-10-02T23:40:42.474Z","updated_at":"2025-07-15T16:12:02.281Z","avatar_url":"https://github.com/hopsoft.png","language":"Ruby","readme":"[![Lines of Code](http://img.shields.io/badge/lines_of_code-126-brightgreen.svg?style=flat)](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)\n[![Code Status](http://img.shields.io/codeclimate/github/hopsoft/hero.svg?style=flat)](https://codeclimate.com/github/hopsoft/hero)\n[![Dependency Status](http://img.shields.io/gemnasium/hopsoft/hero.svg?style=flat)](https://gemnasium.com/hopsoft/hero)\n[![Build Status](http://img.shields.io/travis/hopsoft/hero.svg?style=flat)](https://travis-ci.org/hopsoft/hero)\n[![Coverage Status](https://img.shields.io/coveralls/hopsoft/hero.svg?style=flat)](https://coveralls.io/r/hopsoft/hero?branch=master)\n[![Downloads](http://img.shields.io/gem/dt/hero.svg?style=flat)](http://rubygems.org/gems/hero)\n\n\u003ca rel=\"nofollow\" href=\"https://app.codesponsor.io/link/QMSjMHrtPhvfmCnk5Hbikhhr/hopsoft/hero\"\u003e\u003cimg src=\"https://app.codesponsor.io/embed/QMSjMHrtPhvfmCnk5Hbikhhr/hopsoft/hero.svg\" style=\"width: 888px; height: 68px;\" alt=\"Sponsor\" /\u003e\u003c/a\u003e\n\n# Hero\n\n\u003e Controlling complexity is the essence of computer programming. -- [Brian Kernighan](http://en.wikipedia.org/wiki/Brian_Kernighan)\n\n### Create well organized modular apps.\n\nI've seen my share of poor app structure.\nHell, I wrote most of it.\nWhether is fat controllers, giant models with mystery callbacks, or a junk drawer lib directory.\n\nThe question remains. **Where do I put my business logic?**\n\n## Why Hero?\n\n* It matches the mental map of your business requirements\n* It produces testable components\n* It easily handles changing requirements\n* It reduces the ramp up time for new team members\n\nThink of Hero as a simplified state machine.\n\n## Match the Implementation to the Business Process\n\nThe problem has always been: **How do you effectively model a business process within your app?**\n\nThings start simply enough but eventually edge cases force *gotchas* into\nvarious libs, modules, and classes. Before you know you it,\nyou have a lump of spaghetti that's difficult to maintain and even harder to improve.\n\nHero provides a simple pattern that encourages you to\n\u003ca href=\"http://en.wikipedia.org/wiki/Decomposition_(computer_science)\"\u003edecompose\u003c/a\u003e\nthese processes into managable chunks.\n\n---\n\n## Quick Start\n\n```bash\ngem install hero\n```\n\nLets model a business process for collecting the top news stories from Hacker News, Reddit, \u0026 Google and then emailing the results to someone.\n\n---\n\nGather News\n\n- Get news from Hacker News\n- Get news from Reddit\n- Get news from Google\n- Email Results\n\n---\n\nNow that we have the basic requirements, lets model it with Hero.\n\n```ruby\nHero::Formula[:gather_news].add_step :hacker_news do |context, options|\n  # make api call\n  # parse results\n  # append results to context\nend\n\nHero::Formula[:gather_news].add_step :reddit do |context, options|\n  # make api call\n  # parse results\n  # append results to context\nend\n\nHero::Formula[:gather_news].add_step :google do |context, options|\n  # make api call\n  # parse results\n  # append results to context\nend\n\nHero::Formula[:gather_news].add_step :email do |context, options|\n  # format news for email\n  # compose the email\n  # send the email\nend\n```\n\nThis looks surprising similar to the requirements.\nIn fact we can publish the specification directly from Hero.\n\n```ruby\nHero::Formula[:gather_news].print\n\n# =\u003e gather_news\n#      1. hacker_news\n#      2. reddit\n#      3. google\n#      4. email\n```\n\nPretty slick.\nNow... lets run the process.\n\n```ruby\nnews = {}\nHero::Formula[:gather_news].run(news)\n```\n\nAnd we're done.\n\n### Key take aways\n\n- The implementation aligns perfectly with the requirements.\n  *Developers and business folks can talk the same lingo.*\n\n- The formula is composed of smaller steps that are interchangable.\n  *We are poised for changing requirements.*\n\n- Steps can be tested independently.\n\n- Each step implements the interface: `def call(context, options)`\n\n\n## Next Steps\n\nAs our app grows in complexity, we should change the steps from blocks to classes.\nHere's an example.\n\n```ruby\n# this\nHero::Formula[:gather_news].add_step :hacker_news do |context, options|\n  # make api call\n  # parse results\n  # append results to context\nend\n\n# changes to this\nmodule GatherNews\n  class HackerNews\n\n    def call(context, options)\n      # make api call\n      # parse results\n      # append results to context\n    end\n\n  end\nend\n\nHero::Formula[:gather_news].add_step GatherNews::HackerNews.new\n```\n\nWe should also create a directory structure that maps to the business process.\nSomething like this.\n\n```bash\n|-app\n  |-formulas\n    |-gather_news\n      |-hacker_news.rb\n      |-reddit.rb\n      |-google.rb\n      |-email.rb\n```\n\nWe also need an initializer to set the formula up.\n\n```ruby\n# app/initializer.rb\nHero::Formula[:gather_news].add_step GatherNews::HackerNews.new\nHero::Formula[:gather_news].add_step GatherNews::Reddit.new\nHero::Formula[:gather_news].add_step GatherNews::Google.new\nHero::Formula[:gather_news].add_step GatherNews::Email.new\n```\n\nNow we have a well structured application thats ready to grow.\nNotice how well organized everything is.\n\nAlso note that we can write tests for each step independent of anything else.\nThis is an important point and a powerful concept.\n\n## Deep Cuts\n\nLogging comes for free if you register a logger with Hero. Here's an example.\n\n```ruby\nHero.logger = Logger.new(STDOUT)\n\nHero::Formula[:log_example].add_step :first_step do |context, options|\n  context \u003c\u003c 1\nend\n\nHero::Formula[:log_example].add_step :second_step do |context, options|\n  context \u003c\u003c 2\nend\n\nHero::Formula[:log_example].add_step :third_step do |context, options|\n  context \u003c\u003c 3\nend\n\nHero::Formula[:log_example].run([])\n\n# The log will now contain the following lines.\n#\n# I, [2012-08-26T11:37:22.267072 #76676]  INFO -- : HERO before log_example -\u003e first_step Context: [] Options: {}\n# I, [2012-08-26T11:37:22.267166 #76676]  INFO -- : HERO after  log_example -\u003e first_step Context: [1] Options: {}\n# I, [2012-08-26T11:37:22.267211 #76676]  INFO -- : HERO before log_example -\u003e second_step Context: [1] Options: {}\n# I, [2012-08-26T11:37:22.267248 #76676]  INFO -- : HERO after  log_example -\u003e second_step Context: [1, 2] Options: {}\n# I, [2012-08-26T11:37:22.267282 #76676]  INFO -- : HERO before log_example -\u003e third_step Context: [1, 2] Options: {}\n# I, [2012-08-26T11:37:22.267333 #76676]  INFO -- : HERO after  log_example -\u003e third_step Context: [1, 2, 3] Options: {}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhopsoft%2Fhero","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhopsoft%2Fhero","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhopsoft%2Fhero/lists"}