module Arturo::SpecialHandling
Adds whitelist and blacklist support to individual features by name or for all features. Blacklists override whitelists. (In the world of Apache, Features are “(deny,allow)”.) @example
# allow admins for some_feature: Arturo::Feature.whitelist(:some_feature) do |user| user.is_admin? end # disallow for small accounts for another_feature: Arturo::Feature.blacklist(:another_feature) do |user| user.account.small? end # allow large accounts access to large features: Arturo::Feature.whitelist do |feature, user| feature.symbol.to_s =~ /^large/ && user.account.large? end
Blacklists and whitelists can be defined before the feature exists and are not persisted, so they are best defined in initializers. This is particularly important if your application runs in several different processes or on several servers.
Public Instance Methods
blacklist(feature_symbol = nil, &block)
click to toggle source
# File lib/arturo/special_handling.rb, line 46 def blacklist(feature_symbol = nil, &block) blacklists << two_arg_block(feature_symbol, block) end
blacklists()
click to toggle source
# File lib/arturo/special_handling.rb, line 38 def blacklists @blacklists ||= [] end
two_arg_block(symbol, block)
click to toggle source
# File lib/arturo/special_handling.rb, line 52 def two_arg_block(symbol, block) return block if symbol.nil? lambda do |feature, recipient| feature.symbol.to_s == symbol.to_s && block.call(recipient) end end
whitelist(feature_symbol = nil, &block)
click to toggle source
# File lib/arturo/special_handling.rb, line 42 def whitelist(feature_symbol = nil, &block) whitelists << two_arg_block(feature_symbol, block) end
whitelists()
click to toggle source
# File lib/arturo/special_handling.rb, line 34 def whitelists @whitelists ||= [] end
Protected Instance Methods
blacklisted?(feature_recipient)
click to toggle source
# File lib/arturo/special_handling.rb, line 67 def blacklisted?(feature_recipient) x_listed?(self.class.blacklists, feature_recipient) end
whitelisted?(feature_recipient)
click to toggle source
# File lib/arturo/special_handling.rb, line 63 def whitelisted?(feature_recipient) x_listed?(self.class.whitelists, feature_recipient) end
x_listed?(lists, feature_recipient)
click to toggle source
# File lib/arturo/special_handling.rb, line 71 def x_listed?(lists, feature_recipient) lists.any? { |block| block.call(self, feature_recipient) } end