Class: Kindergarten::Sandbox
- Inherits:
-
Object
- Object
- Kindergarten::Sandbox
show all
- Defined in:
- lib/kindergarten/sandbox.rb,
lib/kindergarten/exceptions.rb
Defined Under Namespace
Classes: NoPurposeError
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#allows?(action, target) ⇒ Boolean
(also: #allowed?)
-
#broadcast(&block) ⇒ Object
-
#broadcast!(event) ⇒ Object
-
#disallows?(action, target) ⇒ Boolean
(also: #disallowed?)
-
#extend_perimeter(*perimeter_classes) ⇒ Object
(also: #load_perimeter, #load_module)
-
#extend_purpose(perimeter, instance) ⇒ Object
-
#guard(action, target, opts = {}) ⇒ Object
-
#initialize(child) ⇒ Sandbox
constructor
A new instance of Sandbox.
-
#method_missing(name, *args, &block) ⇒ Object
TODO: Find a purpose and call that - move this block to Purpose.
-
#subscribe(purpose_name, *events, &block) ⇒ Object
-
#unguarded(&block) ⇒ Object
-
#unguarded? ⇒ Boolean
-
#unsubscribe(purpose_name, *events) ⇒ Object
Constructor Details
#initialize(child) ⇒ Sandbox
Returns a new instance of Sandbox.
5
6
7
8
9
10
11
12
13
|
# File 'lib/kindergarten/sandbox.rb', line 5
def initialize(child)
@child = child
@governess = Kindergarten::HeadGoverness.new(child)
@purpose = {}
@perimeters = []
@unguarded = false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
TODO: Find a purpose and call that - move this block to Purpose
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/kindergarten/sandbox.rb', line 130
def method_missing(name, *args, &block)
super
rescue NoMethodError => ex
unless purpose.has_key?(name)
raise Kindergarten::Sandbox::NoPurposeError.new(name, self)
end
return purpose[name]
end
|
Instance Attribute Details
#child ⇒ Object
Returns the value of attribute child.
3
4
5
|
# File 'lib/kindergarten/sandbox.rb', line 3
def child
@child
end
|
#governess ⇒ Object
Returns the value of attribute governess.
3
4
5
|
# File 'lib/kindergarten/sandbox.rb', line 3
def governess
@governess
end
|
#perimeters ⇒ Object
Returns the value of attribute perimeters.
3
4
5
|
# File 'lib/kindergarten/sandbox.rb', line 3
def perimeters
@perimeters
end
|
#purpose ⇒ Object
Returns the value of attribute purpose.
3
4
5
|
# File 'lib/kindergarten/sandbox.rb', line 3
def purpose
@purpose
end
|
Instance Method Details
#allows?(action, target) ⇒ Boolean
Also known as:
allowed?
81
82
83
|
# File 'lib/kindergarten/sandbox.rb', line 81
def allows?(action, target)
governess.can?(action, target)
end
|
#broadcast(&block) ⇒ Object
119
120
121
|
# File 'lib/kindergarten/sandbox.rb', line 119
def broadcast(&block)
@broadcast = block
end
|
#broadcast!(event) ⇒ Object
123
124
125
126
127
|
# File 'lib/kindergarten/sandbox.rb', line 123
def broadcast!(event)
return if @broadcast.nil?
@broadcast.call(event)
end
|
#disallows?(action, target) ⇒ Boolean
Also known as:
disallowed?
86
87
88
|
# File 'lib/kindergarten/sandbox.rb', line 86
def disallows?(action, target)
governess.cannot?(action, target)
end
|
#extend_perimeter(*perimeter_classes) ⇒ Object
Also known as:
load_perimeter, load_module
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/kindergarten/sandbox.rb', line 15
def extend_perimeter(*perimeter_classes)
perimeter_classes.each do |perimeter_class|
if @perimeters.collect(&:class).include?(perimeter_class)
return
end
child = self.child
governess = perimeter_class.governess ?
perimeter_class.governess.new(child) :
self.governess
perimeter = perimeter_class.new(self, governess)
raise ArgumentError.new(
"Module must inherit from Kindergarten::Perimeter"
) unless perimeter.kind_of?(Kindergarten::Perimeter)
self.extend_purpose(perimeter.class, perimeter)
unless governess == self.governess || perimeter_class.govern_proc.nil?
self.governess.instance_eval(&perimeter_class.govern_proc)
end
perimeter_class.subscriptions.each do |purpose, events|
if self.purpose[purpose].nil?
Kindergarten.warning "#{perimeter_class} has subscriptions on un-loaded purpose :#{purpose}"
next
end
events.each do |event, blocks|
blocks.each do |block|
self.purpose[purpose]._subscribe(event, &block)
end
end
end
@perimeters << perimeter
end
end
|
#extend_purpose(perimeter, instance) ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/kindergarten/sandbox.rb', line 61
def extend_purpose(perimeter, instance)
name = perimeter.purpose || raise(
Kindergarten::Perimeter::NoPurpose.new(perimeter)
)
name = name.to_sym
self.purpose[name] ||= Kindergarten::Purpose.new(name, self)
self.purpose[name].add_perimeter(perimeter, instance)
end
|
#guard(action, target, opts = {}) ⇒ Object
91
92
93
94
95
|
# File 'lib/kindergarten/sandbox.rb', line 91
def guard(action, target, opts={})
unless allows?(action, target)
raise Kindergarten::AccessDenied.new(action, target, opts)
end
end
|
#subscribe(purpose_name, *events, &block) ⇒ Object
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/kindergarten/sandbox.rb', line 97
def subscribe(purpose_name, *events, &block)
unless (purpose = @purpose[purpose_name.to_sym])
Kindergarten.warning "No such purpose has been loaded: #{purpose_name}"
return
end
events.each do |event|
purpose._subscribe(event, &block)
end
end
|
#unguarded(&block) ⇒ Object
71
72
73
74
75
|
# File 'lib/kindergarten/sandbox.rb', line 71
def unguarded(&block)
@unguarded = true
yield
@unguarded = false
end
|
#unguarded? ⇒ Boolean
77
78
79
|
# File 'lib/kindergarten/sandbox.rb', line 77
def unguarded?
@unguarded == true ? true : false
end
|
#unsubscribe(purpose_name, *events) ⇒ Object
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/kindergarten/sandbox.rb', line 108
def unsubscribe(purpose_name, *events)
unless (purpose = @purpose[purpose_name.to_sym])
Kindergarten.warning "No such purpose has been loaded: #{purpose_name}"
return
end
events.each do |event|
purpose._unsubscribe(event)
end
end
|