{"id":15064622,"url":"https://github.com/tomasc/object_state","last_synced_at":"2026-02-18T00:35:30.309Z","repository":{"id":56886088,"uuid":"66702525","full_name":"tomasc/object_state","owner":"tomasc","description":"Encapsulates object's state, converts from/to params and to cache_key.","archived":false,"fork":false,"pushed_at":"2018-05-01T17:41:55.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-08T18:41:34.592Z","etag":null,"topics":["ruby-gem","ruby-on-rails"],"latest_commit_sha":null,"homepage":null,"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/tomasc.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":"2016-08-27T07:43:11.000Z","updated_at":"2018-05-01T17:41:56.000Z","dependencies_parsed_at":"2022-08-20T14:31:22.879Z","dependency_job_id":null,"html_url":"https://github.com/tomasc/object_state","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/tomasc/object_state","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Fobject_state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Fobject_state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Fobject_state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Fobject_state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomasc","download_url":"https://codeload.github.com/tomasc/object_state/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Fobject_state/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279011816,"owners_count":26085009,"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","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["ruby-gem","ruby-on-rails"],"created_at":"2024-09-25T00:22:44.589Z","updated_at":"2025-10-13T18:25:28.989Z","avatar_url":"https://github.com/tomasc.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Object State\n\n[![Build Status](https://travis-ci.org/tomasc/object_state.svg)](https://travis-ci.org/tomasc/object_state) [![Gem Version](https://badge.fury.io/rb/object_state.svg)](http://badge.fury.io/rb/object_state) [![Coverage Status](https://img.shields.io/coveralls/tomasc/object_state.svg)](https://coveralls.io/r/tomasc/object_state)\n\nThis gem helps to encapsulate state of objects of (typically) Ruby on Rails applications.\n\n* The state becomes easy to identify in the source code.\n* There is a standard method for converting the state to and from query params.\n* The state can be expressed in the form of a `cache_key`.\n* It is possible to additionally typecast and/or validate its attributes.\n\nUseful for pagination, filtering etc.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'object_state'\n```\n\nAnd then execute:\n\n```\n$ bundle\n```\n\nOr install it yourself as:\n\n```\n$ gem install object_state\n```\n\n## Usage\n\n### Encapsulate object's state\n\n```ruby\nclass MyObj\n  include ObjectState::Owner\n\n  object_state do\n    attr_accessor :current_date\n  end\nend\n```\n\n### Export state hash\n\n```ruby\nmy_obj.to_object_state_hash # =\u003e { my_obj =\u003e { id: \"123\", current_date: \"2016-08-27\" } }\n```\n\nThis hash can be easily used as query params, for example:\n\n```ruby\nmy_obj_path(my_obj, my_obj.to_object_state_hash)\n```\n\nAn attribute can be easily overridden:\n\n```ruby\nmy_obj_path(my_obj, my_obj.to_object_state_hash(current_date: Date.tomorow))\n```\n\n### Assign values from state hash\n\n```ruby\nmy_obj.assign_attributes_from_object_state_hash(…)\n```\n\nThe attributes will be assigned only if the id in the state hash matches the id of your object. This is helpful for example when used in controllers.\n\n## Supported attribute definitions\n\n* PoRo (`attr_accessor`)\n* Mongoid fields\n* Virtus attributes\n\n## Advanced usage\n\nOptionally the state can be processed by a custom class. This is useful when the values need to be typecast, validated, or transformed. The `ObjectState::State` includes `ActiveModel::Model` and `Virtus` so you can use [Virtus' attribute definition](https://github.com/solnic/virtus) and [ActiveModel validations](http://api.rubyonrails.org/classes/ActiveModel/Validations.html). For example:\n\n```ruby\nclass MyObj::State \u003c ObjectState::State\n  attribute :current_date, Date\n\n  validates :current_date, inclusion: { in: Date.today..Date.tomorrow }, presence: true\nend\n```\n\nOnly valid values will be assigned to your object.\n\n```ruby\nclass MyObj\n  include ObjectState::Owner\n\n  object_state class_name: 'MyObj::State' do\n    attr_accessor :current_date\n  end\nend\n```\n\n## Cache\n\nOften values or views associated with the object need to be cached (and the cache expired) depending on its state. The `:object_state_cache_key` generates a cache key based on the state's values. For example the `MyObj` from the above example:\n\n```ruby\nmy_obj.object_state_cache_key # =\u003e '2016-08-27'\n```\n\nIn fact the `object_state` method automatically merges the state object's cache key with the object's cache_key:\n\n```ruby\nmy_obj.cache_key # =\u003e '\u003cobject-cache-key\u003e/2016-08-27'\n```\n\nThis can be disabled as follows:\n\n```ruby\nclass MyObj\n  include ObjectState::Owner\n\n  object_state merge_cache_key: false do\n    attr_accessor :current_date\n  end\nend\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at \u003chttps://github.com/tomasc/object_state\u003e.\n\n## License\n\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%2Ftomasc%2Fobject_state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomasc%2Fobject_state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasc%2Fobject_state/lists"}