Module: Concealer

Extended by:
ActiveSupport::Concern
Defined in:
lib/concealer.rb,
lib/concealer/proxy.rb,
lib/concealer/version.rb,
lib/concealer/fallback.rb,
lib/concealer/strategy.rb

Defined Under Namespace

Classes: Fallback, Proxy, Strategy

Constant Summary collapse

VERSION =
"0.1.0"
@@default_fallback =
Concealer::Fallback::Nil.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fallback_for(model, method) ⇒ Object

Returns the set fallback for the given method on the given model If none is set it returns the default fallback set with Concealer.default_fallback=



62
63
64
65
# File 'lib/concealer.rb', line 62

def self.fallback_for(model, method)
  @@fallbacks ||= {}
  @@fallbacks.try(:[], model.class.to_s.to_sym).try(:[], method.to_sym) || @@default_fallback
end

.register_fallback(model, method, fallback) ⇒ Object

Registers a new fallback for the given method on the given model



51
52
53
54
55
56
# File 'lib/concealer.rb', line 51

def self.register_fallback(model, method, fallback)
  name = model.is_a?(Symbol) ? model : model.to_s.to_sym
  @@fallbacks ||= {}
  @@fallbacks[name] ||= {}
  @@fallbacks[name][method] = fallback
end

.strategyObject

Returns the strategy that’s set for concealer. Default is Concealer::Strategy::Allow



35
36
37
# File 'lib/concealer.rb', line 35

def self.strategy
  Thread.current[:concealer_strategy] || Concealer::Strategy::Allow.new
end

.strategy=(strategy) ⇒ Object

Sets the strategy concealer uses to check permissions



28
29
30
# File 'lib/concealer.rb', line 28

def self.strategy=(strategy)
  Thread.current[:concealer_strategy] = strategy
end

.with_strategy(strategy) ⇒ Object

Executes the block with the given strategy.



42
43
44
45
46
# File 'lib/concealer.rb', line 42

def self.with_strategy(strategy)
  save, self.strategy = self.strategy, strategy
  yield
  self.strategy = save
end

Instance Method Details

#concealedObject

Accesses the current object using Concealer to check method calls

Example:

Concealer.strategy = Concealer::Strategy::Deny.new
user = User.new(:name => "Christian")
user.name # => Christian
user.concealed.name # => nil


76
77
78
# File 'lib/concealer.rb', line 76

def concealed
  Proxy.new(self, Concealer.strategy)
end

#default_fallbackObject

:singleton-method: default_fallback Returns the set default fallback. If not explicitly set to something else it is Concealer::Fallback::Nil.



22
# File 'lib/concealer.rb', line 22

mattr_accessor :default_fallback