{"id":21203420,"url":"https://github.com/dux/class-mattr","last_synced_at":"2025-09-11T00:45:54.818Z","repository":{"id":56843536,"uuid":"338933047","full_name":"dux/class-mattr","owner":"dux","description":"class-mattr gem provides simple way to set and get method attributes in ruby.","archived":false,"fork":false,"pushed_at":"2021-03-02T10:25:09.000Z","size":53,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-17T09:49:59.590Z","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/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-15T00:35:39.000Z","updated_at":"2022-08-01T12:27:19.000Z","dependencies_parsed_at":"2022-09-07T07:11:26.874Z","dependency_job_id":null,"html_url":"https://github.com/dux/class-mattr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dux/class-mattr","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dux%2Fclass-mattr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dux%2Fclass-mattr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dux%2Fclass-mattr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dux%2Fclass-mattr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dux","download_url":"https://codeload.github.com/dux/class-mattr/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dux%2Fclass-mattr/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274556350,"owners_count":25307506,"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-09-10T02:00:12.551Z","response_time":83,"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":[],"created_at":"2024-11-20T20:23:53.754Z","updated_at":"2025-09-11T00:45:54.782Z","avatar_url":"https://github.com/dux.png","language":"Ruby","readme":"# Method attributes\n\nclass-mattr gem provides simple way to set and get method attributes.\n\n## Installation\n\nto install\n\n`gem install class-mattr`\n\nor in Gemfile\n\n`gem 'class-mattr'`\n\nand to use\n\n`require 'class-mattr'`\n\n## How to use\n\n* include `ClassMattr`\n* define method attributes via `mattr.name argument`\n* get them via `mattr :name` (class and instance method provided)\n\n```ruby\nclass Foo\n  include ClassMattr\n\n  mattr.name 'Test method'\n  mattr.opt  guest: true\n  def test\n  end\n\n  def get\n    mattr :test # get :test method attributes\n  end\nend\n```\n\n### Real world scenario\n\nProtect all urls in a way that user has to have a session (`current_user` is defined),\nbut allow to bypass that rule if `mattr.guest_access` is defined\n\nOptionaly you can define array of method names as sysbols for `mattr` method, to be able to use them without `mattr.` prefix.\n\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  include ClassMattr\n\n  # allow usage without mattr prefix\n  mattr [:guest_access]\n\n  before_action do\n    # get all method attributes\n    opts = mattr params[:action]\n\n    if !opts[:guest_only] \u0026\u0026 !current_user\n      redirect_to '/login', info: 'You must log in to access this resource'\n    end\n  end\nend\n\nclass UserController \u003c ApplicationController\n  # mattr.guest_access, aliased trough class method\n  # default argument is true, if nil defined\n  guest_access\n  def login\n    # ...\n  end\n\n  def profile\n    # ...\n  end\nend\n```\n\n### Manual hash get\n\nIf you need, you can get method attributes hash manualy\n\n```ruby\nclass ClassA\n  include ClassMattr\n\n  mattr.foo 123\n  mattr.opt name: 'Dux'\n\n  mattr.get_hash # { foo:123, name: 'Dux' }\nend\n```\n\n### Ruby metaprograming fun\n\n`ClassMattr` is exported as a module and a function too.\nIf call `ClassMattr` in a [function context](https://github.com/dux/class-mattr/blob/main/lib/lib/method.rb)\nyou can include and define exported methods in one go as `ClassMattr :exported_method1, :exported_method2`.\n\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  # same as\n  # include ClassMattr\n  # mattr [:guest_access]\n  ClassMattr :guest_access\n\n  before_action do\n    opts = mattr :login # { guest_access: true }\n  end\nend\n\nclass UserController \u003c ApplicationController\n  guest_access\n  def login\n    # ...\n  end\nend\n```\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-mattr.\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","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdux%2Fclass-mattr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdux%2Fclass-mattr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdux%2Fclass-mattr/lists"}