Class: Kindergarten::Perimeter
- Inherits:
-
Object
- Object
- Kindergarten::Perimeter
- Defined in:
- lib/kindergarten/perimeter.rb,
lib/kindergarten/exceptions.rb
Overview
A Perimeter is used to define the places where the child can play.
Defined Under Namespace
Classes: NoExposedMethods, NoPurpose, RestrictedMethodError, Unguarded
Class Attribute Summary collapse
-
.exposed_methods ⇒ Object
readonly
Returns the value of attribute exposed_methods.
-
.govern_proc ⇒ Object
readonly
Returns the value of attribute govern_proc.
Instance Attribute Summary collapse
-
#child ⇒ Object
readonly
Returns the value of attribute child.
-
#governess ⇒ Object
readonly
Returns the value of attribute governess.
-
#sandbox ⇒ Object
readonly
Returns the value of attribute sandbox.
Class Method Summary collapse
-
.expose(*list) ⇒ Object
(also: sandbox)
Defines a list of sandboxed methods.
-
.govern(&proc) ⇒ Object
Instruct the Governess how to govern this perimeter.
-
.governess(*klass) ⇒ Object
Get/set the governess of the perimeter.
-
.instance(child = nil, governess = nil) ⇒ Perimeter
Obtain an un-sandboxed instance for testing purposes.
-
.purpose(*purpose) ⇒ Object
Get/set the purpose of the perimeter.
-
.subscribe(purpose, event, block) ⇒ Object
Subscribe to an event from a given purpose.
- .subscriptions ⇒ Object
Instance Method Summary collapse
- #fire(event, payload = nil) ⇒ Object
-
#governed(method, unguarded = false, &block) ⇒ Object
Perform a block under the watchful eye off the governess.
-
#initialize(sandbox, governess) ⇒ Perimeter
constructor
A new instance of Perimeter.
-
#sandbox_methods ⇒ Array
List of sandbox methods.
Constructor Details
#initialize(sandbox, governess) ⇒ Perimeter
Returns a new instance of Perimeter.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/kindergarten/perimeter.rb', line 102 def initialize(sandbox, governess) if sandbox.is_a? Kindergarten::Sandbox @sandbox = sandbox @child = sandbox.child else @child = sandbox end @governess = governess unless @governess.nil? || self.class.govern_proc.nil? @governess.instance_eval(&self.class.govern_proc) end end |
Class Attribute Details
.exposed_methods ⇒ Object (readonly)
Returns the value of attribute exposed_methods.
24 25 26 |
# File 'lib/kindergarten/perimeter.rb', line 24 def exposed_methods @exposed_methods end |
.govern_proc ⇒ Object (readonly)
Returns the value of attribute govern_proc.
24 25 26 |
# File 'lib/kindergarten/perimeter.rb', line 24 def govern_proc @govern_proc end |
Instance Attribute Details
#child ⇒ Object (readonly)
Returns the value of attribute child.
92 93 94 |
# File 'lib/kindergarten/perimeter.rb', line 92 def child @child end |
#governess ⇒ Object (readonly)
Returns the value of attribute governess.
92 93 94 |
# File 'lib/kindergarten/perimeter.rb', line 92 def governess @governess end |
#sandbox ⇒ Object (readonly)
Returns the value of attribute sandbox.
92 93 94 |
# File 'lib/kindergarten/perimeter.rb', line 92 def sandbox @sandbox end |
Class Method Details
.expose(*list) ⇒ Object Also known as: sandbox
Defines a list of sandboxed methods
Can be called multiple times to grow the list.
42 43 44 45 |
# File 'lib/kindergarten/perimeter.rb', line 42 def expose(*list) @exposed_methods ||= [] @exposed_methods |= list end |
.govern(&proc) ⇒ Object
Instruct the Governess how to govern this perimeter
49 50 51 |
# File 'lib/kindergarten/perimeter.rb', line 49 def govern(&proc) @govern_proc = proc end |
.governess(*klass) ⇒ Object
Get/set the governess of the perimeter
59 60 61 |
# File 'lib/kindergarten/perimeter.rb', line 59 def governess(*klass) klass.any? ? @governess = klass[0] : @governess end |
.instance(child = nil, governess = nil) ⇒ Perimeter
Obtain an un-sandboxed instance for testing purposes
98 99 100 |
# File 'lib/kindergarten/perimeter.rb', line 98 def self.instance(child=nil, governess=nil) self.new(child, governess) end |
.purpose(*purpose) ⇒ Object
Get/set the purpose of the perimeter
54 55 56 |
# File 'lib/kindergarten/perimeter.rb', line 54 def purpose(*purpose) purpose.any? ? @purpose = purpose[0] : @purpose end |
.subscribe(purpose, event, block) ⇒ Object
Subscribe to an event from a given purpose
80 81 82 83 84 85 |
# File 'lib/kindergarten/perimeter.rb', line 80 def subscribe(purpose, event, block) @callbacks ||= {} @callbacks[purpose] ||= {} @callbacks[purpose][event] ||= [] @callbacks[purpose][event] << block end |
.subscriptions ⇒ Object
87 88 89 |
# File 'lib/kindergarten/perimeter.rb', line 87 def subscriptions @callbacks ||= {} end |
Instance Method Details
#fire(event, payload = nil) ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/kindergarten/perimeter.rb', line 138 def fire(event, payload=nil) if @sandbox.nil? Kindergarten.warning("There is no sandbox, is this a test-perimeter?") return end @sandbox.purpose[self.class.purpose].fire(event, payload) end |
#governed(method, unguarded = false, &block) ⇒ Object
Perform a block under the watchful eye off the governess
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/kindergarten/perimeter.rb', line 126 def governed(method, unguarded=false, &block) if unguarded == true self.governess.unguarded do self.governess.governed(method, &block) end else self.governess.governed(method, &block) end end |
#sandbox_methods ⇒ Array
Returns List of sandbox methods.
121 122 123 |
# File 'lib/kindergarten/perimeter.rb', line 121 def sandbox_methods self.class.exposed_methods end |