class Excom::Plugins::Sentry::Sentinel

Attributes

service[R]

Public Class Methods

allow(*actions) click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 37
def self.allow(*actions)
  actions.each do |name|
    define_method("#{name}?") { true }
  end
end
delegations() click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 55
def self.delegations
  const_get(:Delegations)
end
denial_reason() click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 33
def self.denial_reason
  @denial_reason ||= :denied
end
denial_reason=(reason) click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 29
def self.denial_reason=(reason)
  @denial_reason = reason
end
deny(*actions, with: nil) click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 43
def self.deny(*actions, with: nil)
  return deny_with(with){ deny(*actions) } unless with.nil?

  actions.each do |name|
    define_method("#{name}?") { false }
  end
end
deny_with(reason) click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 21
def self.deny_with(reason)
  return self.denial_reason = reason unless block_given?

  klass = Class.new(self, &Proc.new)
  klass.denial_reason = reason
  sentinels << klass
end
inherited(sentry_class) click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 4
def self.inherited(sentry_class)
  return unless self == Sentinel

  sentry_class.denial_reason = denial_reason
  sentry_class.const_set(:Delegations, Module.new)
  sentry_class.send(:include, sentry_class::Delegations)
end
new(service) click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 61
def initialize(service)
  @service = service
end
sentinels() click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 51
def self.sentinels
  @sentinels ||= []
end
service_class() click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 17
def self.service_class
  @service_class
end
service_class=(klass) click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 12
def self.service_class=(klass)
  sentinels.each{ |s| s.service_class = klass }
  @service_class = klass
end

Public Instance Methods

denial_reason(action) click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 65
def denial_reason(action)
  method = "#{action}?"

  reason = sentries.reduce(nil) do |result, sentry|
    result || (sentry.class.denial_reason unless !sentry.respond_to?(method) || sentry.public_send(method))
  end

  Proc === reason ? instance_exec(&reason) : reason
end
method_missing(name, *args) click to toggle source
Calls superclass method
# File lib/excom/plugins/sentry/sentinel.rb, line 135
def method_missing(name, *args)
  unless delegations_defined?
    define_delegations!
    return send(name, *args) if respond_to?(name)
  end

  if name.to_s.end_with?(??)
    sentinels[1..-1].each do |sentry|
      return sentry.public_send(name) if sentry.respond_to?(name)
    end
  end

  super
end
sentry(klass) click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 75
def sentry(klass)
  klass = derive_sentry_class(klass) unless Class === klass
  klass.new(service)
end
to_hash() click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 80
def to_hash
  sentries.reduce({}) do |result, sentry|
    partial = sentry.public_methods(false).grep(/\?$/).each_with_object({}) do |method, hash|
      hash[method.to_s[0...-1]] = !!sentry.public_send(method)
    end

    result.merge!(partial){ |_k, old, new| old && new }
  end
end

Private Instance Methods

constantize(klass, sentry_name) click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 106
        def constantize(klass, sentry_name)
  module_prefix = (inline? ? self.class.service_class.name : self.class.name).sub(/[^:]+\Z/, ''.freeze)

  klass_name = module_prefix + "_#{klass}".gsub!(/(_([a-z]))/){ $2.upcase } + sentry_name

  klass_name.respond_to?(:constantize) ?
    klass_name.constantize :
    klass_name.split('::'.freeze).reduce(Object){ |obj, name| obj.const_get(name) }
end
define_delegations!() click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 120
        def define_delegations!
  delegated_methods = self.class.service_class.arg_methods.instance_methods +
    Array(self.class.service_class.plugins[:sentry].options[:delegate])

  delegated_methods.each do |name|
    self.class.delegations.send(:define_method, name) { service.public_send(name) }
  end

  self.class.instance_variable_set('@delegations_defined'.freeze, true)
end
delegations_defined?() click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 131
        def delegations_defined?
  self.class.instance_variable_get('@delegations_defined'.freeze)
end
derive_sentry_class(klass) click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 100
        def derive_sentry_class(klass)
  constantize(klass, '::Sentry'.freeze)
rescue NameError
  constantize(klass, 'Sentry'.freeze)
end
inline?() click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 116
        def inline?
  self.class.service_class.const_defined?(:Sentry) && self.class.service_class::Sentry == self.class
end
sentinels() click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 94
        def sentinels
  @sentinels ||= self.class.sentinels.map do |klass|
    klass.new(service)
  end
end
sentries() click to toggle source
# File lib/excom/plugins/sentry/sentinel.rb, line 90
        def sentries
  [self] + sentinels
end