allowable 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +0 -0
- data/LICENSE.txt +21 -0
- data/README.md +109 -0
- data/lib/allowable.rb +6 -0
- data/lib/allowable/allow.rb +17 -0
- data/lib/allowable/allowable.rb +26 -0
- data/lib/allowable/core_ext/hash.rb +6 -0
- data/lib/allowable/forbid.rb +17 -0
- data/lib/allowable/railtie.rb +14 -0
- data/lib/allowable/version.rb +5 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dd4c802b74f2cef0f4e342689bbe18bdb5f9d6db
|
4
|
+
data.tar.gz: f034111133e060edbc1e95c809d565ab22f7d3b1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7322dafe2fe77333cd801c086d8acd54eefafcea9b81d9befa7795eeebd55b908ac7ac0b9bb96fda1057d44778c3df344b1a5e4b5392975fdb5b38730939be13
|
7
|
+
data.tar.gz: 6eb308d093112989a07acd046753350146c987df73bfc92622d2e777c0b4cb51b5f18878e1b63e2405d0d05d8686a4b11e2fe73990c0375696c8f88f9beaddb0
|
data/CHANGELOG.md
ADDED
File without changes
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 M. Simon Borg
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# Allowable
|
2
|
+
[](https://badge.fury.io/rb/allowable)
|
3
|
+
[](https://codeclimate.com/github/msimonborg/allowable)
|
4
|
+
[](https://travis-ci.org/msimonborg/allowable)
|
5
|
+
[](https://coveralls.io/github/msimonborg/allowable?branch=master)
|
6
|
+
|
7
|
+
Filter hashes by setting allowed or forbidden values for specific keys.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'allowable'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install allowable
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
The gem will add four methods to `Hash`: `#allow`, `#allow!`, `#forbid`, and `#forbid!`
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
hash = { one: 'one', two: 'two' }
|
30
|
+
|
31
|
+
hash.forbid(one: 'one') # => { two: 'two' }
|
32
|
+
|
33
|
+
hash.allow(one: 'two') # => { two: 'two' }
|
34
|
+
|
35
|
+
hash.allow(one: ['one', 'two']) # => { one: 'one', two: 'two' }
|
36
|
+
|
37
|
+
hash.forbid(one: ['one', 'two']) # => { two: 'two' }
|
38
|
+
|
39
|
+
hash.allow!(one: 'two') # => { two: 'two' }
|
40
|
+
|
41
|
+
hash.forbid!(two: 'two') # => {}
|
42
|
+
|
43
|
+
hash # => {}
|
44
|
+
```
|
45
|
+
|
46
|
+
#### Type sensitive for `Hash`
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
hash = { 'one' => 'one', 'two' => 'two' }
|
50
|
+
|
51
|
+
hash.forbid(one: 'one') # => { "one" => "one", "two" => "two" }
|
52
|
+
|
53
|
+
hash.forbid('one' => 'one') # => { "two" => "two" }
|
54
|
+
```
|
55
|
+
|
56
|
+
## With Rails and strong parameters
|
57
|
+
|
58
|
+
When added to the `Gemfile` in a Rails project, `ActionController::Parameters` will automatically receive these methods so you can use them with your `strong_parameters`:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
def user_params
|
62
|
+
params.require(:user).permit(:email, :password, :role).forbid(role: ['sys_admin', 'owner'])
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
#### Type insensitive for `HashWithIndifferentAccess`
|
67
|
+
```ruby
|
68
|
+
params = ActionController::Parameters.new('one' => 'one', 'two' => 'two').permit(:one, :two)
|
69
|
+
|
70
|
+
params.forbid(one: 'one').to_h # => { "two" => "two" }
|
71
|
+
|
72
|
+
params.forbid('one' => 'one').to_h # => { "two" => "two" }
|
73
|
+
```
|
74
|
+
|
75
|
+
If your custom `Hash`-like class implements `#delete` and the `#[]` finder, you can `include Allowable` to mix in the methods.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class MyHash
|
79
|
+
include Allowable
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
## Platform support
|
84
|
+
|
85
|
+
This gem should work with Ruby >= 2.0.0, however at the moment it is only tested for Ruby >= 2.2.2
|
86
|
+
|
87
|
+
Tested against:
|
88
|
+
* MRI 2.2.2
|
89
|
+
* MRI 2.3.0
|
90
|
+
* MRI 2.3.4
|
91
|
+
* MRI 2.4.1
|
92
|
+
* JRuby 9.1.6.0
|
93
|
+
* JRuby HEAD
|
94
|
+
* MRI HEAD
|
95
|
+
|
96
|
+
## Development
|
97
|
+
|
98
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
99
|
+
|
100
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/msimonborg/allowable.
|
105
|
+
|
106
|
+
|
107
|
+
## License
|
108
|
+
|
109
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/allowable.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Allowable
|
4
|
+
# Adds the #allow and #allow! methods
|
5
|
+
module Allow
|
6
|
+
def allow(filters = {})
|
7
|
+
dup.allow!(filters)
|
8
|
+
end
|
9
|
+
|
10
|
+
def allow!(filters = {})
|
11
|
+
filters.each do |key, val|
|
12
|
+
delete(key) unless Array(val).include?(self[key])
|
13
|
+
end
|
14
|
+
self
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'allowable/allow'
|
4
|
+
require 'allowable/forbid'
|
5
|
+
|
6
|
+
# Filter hashes by setting allowed or forbidden values for specific keys.
|
7
|
+
#
|
8
|
+
# hash = { one: 'one', two: 'two' }
|
9
|
+
#
|
10
|
+
# hash.forbid(one: 'one') # => { two: 'two' }
|
11
|
+
#
|
12
|
+
# hash.allow(one: 'two') # => { two: 'two' }
|
13
|
+
#
|
14
|
+
# hash.allow(one: ['one', 'two']) # => { one: 'one', two: 'two' }
|
15
|
+
#
|
16
|
+
# hash.forbid(one: ['one', 'two']) # => { two: 'two' }
|
17
|
+
#
|
18
|
+
# hash.allow!(one: 'two') # => { two: 'two' }
|
19
|
+
#
|
20
|
+
# hash.forbid!(two: 'two') # => {}
|
21
|
+
#
|
22
|
+
# hash # => {}
|
23
|
+
module Allowable
|
24
|
+
include Forbid
|
25
|
+
include Allow
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Allowable
|
4
|
+
# Adds the #forbid and #forbid! methods
|
5
|
+
module Forbid
|
6
|
+
def forbid(filters = {})
|
7
|
+
dup.forbid!(filters)
|
8
|
+
end
|
9
|
+
|
10
|
+
def forbid!(filters = {})
|
11
|
+
filters.each do |key, val|
|
12
|
+
delete(key) if Array(val).include?(self[key])
|
13
|
+
end
|
14
|
+
self
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/railtie'
|
4
|
+
|
5
|
+
module Allowable
|
6
|
+
# Railtie initializer that adds Allowable methods to controller params
|
7
|
+
class Railtie < ::Rails::Railtie
|
8
|
+
initializer :allowable do
|
9
|
+
ActiveSupport.on_load :action_controller do
|
10
|
+
ActionController::Parameters.send :include, Allowable
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: allowable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- M. Simon Borg
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Filter hash keys based on allowed and forbidden values.
|
42
|
+
email:
|
43
|
+
- msimonborg@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- CHANGELOG.md
|
49
|
+
- LICENSE.txt
|
50
|
+
- README.md
|
51
|
+
- lib/allowable.rb
|
52
|
+
- lib/allowable/allow.rb
|
53
|
+
- lib/allowable/allowable.rb
|
54
|
+
- lib/allowable/core_ext/hash.rb
|
55
|
+
- lib/allowable/forbid.rb
|
56
|
+
- lib/allowable/railtie.rb
|
57
|
+
- lib/allowable/version.rb
|
58
|
+
homepage: https://github.com/msimonborg/allowable
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 2.0.0
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.6.11
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Filter hash keys based on allowed and forbidden values.
|
82
|
+
test_files: []
|