Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dux/class-mattr
class-mattr gem provides simple way to set and get method attributes in ruby.
https://github.com/dux/class-mattr
Last synced: about 2 months ago
JSON representation
class-mattr gem provides simple way to set and get method attributes in ruby.
- Host: GitHub
- URL: https://github.com/dux/class-mattr
- Owner: dux
- License: mit
- Created: 2021-02-15T00:35:39.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-03-02T10:25:09.000Z (almost 4 years ago)
- Last Synced: 2024-02-23T06:23:04.822Z (11 months ago)
- Language: Ruby
- Homepage:
- Size: 51.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Method attributes
class-mattr gem provides simple way to set and get method attributes.
## Installation
to install
`gem install class-mattr`
or in Gemfile
`gem 'class-mattr'`
and to use
`require 'class-mattr'`
## How to use
* include `ClassMattr`
* define method attributes via `mattr.name argument`
* get them via `mattr :name` (class and instance method provided)```ruby
class Foo
include ClassMattrmattr.name 'Test method'
mattr.opt guest: true
def test
enddef get
mattr :test # get :test method attributes
end
end
```### Real world scenario
Protect all urls in a way that user has to have a session (`current_user` is defined),
but allow to bypass that rule if `mattr.guest_access` is definedOptionaly you can define array of method names as sysbols for `mattr` method, to be able to use them without `mattr.` prefix.
```ruby
class ApplicationController < ActionController::Base
include ClassMattr# allow usage without mattr prefix
mattr [:guest_access]before_action do
# get all method attributes
opts = mattr params[:action]if !opts[:guest_only] && !current_user
redirect_to '/login', info: 'You must log in to access this resource'
end
end
endclass UserController < ApplicationController
# mattr.guest_access, aliased trough class method
# default argument is true, if nil defined
guest_access
def login
# ...
enddef profile
# ...
end
end
```### Manual hash get
If you need, you can get method attributes hash manualy
```ruby
class ClassA
include ClassMattrmattr.foo 123
mattr.opt name: 'Dux'mattr.get_hash # { foo:123, name: 'Dux' }
end
```### Ruby metaprograming fun
`ClassMattr` is exported as a module and a function too.
If call `ClassMattr` in a [function context](https://github.com/dux/class-mattr/blob/main/lib/lib/method.rb)
you can include and define exported methods in one go as `ClassMattr :exported_method1, :exported_method2`.```ruby
class ApplicationController < ActionController::Base
# same as
# include ClassMattr
# mattr [:guest_access]
ClassMattr :guest_accessbefore_action do
opts = mattr :login # { guest_access: true }
end
endclass UserController < ApplicationController
guest_access
def login
# ...
end
end
```## Dependency
none
## Development
After checking out the repo, run `bundle install` to install dependencies. Then, run `rspec` to run the tests.
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/dux/class-mattr.
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the
[Contributor Covenant](http://contributor-covenant.org) code of conduct.## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).