Class: Excom::Plugins::Sentry::Sentinel

Inherits:
Object
  • Object
show all
Defined in:
lib/excom/plugins/sentry/sentinel.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ Sentinel

Returns a new instance of Sentinel.



61
62
63
# File 'lib/excom/plugins/sentry/sentinel.rb', line 61

def initialize(service)
  @service = service
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



59
60
61
# File 'lib/excom/plugins/sentry/sentinel.rb', line 59

def service
  @service
end

Class Method Details

.allow(*actions) ⇒ Object



37
38
39
40
41
# File 'lib/excom/plugins/sentry/sentinel.rb', line 37

def self.allow(*actions)
  actions.each do |name|
    define_method("#{name}?") { true }
  end
end

.delegationsObject



55
56
57
# File 'lib/excom/plugins/sentry/sentinel.rb', line 55

def self.delegations
  const_get(:Delegations)
end

.denial_reasonObject



33
34
35
# File 'lib/excom/plugins/sentry/sentinel.rb', line 33

def self.denial_reason
  @denial_reason ||= :denied
end

.denial_reason=(reason) ⇒ Object



29
30
31
# File 'lib/excom/plugins/sentry/sentinel.rb', line 29

def self.denial_reason=(reason)
  @denial_reason = reason
end

.deny(*actions, with: nil) ⇒ Object



43
44
45
46
47
48
49
# 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) ⇒ Object



21
22
23
24
25
26
27
# 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) ⇒ Object



4
5
6
7
8
9
10
# 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

.sentinelsObject



51
52
53
# File 'lib/excom/plugins/sentry/sentinel.rb', line 51

def self.sentinels
  @sentinels ||= []
end

.service_classObject



17
18
19
# File 'lib/excom/plugins/sentry/sentinel.rb', line 17

def self.service_class
  @service_class
end

.service_class=(klass) ⇒ Object



12
13
14
15
# 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

Instance Method Details

#denial_reason(action) ⇒ Object



65
66
67
68
69
70
71
72
73
# 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

#sentry(klass) ⇒ Object



75
76
77
78
# 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_hashObject



80
81
82
83
84
85
86
87
88
# 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