{"id":23445961,"url":"https://github.com/norbertmaleckii/simple-enumeration-rb","last_synced_at":"2025-04-13T15:11:47.216Z","repository":{"id":38274064,"uuid":"421739690","full_name":"norbertmaleckii/simple-enumeration-rb","owner":"norbertmaleckii","description":"Helps you to declare enumerations in a very simple and flexible way. Define your enumerations in classes, it means you can add new behaviour and also reuse them.","archived":false,"fork":false,"pushed_at":"2025-01-23T02:49:09.000Z","size":91,"stargazers_count":8,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T06:05:38.442Z","etag":null,"topics":["rails","rails-enumerations","ruby","ruby-enumerations"],"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/norbertmaleckii.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-27T08:37:57.000Z","updated_at":"2024-10-28T12:35:38.000Z","dependencies_parsed_at":"2024-12-23T20:32:45.028Z","dependency_job_id":"ae1628fe-b706-41f4-87b0-03dc8b72983a","html_url":"https://github.com/norbertmaleckii/simple-enumeration-rb","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/norbertmaleckii%2Fsimple-enumeration-rb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/norbertmaleckii%2Fsimple-enumeration-rb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/norbertmaleckii%2Fsimple-enumeration-rb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/norbertmaleckii%2Fsimple-enumeration-rb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/norbertmaleckii","download_url":"https://codeload.github.com/norbertmaleckii/simple-enumeration-rb/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248732489,"owners_count":21152852,"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":["rails","rails-enumerations","ruby","ruby-enumerations"],"created_at":"2024-12-23T20:28:41.269Z","updated_at":"2025-04-13T15:11:47.190Z","avatar_url":"https://github.com/norbertmaleckii.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SimpleEnumeration\n\nEnumerations system for Ruby with awesome features!\n\nHelps you to declare enumerations in a very simple and flexible way. Define your enumerations in classes, it means you can add new behaviour and also reuse them.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'simple_enumeration'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install simple_enumeration\n\n## Usage\n\n### Creating enumerations\n\nEnumerations are defined as plain Ruby classes. For rails usage you can put definitions inside `app/enumerations` folder.\n\nWhen we want define basic enumerations collection, we use `define_basic_collection` method like that:\n\n```ruby\nclass PaymentStatusEnumeration \u003c SimpleEnumeration::Entity\n  define_basic_collection(\n    :unpaid,\n    :failed,\n    :expired,\n    completed: :paid\n  )\nend\n```\n\nAfter that we have new class and instance methods at our disposal:\n\n* All enumeration's types have basic collection class methods:\n\n  ```ruby\n  PaymentStatusEnumeration.basic_collection\n  #=\u003e #\u003cSimpleEnumeration::Collection:0x000000010d73ec78 @name=:basic,\n  # @types={\n  #  \"unpaid\"=\u003e#\u003cSimpleEnumeration::Type:0x000000010d733210 @converted_value=\"unpaid\", @definition=:unpaid, @enum_class=PaymentStatusEnumeration, @value=\"unpaid\"\u003e,\n  #  \"failed\"=\u003e#\u003cSimpleEnumeration::Type:0x000000010d730358 @converted_value=\"failed\", @definition=:failed, @enum_class=PaymentStatusEnumeration, @value=\"failed\"\u003e,\n  #  \"expired\"=\u003e#\u003cSimpleEnumeration::Type:0x000000010d726880 @converted_value=\"expired\", @definition=:expired, @enum_class=PaymentStatusEnumeration, @value=\"expired\"\u003e,\n  #  \"completed\"=\u003e#\u003cSimpleEnumeration::Type:0x000000010d725570 @converted_value=\"paid\", @definition={:completed=\u003e:paid}, @enum_class=PaymentStatusEnumeration, @value=\"completed\"\u003e\n  #  }\u003e\n\n  PaymentStatusEnumeration.basic_collection_values\n  #=\u003e [\"unpaid\", \"failed\", \"expired\", \"paid\"]\n\n  PaymentStatusEnumeration.basic_collection_value?('completed')\n  #=\u003e false\n\n  PaymentStatusEnumeration.basic_collection_value?('paid')\n  #=\u003e true\n\n  PaymentStatusEnumeration.basic_collection_for_select\n  #=\u003e [[\"Nieopłacone\", \"unpaid\"], [\"Zakończone niepowodzeniem\", \"failed\"], [\"Wygasłe\", \"expired\"], [\"Zapłacone\", \"paid\"]]\n\n  PaymentStatusEnumeration.basic_collection_humanized\n  #=\u003e [\"Nieopłacone\", \"Zakończone niepowodzeniem\", \"Wygasłe\", \"Zapłacone\"]\n  ```\n\n* Each enumeration's type has own class method:\n\n  ```ruby\n  PaymentStatusEnumeration.unpaid\n  #=\u003e #\u003cSimpleEnumeration::Type:0x000000010d733210 @converted_value=\"unpaid\", @definition=:unpaid, @enum_class=PaymentStatusEnumeration, @value=\"unpaid\"\u003e\n\n  PaymentStatusEnumeration.failed\n  #=\u003e #\u003cSimpleEnumeration::Type:0x000000010d730358 @converted_value=\"failed\", @definition=:failed, @enum_class=PaymentStatusEnumeration, @value=\"failed\"\u003e\n\n  PaymentStatusEnumeration.expired\n  #=\u003e #\u003cSimpleEnumeration::Type:0x000000010d726880 @converted_value=\"expired\", @definition=:expired, @enum_class=PaymentStatusEnumeration, @value=\"expired\"\u003e\n\n  PaymentStatusEnumeration.completed\n  #=\u003e #\u003cSimpleEnumeration::Type:0x000000010d725570 @converted_value=\"paid\", @definition={:completed=\u003e:paid}, @enum_class=PaymentStatusEnumeration, @value=\"completed\"\u003e\n\n  PaymentStatusEnumeration.completed.definition\n  # =\u003e :completed\n\n  PaymentStatusEnumeration.completed.value\n  #=\u003e \"completed\"\n\n  PaymentStatusEnumeration.completed.converted_value\n  #=\u003e \"paid\"\n\n  PaymentStatusEnumeration.completed.for_select\n  #=\u003e [\"Zapłacone\", \"paid\"]\n\n  PaymentStatusEnumeration.completed.humanized\n  #=\u003e \"Zapłacone\"\n\n  PaymentStatusEnumeration.completed.meta\n  #=\u003e {:color=\u003e\"green\"}\n\n  PaymentStatusEnumeration.completed.translations\n  #=\u003e {:text=\u003e\"Zapłacone\", :color=\u003e\"green\"\n  ```\n\nWhen we want define custom enumerations collection, we use `define_custom_collection` method:\n\n```ruby\nclass PaymentStatusEnumeration \u003c SimpleEnumeration::Entity\n  define_basic_collection(\n    :unpaid,\n    :failed,\n    :expired,\n    completed: :paid\n  )\n\n  define_custom_collection(\n    :finished,\n    failed,\n    expired,\n    completed\n  )\nend\n```\n\nAfter that we have all methods described above and new ones at our disposal:\n\n```ruby\n  PaymentStatusEnumeration.finished_collection\n  #=\u003e #\u003cSimpleEnumeration::Collection:0x0000000109873bd8 @name=:finished,\n  # @types={\n  #  \"failed\"=\u003e#\u003cSimpleEnumeration::Type:0x000000010d730358 @converted_value=\"failed\", @definition=:failed, @enum_class=PaymentStatusEnumeration, @value=\"failed\"\u003e,\n  #  \"expired\"=\u003e#\u003cSimpleEnumeration::Type:0x000000010d726880 @converted_value=\"expired\", @definition=:expired, @enum_class=PaymentStatusEnumeration, @value=\"expired\"\u003e,\n  #  \"completed\"=\u003e#\u003cSimpleEnumeration::Type:0x000000010d725570 @converted_value=\"paid\", @definition={:completed=\u003e:paid}, @enum_class=PaymentStatusEnumeration, @value=\"completed\"\u003e\n  #  }\u003e\n\n  PaymentStatusEnumeration.finished_collection_values\n  #=\u003e [\"failed\", \"expired\", \"paid\"]\n\n  PaymentStatusEnumeration.finished_collection_value?('completed')\n  #=\u003e false\n\n  PaymentStatusEnumeration.finished_collection_value?('paid')\n  #=\u003e true\n\n  PaymentStatusEnumeration.finished_collection_value?('unpaid')\n  #=\u003e false\n\n  PaymentStatusEnumeration.finished_collection_for_select\n  #=\u003e [[\"Zakończone niepowodzeniem\", \"failed\"], [\"Wygasłe\", \"expired\"], [\"Zapłacone\", \"paid\"]]\n\n  PaymentStatusEnumeration.finished_collection_humanized\n  #=\u003e [\"Zakończone niepowodzeniem\", \"Wygasłe\", \"Zapłacone\"]\n  ```\n\n\n### Model definition\n\nInitially, there is no special dsl extension, but we can simply do that:\n\n```ruby\n# ActiveRecord class\n#\nclass Order \u003c ApplicationRecord\n  extend SimpleEnumeration::ObjectHelper\n\n  define_simple_enumeration :payment_status\nend\n\n# Plain Ruby class\n#\nclass Order\n  extend SimpleEnumeration::ObjectHelper\n\n  define_simple_enumeration :payment_status\n\n  attr_accessor :payment_status\n\n  def initialize(payment_status:)\n    @payment_status = payment_status\n  end\nend\n```\n\nThis will allow us to use enumeration type methods:\n\n```ruby\norder = Order.new(payment_status: 'paid')\n\norder.payment_status_enumeration.type.humanized\n#=\u003e \"Zapłacone\"\norder.payment_status_enumeration.type.meta\n#=\u003e {:color=\u003e\"green\"}\norder.payment_status_enumeration.type.translations\n#=\u003e {:text=\u003e\"Zapłacone\", :color=\u003e\"green\"}\norder.payment_status_enumeration.type.for_select\n#=\u003e [\"Zapłacone\", \"paid\"]\n\norder.payment_status_enumeration.unpaid_value?\n#=\u003e false\norder.payment_status_enumeration.failed_value?\n#=\u003e false\norder.payment_status_enumeration.expired_value?\n#=\u003e false\norder.payment_status_enumeration.completed_value?\n#=\u003e true\norder.payment_status_enumeration.basic_collection_value?\n#=\u003e true\norder.payment_status_enumeration.finished_collection_value?\n#=\u003e true\n```\n\n### Translations\n\nTranslations are defined using i18n gem.\n\n#### I18n lokup\n\nThe I18n strings are located on `simple_enumeration.\u003cenumeration_name\u003e.\u003cvalue\u003e.text`:\n\n```yaml\npl:\n  simple_enumeration:\n    payment_status:\n      unpaid:\n        text: \"Nieopłacone\"\n        color: yellow\n```\n\n#### Translate a name-spaced enumeration\n\nIn order to translate an enumeration in a specific namespace (say `Payments::StatusEnumeration`),\nyou can add the following:\n\n```yaml\npl:\n  simple_enumeration:\n    payments/status:\n      unpaid:\n        text: \"Nieopłacone\"\n        color: yellow\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` 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 the created tag, 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 https://github.com/norbertmaleckii/simple-enumeration-rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/norbertmaleckii/simple-enumeration-rb/blob/main/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Enumeration project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/norbertmaleckii/simple-enumeration-rb/blob/main/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnorbertmaleckii%2Fsimple-enumeration-rb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnorbertmaleckii%2Fsimple-enumeration-rb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnorbertmaleckii%2Fsimple-enumeration-rb/lists"}