forbidium 1.0.1 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '008d26bbd4350a53113ea397507d127e219f0220'
4
- data.tar.gz: 7e4e0a68c9c7a4adf3fc9065339be324529cea65
3
+ metadata.gz: 9044ce986e2a490f08eb553163ae652e7100d6e3
4
+ data.tar.gz: 4ace021f869f46424ef2d215b6581de7897b4c05
5
5
  SHA512:
6
- metadata.gz: 6e05dd2083cb2325b6808ffdbf63748beacaf55c211eb2332e084ee72591429799e7d80a0e0eab957b5ad1695cb70dd916b34c25ee12d3ada58b0d19bc00f4c4
7
- data.tar.gz: 15ce9fe7cf6407b9f2066051cf05c3481fb90782ca8de0411a95d3b781e50070b56e676d4c2bc89a09fd7e2b9138433f18e725c8d42eca4cbbb3c47b080072d8
6
+ metadata.gz: 3ec7e6d6f08e3e019e4318addb6a66162051dd3275bd6007d580d977f4c41940aa71664de4d0506412a581b40ad3310cb29dc01f135487ab2512cd7723e223b6
7
+ data.tar.gz: 6eead3f3ca067cca134c626d2d6571f8027bef6eebf50c662b4fa8529136df6cdcf1fe328c6476f0ecd8128acc353c832ca8133bbde260a0679816ea6bfcaae0
data/README.md CHANGED
@@ -1,4 +1,8 @@
1
1
  # Forbidium
