Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/norbertmaleckii/simple-policy-rb
Helps you to define policy in a simple way. Define your policies in classses, it means you can separate them from models and reuse them.
https://github.com/norbertmaleckii/simple-policy-rb
rails rails-policies ruby ruby-policies
Last synced: 6 days ago
JSON representation
Helps you to define policy in a simple way. Define your policies in classses, it means you can separate them from models and reuse them.
- Host: GitHub
- URL: https://github.com/norbertmaleckii/simple-policy-rb
- Owner: norbertmaleckii
- License: mit
- Created: 2021-10-28T12:31:29.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-18T18:09:23.000Z (over 1 year ago)
- Last Synced: 2024-04-26T00:42:29.477Z (8 months ago)
- Topics: rails, rails-policies, ruby, ruby-policies
- Language: Ruby
- Homepage:
- Size: 46.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# SimplePolicy
Policy system for Ruby with awesome features!
Helps you to define policy in a simple way. Define your policies in classses, it means you can separate them from models and reuse them.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'simple_policy'
```And then execute:
$ bundle install
Or install it yourself as:
$ gem install simple_policy
## Usage
Policies are defined as plain Ruby classes. For rails usage you can put definitions inside `app/policies` folder.
When we want define basic policy class, simply do that:
```ruby
class UserEndOfSubscriptionPolicy < SimplePolicy::Entity
object_alias :userdef call
block_passed? && correct_action? && end_of_subscription?
enddef block_passed?
return true unless blockinstance_eval(&block)
enddef correct_action?
options[:action] == 'games/play'
enddef end_of_subscription?
user.end_of_subscription_at < Time.now
end
end
```After that we can use our policy for one object:
```ruby
User = Struct.new(:end_of_subscription_at, :check_subscription)user = User.new(Time.now - (60 * 60 * 24), false)
UserEndOfSubscriptionPolicy.applies_to?(user, action: 'games/play')
#=> trueUserEndOfSubscriptionPolicy.applies_to?(user, action: 'games/play') { user.check_subscription }
#=> false
```Or for collection:
```ruby
User = Struct.new(:end_of_subscription_at, :check_subscription)users = Array.new(5) { |i| User.new(Time.now - (60 * 60 * 24 * (i - 2)), false) }
UserEndOfSubscriptionPolicy.applies_to_all?(users, action: 'games/play')
#=> falseUserEndOfSubscriptionPolicy.applies_to_all?(users, action: 'games/play') { user.check_subscription }
#=> falseUserEndOfSubscriptionPolicy.applies_to_any?(users, action: 'games/play')
#=> trueUserEndOfSubscriptionPolicy.applies_to_any?(users, action: 'games/play') { user.check_subscription }
#=> false
```## Development
After 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.
To 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).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/norbertmaleckii/simple-policy-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-policy-rb/blob/main/CODE_OF_CONDUCT.md).
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the SimplePolicy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/norbertmaleckii/simple-policy-rb/blob/main/CODE_OF_CONDUCT.md).