{"id":19667255,"url":"https://github.com/willnet/yuba","last_synced_at":"2025-06-11T20:07:40.610Z","repository":{"id":22669744,"uuid":"96433454","full_name":"willnet/yuba","owner":"willnet","description":"Add new layers to rails for more tidy source code","archived":false,"fork":false,"pushed_at":"2025-05-31T09:40:00.000Z","size":215,"stargazers_count":42,"open_issues_count":4,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-06-11T04:57:48.850Z","etag":null,"topics":[],"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/willnet.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-06T13:30:15.000Z","updated_at":"2025-05-31T09:39:57.000Z","dependencies_parsed_at":"2022-07-27T03:17:25.032Z","dependency_job_id":null,"html_url":"https://github.com/willnet/yuba","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/willnet/yuba","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnet%2Fyuba","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnet%2Fyuba/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnet%2Fyuba/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnet%2Fyuba/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willnet","download_url":"https://codeload.github.com/willnet/yuba/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnet%2Fyuba/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259330407,"owners_count":22841629,"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-11T16:31:23.582Z","updated_at":"2025-06-11T20:07:40.603Z","avatar_url":"https://github.com/willnet.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Yuba\n\n[![Build Status](https://travis-ci.org/willnet/yuba.svg?branch=master)](https://travis-ci.org/willnet/yuba)\n[![Gem Version](https://badge.fury.io/rb/yuba.svg)](https://badge.fury.io/rb/yuba)\n\n## warning\n\nThe version of this gem is now 0.0.x. It works, but there must be occasional breaking changes to the API.\n\n## Summary\n\nYuba adds new layers to rails.\n\n- Service\n- Form\n- ViewModel\n\nIt is convenient to use them in combination, but you can use them even individually.\nIf you have difficulties with a large rails application, Yuba helps you.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'yuba'\n```\n\nAnd then execute:\n\n```bash\n$ bundle install\n```\n\n## Support\n\n- Rails 7.1+\n- Ruby 3.2+\n\n## ViewModel\n\nViewModel is useful when there are many instance variables in controllers.\n\n```ruby\nclass PostViewModel \u003c Yuba::ViewModel\n  property :post\n  property :author, public: true\n  property :other, optional: true\n\n  def title\n    post.title\n  end\n\n  def body\n    post.body\n  end\nend\n\nPost = Struct.new(:title, :body)\npost = Post.new('hello', 'world')\n\nview_model = PostViewModel.new(post: post, author: 'willnet')\nview.title #=\u003e 'hello'\nview.body #=\u003e 'world'\nview.author #=\u003e 'willnet'\nview.post #=\u003e NoMethodError\n```\n\n### property\n\n`.property` method registers property to the class.\n\nWe need to pass those properties as arguments to the `initialize` except when `optional: true` is attached. You get ArgumentError if you don't pass `property` to `initialize`.\n\nProperty is default to private. It means you can use it in the internal instance. If you want to use it as public, use `public: true` option.\n\n### Auto Assign\n\nYou can use ViewModel in a controller like the following.\n\n```ruby\nclass PostsController \u003c ApplicationController\n  def show\n    @view_model = PostViewModel.new(post: post, author: 'willnet')\n  end\nend\n```\n\nIn view template, if you want to access post and author, you have to use `@view_model` instance variable like `@view_model.post.title`. if it feels troublesome, you can write like following\n\n```ruby\nclass PostsController \u003c ApplicationController\n  def show\n    view_model = PostViewModel.new(post: post, author: 'willnet')\n    render view_model: view_model\n  end\nend\n```\n\nview_model option of render takes ViewModel, which gets its public methods (include public property) and assigns them to instance variables in the view template. So you can write `\u003c%= @post.title %\u003e` instead of `\u003c%= @view_model.post.title %\u003e`\n\n## Service\n\nService is valuable when a controller has many application logic.\n\n```ruby\nclass PostController \u003c ApplicationController\n  def new\n    @post = CreatePostService.call(user: current_user).post\n  end\n\n  def create\n    service = CreatePostService.call(user: current_user, params: params)\n\n    if service.success?\n      redirect_to root_path\n    else\n      @post = service.post\n      render :new\n    end\n  end\nend\n\nclass CreatePostService \u003c Yuba::Service\n  property :user, public: true\n  property :params, optional: true\n\n  def call\n    if post.save\n      notify_to_admin\n    else\n      fail!\n    end\n  end\n\n  def post\n    user.posts.build(post_params)\n  end\n\n  private\n\n  def notify_to_admin\n    AdminMailer.notify_create_post(post).deliver_later\n  end\n\n  def post_params\n    params.require(:post).permit(:title, :body)\n  end\nend\n```\n\n- `.property` method registers property to the class like ViewModel.\n- `.call` invokes `#call` after assigning arguments as properties.\n- `#success?` returns `true` if you don't invoke `#fail!`\n\nYou have inspection methods for properties.\n\n```ruby\nservice = CreatePostService.new(user: someuser)\nservice.has_property?(:user) #=\u003e true\nservice.has_value?(:user) #=\u003e true\nservice.has_public_property?(:user) #=\u003e true\nservice.has_private_property?(:user) #=\u003e false\nservice.has_required_property?(:user) #=\u003e true\nservice.has_optional_property?(:user) #=\u003e false\n```\n\n## Form\n\nForm is just wrapper of [reform-rails](https://github.com/trailblazer/reform-rails) for now.\n\nYou can see documentation [here](http://trailblazer.to/gems/reform/rails.html).\n\n## Combination Sample\n\n```ruby\nclass ArtistsController \u003c ApplicationController\n  def new\n    @view_model = Artist::CreateService.new(params: params).view_model\n  end\n\n  def create\n    service = Artist::CreateService.call(params: params)\n\n    if service.success?\n      redirect_to artists_path\n    else\n      @view_model = service.view_model\n      render :new\n    end\n  end\nend\n```\n\n```ruby\nclass Artist::CreateService \u003c Yuba::Service\n  property :params\n\n  def call\n    if form.validate(params)\n      form.save\n    else\n      fail!\n    end\n  end\n\n  def view_model\n    Artist::CreateViewModel.new(form: form)\n  end\n\n  private\n\n  def form\n    @form ||= ArtistForm.new(Artist.new)\n  end\nend\n```\n\n```ruby\nclass ArtistForm \u003c Yuba::Form\n  property :name\n\n  validates :name, presence: true, length: { maximum: 100 }\nend\n```\n\n```ruby\nclass Artist::CreateViewModel \u003c Yuba::ViewModel\n  property :form, public: true\nend\n```\n\n## generators\n\nYou can use generators.\n\n```\nrails generate yuba:service create_artist\nrails generate yuba:form artist\nrails generate yuba:view_model artist_index\n```\n\n## Contributing\n\nYou can try to test by doing as following.\n\n```\ngit clone https://github.com/willnet/yuba.git\ncd yuba\nbundle\nbundle exec rake\n```\n\n## License\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillnet%2Fyuba","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillnet%2Fyuba","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillnet%2Fyuba/lists"}