2
+ [![Gem Version](https://badge.fury.io/rb/forbidium.svg)](https://badge.fury.io/rb/forbidium)
3
+ [![Code Climate](https://codeclimate.com/github/msimonborg/forbidium/badges/gpa.svg)](https://codeclimate.com/github/msimonborg/forbidium)
4
+ [![Build Status](https://travis-ci.org/msimonborg/forbidium.svg?branch=master)](https://travis-ci.org/msimonborg/forbidium)
5
+ [![Coverage Status](https://coveralls.io/repos/github/msimonborg/forbidium/badge.svg?branch=master)](https://coveralls.io/github/msimonborg/forbidium?branch=master)
2
6
 
3
7
  Filter hashes by setting allowed or forbidden values for specific keys.
4
8
 
@@ -19,6 +23,8 @@ Or install it yourself as:
19
23
  $ gem install forbidium
20
24
 
21
25
  ## Usage
26
+ The gem will add four methods to `Hash`: `#allow`, `#allow!`, `#forbid`, and `#forbid!`
27
+
22
28
  ```ruby
23
29
  hash = { one: 'one', two: 'two' }
24
30
 
@@ -37,6 +43,14 @@ hash.forbid!(two: 'two') # => {}
37
43
  hash # => {}
38
44
  ```
39
45
 
46
+ When added to the `Gemfile` in a Rails project, `ActionController::Parameters` will also receive these methods so you can use them with your `strong_parameters`:
47
+
48
+ ```ruby
49
+ def user_params
50
+ params.require(:user).permit(:email, :password, :role).forbid(role: ['sys_admin', 'owner'])
51
+ end
52
+ ```
53
+
40
54
  ## Platform support
41
55
 
42
56
  Tested against:
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Forbidium
4
+ module ActionController # :nodoc:
5
+ module Parameters # :nodoc:
6
+ # Adds the #forbid and #forbid! methods
7
+ module Forbid # :nodoc:
8
+ include Forbidium::Forbid
9
+
10
+ def forbid!(filters = {})
11
+ filters.each do |key, val|
12
+ delete(key) if Array(val).include?(self[key])
13
+ if key.is_a? Symbol
14
+ delete(key.to_s) if Array(val).include?(self[key.to_s])
15
+ end
16
+ end
17
+ self
18
+ end
19
+ end
20
+
21
+ # Adds the #allow and #allow! methods
22
+ module Allow # :nodoc:
23
+ include Forbidium::Allow
24
+
25
+ def allow!(filters = {})
26
+ filters.each do |key, val|
27
+ delete(key) unless Array(val).include?(self[key])
28
+ if key.is_a? Symbol
29
+ delete(key.to_s) unless Array(val).include?(self[key.to_s])
30
+ end
31
+ end
32
+ self
33
+ end
34
+ end
35
+
36
+ include Allow
37
+ include Forbid
38
+ end
39
+ end
40
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Add #allow, #allow!, #forbid, and #forbid! to Hash
4
4
  class Hash
5
- include Forbidium
5
+ include Forbidium::Hash
6
6
  end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'forbidium/allow'
4
- require 'forbidium/forbid'
5
-
6
3
  # Filter hashes by setting allowed or forbidden values for specific keys.
7
4
  #
8
5
  # hash = { one: 'one', two: 'two' }
@@ -21,7 +18,15 @@ require 'forbidium/forbid'
21
18
  #
22
19
  # hash # => {}
23
20
  module Forbidium
24
- def self.included(base)
25
- [Forbidium::Allow, Forbidium::Forbid].each { |mod| base.send :include, mod }
21
+ module Allow # :nodoc:
22
+ def allow(filters = {})
23
+ dup.allow!(filters)
24
+ end
25
+ end
26
+
27
+ module Forbid # :nodoc:
28
+ def forbid(filters = {})
29
+ dup.forbid!(filters)
30
+ end
26
31
  end
27
32
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Forbidium
4
+ module Hash # :nodoc:
5
+ # Adds the #forbid and #forbid! methods
6
+ module Forbid # :nodoc:
7
+ include Forbidium::Forbid
8
+
9
+ def forbid!(filters = {})
10
+ filters.each do |key, val|
11
+ delete(key) if Array(val).include?(self[key])
12
+ end
13
+ self
14
+ end
15
+ end
16
+
17
+ # Adds the #allow and #allow! methods
18
+ module Allow # :nodoc:
19
+ include Forbidium::Allow
20
+
21
+ def allow!(filters = {})
22
+ filters.each do |key, val|
23
+ delete(key) unless Array(val).include?(self[key])
24
+ end
25
+ self
26
+ end
27
+ end
28
+
29
+ include Forbid
30
+ include Allow
31
+ end
32
+ end
@@ -7,7 +7,9 @@ module Forbidium
7
7
  class Railtie < ::Rails::Railtie
8
8
  initializer :forbidium do
9
9
  ActiveSupport.on_load :action_controller do
10
- ActionController::Parameters.send :include, Forbidium
10
+ ::ActionController::Parameters.send(
11
+ :include, Forbidium::ActionController::Parameters
12
+ )
11
13
  end
12
14
  end
13
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Forbidium
4
- VERSION = '1.0.1'
4
+ VERSION = '1.1.0'
5
5
  end
data/lib/forbidium.rb CHANGED
@@ -2,5 +2,7 @@
2
2
 
3
3
  require 'forbidium/version'
4
4
  require 'forbidium/forbidium'
5
+ require 'forbidium/action_controller/parameters'
6
+ require 'forbidium/hash'
5
7
  require 'forbidium/core_ext/hash'
6
8
  require 'forbidium/railtie' if defined? Rails
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forbidium
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - M. Simon Borg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-01 00:00:00.000000000 Z
11
+ date: 2017-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -48,10 +48,10 @@ files:
48
48
  - LICENSE.txt
49
49
  - README.md
50
50
  - lib/forbidium.rb
51
- - lib/forbidium/allow.rb
51
+ - lib/forbidium/action_controller/parameters.rb
52
52
  - lib/forbidium/core_ext/hash.rb
53
- - lib/forbidium/forbid.rb
54
53
  - lib/forbidium/forbidium.rb
54
+ - lib/forbidium/hash.rb
55
55
  - lib/forbidium/railtie.rb
56
56
  - lib/forbidium/version.rb
57
57
  homepage: https://github.com/msimonborg/forbidium
@@ -66,7 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
- version: 2.0.0
69
+ version: 2.2.0
70
70
  required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - ">="
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Forbidium
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
- delete(key.to_s) unless Array(val).include?(self[key.to_s])
14
- end
15
- self
16
- end
17
- end
18
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Forbidium
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
- delete(key.to_s) if Array(val).include?(self[key.to_s])
14
- end
15
- self
16
- end
17
- end
18
- end