Module: Allowable

Included in:
Hash
Defined in:
lib/allowable/allowable.rb,
lib/allowable/railtie.rb,
lib/allowable/version.rb

Overview

Filter hashes by setting allowed or forbidden values for specific keys.

hash = { one: 'one', two: 'two' }

hash.forbid(one: 'one') # => { two: 'two' }

hash.allow(one: 'two') # => { two: 'two' }

hash.allow(one: ['one', 'two']) # => { one: 'one', two: 'two' }

hash.forbid(one: ['one', 'two']) # => { two: 'two' }

hash.allow!(one: 'two') # => { two: 'two' }

hash.forbid!(two: 'two') # => {}

hash # => {}

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
'1.1.0'

Instance Method Summary collapse

Instance Method Details

#allow(filters = {}) ⇒ Object



21
22
23
# File 'lib/allowable/allowable.rb', line 21

def allow(filters = {})
  dup.allow!(filters)
end

#allow!(filters = {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/allowable/allowable.rb', line 25

def allow!(filters = {})
  filters.each do |key, val|
    delete(key) unless Array(val).include?(self[key]) || val == self[key]
  end
  self
end

#forbid(filters = {}) ⇒ Object



32
33
34
# File 'lib/allowable/allowable.rb', line 32

def forbid(filters = {})
  dup.forbid!(filters)
end

#forbid!(filters = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/allowable/allowable.rb', line 36

def forbid!(filters = {})
  filters.each do |key, val|
    delete(key) if Array(val).include?(self[key]) || val == self[key]
  end
  self
end