{"id":21203414,"url":"https://github.com/dux/class-cattr","last_synced_at":"2025-03-14T22:42:49.113Z","repository":{"id":56843532,"uuid":"340958201","full_name":"dux/class-cattr","owner":"dux","description":"class-cattr gem provides simple way to set and get class attributes in Ruby.","archived":false,"fork":false,"pushed_at":"2022-08-03T06:42:39.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-24T01:40:55.218Z","etag":null,"topics":["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/dux.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-21T17:16:50.000Z","updated_at":"2022-08-01T12:27:11.000Z","dependencies_parsed_at":"2022-09-07T02:23:44.805Z","dependency_job_id":null,"html_url":"https://github.com/dux/class-cattr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dux%2Fclass-cattr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dux%2Fclass-cattr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dux%2Fclass-cattr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dux%2Fclass-cattr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dux","download_url":"https://codeload.github.com/dux/class-cattr/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243658152,"owners_count":20326464,"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":["ruby"],"created_at":"2024-11-20T20:23:51.043Z","updated_at":"2025-03-14T22:42:47.860Z","avatar_url":"https://github.com/dux.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ruby class attributes\n\nclass-cattr gem provides simple way to set and get class attributes.\n\n## Installation\n\nto install\n\n`gem install class-cattr`\n\nor in Gemfile\n\n`gem 'class-cattr'`\n\nand to use\n\n`require 'class-cattr'`\n\n## How to use\n\n* require 'class-cattr'\n* define class attributes as `cattr :name, default: value, class: true|fase, instance: true|false`\n* get them via `cattr.name` (or via class and instance method if provided)\n\n```ruby\nclass AppModel\n  # define :admin_path class attrbiute with class and instance methodss\n  cattr :admin_path,\n    # defaults value will be calculated on read, if proc provided\n    default: proc { '/admin/%s' % to_s.tableize },\n    \n    # create class setter and getter\n    class: true,\n    \n    # create instance setter and getter\n    instance: true\n\n  # :icon, default value is nil\n  cattr :icon, 'undefined.png'\nend\n\nclass User \u003c AppModel\n  cattr.icon = 'user.png'\nend\n\nclass Product \u003c AppModel\nend\n\n###\n\nUser.cattr.admin_path  # '/admin/people' (available allways)\nUser.admin_path        # '/admin/people' (from class: true)\n@user.cattr.admin_path # '/admin/people' (available allways)\n@user.admin_path       # '/admin/people' (from instance: true)\n\nUser.cattr.icon        # 'user.png'\nUser.icon              # NoMethodErorr\n@user.cattr.icon       # 'user.png'\n@user.icon             # NoMethodErorr\n\nProduct.cattr.icon     # 'undefined.png'\nProduct.icon           # NoMethodErorr\n@product.cattr.icon    # 'undefined.png'\n@product.icon          # NoMethodErorr\n```\n\nTips\n\n```ruby\nclass User\n  cattr :icon, 'undefined.png'\n  # can ve defined as\n  cattr.icon = 'undefined.png'\n  # but in this fashion you can't define class and instance setters and getters\n  \n  # this all all the same\n  catr.time_now = proc { Time.now }\n  catr(:time_now) { Time.now }\n  catr :time_now, default: proc { Time.now }\n\n  # you can define argument withut equal sign\n  self.admin_path 'foo'\n  # same as\n  self.admin_path = 'foo'\nend\n\n# it is advice to define cattr with\ncattr :name, opts\n\n# and use via\nKlass.cattr.name\n@klass.cattr.name\n```\n\n## Q\u0026A\n\nQ: Why did you create this when Rails provides `class_attribute` ?\n\u003cbr\u003e\nA: There is a small but nimble Ruby community that uses Ruby and outside the Rails eco-system.\n\nQ: Why did you not use some and improve one of the existing similar libs?\n\u003cbr\u003e\nA: I like clean interface without base class pollution, approach I did not find anywhre.\nThis gem only adds `cattr` methods to class and instance.\nOptionaly, you can polute only class methods, you can pollute instance only methods, or both.\n\n## Dependency\n\nnone\n\n## Development\n\nAfter checking out the repo, run `bundle install` to install dependencies. Then, run `rspec` to run the tests.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/dux/class-cattr.\nThis project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the\n[Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdux%2Fclass-cattr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdux%2Fclass-cattr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdux%2Fclass-cattr/lists"}