Class: Excom::Plugins::Sentry::Sentinel
- Inherits:
-
Object
- Object
- Excom::Plugins::Sentry::Sentinel
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(command) ⇒ Sentinel
Returns a new instance of Sentinel.
40
41
42
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 40
def initialize(command)
@command = command
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 85
def method_missing(name, *)
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
#command ⇒ Object
Returns the value of attribute command.
38
39
40
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 38
def command
@command
end
|
Class Method Details
.allow(*actions) ⇒ Object
20
21
22
23
24
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 20
def self.allow(*actions)
actions.each do |name|
define_method("#{name}?") { true }
end
end
|
.denial_reason ⇒ Object
16
17
18
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 16
def self.denial_reason
@denial_reason ||= :denied
end
|
.denial_reason=(reason) ⇒ Object
12
13
14
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 12
def self.denial_reason=(reason)
@denial_reason = reason
end
|
.deny(*actions, with: nil) ⇒ Object
26
27
28
29
30
31
32
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 26
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
4
5
6
7
8
9
10
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 4
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
|
.sentinels ⇒ Object
34
35
36
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 34
def self.sentinels
@sentinels ||= []
end
|
Instance Method Details
#denial_reason(action) ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 44
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
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 54
def sentry(klass)
unless Class === klass
klass_name = self.class.name.sub(/[^:]+\Z/, ''.freeze) + "_#{klass}".gsub!(/(_([a-z]))/){ $2.upcase } + 'Sentry'.freeze
klass = klass_name.respond_to?(:constantize) ?
klass_name.constantize :
klass_name.split('::'.freeze).reduce(Object){ |obj, name| obj.const_get(name) }
end
klass.new(command)
end
|
#to_hash ⇒ Object
65
66
67
68
69
70
71
72
73
|
# File 'lib/excom/plugins/sentry/sentinel.rb', line 65
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
|