{"id":18656567,"url":"https://github.com/zendesk/method_struct","last_synced_at":"2025-04-11T18:30:54.497Z","repository":{"id":56883475,"uuid":"11423015","full_name":"zendesk/method_struct","owner":"zendesk","description":null,"archived":false,"fork":false,"pushed_at":"2023-04-09T19:13:11.000Z","size":54,"stargazers_count":34,"open_issues_count":1,"forks_count":5,"subscribers_count":87,"default_branch":"master","last_synced_at":"2025-03-25T16:51:35.499Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zendesk.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2013-07-15T12:51:34.000Z","updated_at":"2025-02-28T16:34:42.000Z","dependencies_parsed_at":"2023-07-14T18:50:22.844Z","dependency_job_id":null,"html_url":"https://github.com/zendesk/method_struct","commit_stats":null,"previous_names":["basecrm/method_struct"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fmethod_struct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fmethod_struct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fmethod_struct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fmethod_struct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zendesk","download_url":"https://codeload.github.com/zendesk/method_struct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248458362,"owners_count":21107063,"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":[],"created_at":"2024-11-07T07:24:04.483Z","updated_at":"2025-04-11T18:30:54.061Z","avatar_url":"https://github.com/zendesk.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MethodStruct\n\n[![Tests](https://github.com/zendesk/method_struct/workflows/Tests/badge.svg)](https://github.com/zendesk/method_struct/actions?query=workflow%3ATests+branch%3Amaster)\n\nFacilitates extracting large methods into objects - see Usage.\nFor a more in-depth treatment of the refactoring see\nhttp://sourcemaking.com/refactoring/replace-method-with-method-object. You can also take a look at http://www.bunsch.pl/2014/10/14/wrangling-service-objects-with-method_struct/ for an exmaple of usage in a Rails app.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'method_struct'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install method_struct\n\n## Usage\n\nSay you have this:\n\n```ruby\nclass UsersController\n  def create\n    User.create(:email =\u003e params[:email], :name =\u003e params[:name])\n    Mailer.registration_email(params[:email]).deliver\n  end\nend\n```\n\nYou can change it into this:\n\n```ruby\nclass Registrator \u003c MethodStruct.new(:email, :name)\n  def call\n    create_user!\n    send_email!\n  end\n\n  private\n  def create_user!\n    User.create(:email =\u003e email, :name =\u003e name)\n  end\n\n  def send_email!\n    Mailer.registration_email(:email).deliver\n  end\nend\n\nclass UsersController\n  def create\n    Registrator.call(params[:email], params[:name])\n    # Or\n    Registrator.call(:email =\u003e params[:email], :name =\u003e params[:name])\n    # Or - thanks to ruby changing [] into .call\n    Registrator[params[:email], params[:name]]\n  end\nend\n```\n\nYou can also specify a different method name like so:\n\n```ruby\nclass Registrator \u003c MethodStruct.new(:email, :name, :method_name =\u003e :register)\n  def register\n    # ...\n  end\nend\n\nclass UsersController\n  def create\n    Registrator.register(params[:email], params[:name])\n  end\nend\n```\n\nYou can use `:require_all =\u003e true` if you want to verify that all arguments\nhave been specified or `:require_presence =\u003e true` to verify that all arguments\nare non-nil. Global defaults for these options can be changed like so:\n\n```ruby\nMethodStruct::Defaults.set(\n  :require_presence =\u003e true,\n  :method_name =\u003e :do_it\n)\n```\n\nMethod struct currently also supports a `do` syntax, but it is discouraged (and may get depracated)\ndue to odd constant scoping. Example:\n\n```ruby\nRegistrator = MethodStruct.new(:email, :name) do\n  REGISTRATION_PATH = \"/register\" # this is now a top-level constant\n\n  def call\n    ...\n  end\nend\n```\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n\n## Releasing new version of gem\n\n1. Update version [lib/method_struct/version.rb](lib/method_struct/version.rb) and push to `master`\n2. Create new GitHub release with tag name starting with `v` and the version, for example `v0.3.0`\n3. Gem will be automatically built and pushed to rubygems.org with GitHub Action\n\n## Copyright and license\n\nCopyright 2013 Zendesk\n\nLicensed under the [Apache License, Version 2.0](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzendesk%2Fmethod_struct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzendesk%2Fmethod_struct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzendesk%2Fmethod_struct/lists"}