Class: Kindergarten::Perimeter

Inherits:
Object
  • Object
show all
Defined in:
lib/kindergarten/perimeter.rb,
lib/kindergarten/exceptions.rb

Overview

A Perimeter is used to define the places where the child can play.

Examples:

class ExamplePerimeter < Kindergarten::Perimeter
  purpose :books

  govern do
    can :read, Book do |book|
      book.level <= 2
    end
  end

  def read(book)
    guard(:read, book)
    book.read
  end

  sandbox :read
end

Defined Under Namespace

Classes: NoExposedMethods, NoPurpose, RestrictedMethodError, Unguarded

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_methodsObject (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_procObject (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

#childObject (readonly)

Returns the value of attribute child.



92
93
94
# File 'lib/kindergarten/perimeter.rb', line 92

def child
  @child
end

#governessObject (readonly)

Returns the value of attribute governess.



92
93
94
# File 'lib/kindergarten/perimeter.rb', line 92

def governess
  @governess
end

#sandboxObject (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.

Examples:

class BondModule < Kindergarten::Perimeter
  # ...
  expose :m, :q

  # ...
  expose :enemies
end

BondModule.exposed_methods
=> [ :m, :q, :enemies ]


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

Returns:

  • (Perimeter)

    with the given child and/or governess



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

Examples:

Symbol form

subscribe :users, :create, :user_created

def user_created(event)
  # ...
end

Block form

subscribe :users, :update do |event|
  # ...
end

Parameters:

  • purpose (Symbol)

    Listen to other perimeters that have this purpose

  • event (Symbol)

    Listen for events with this name

  • block (Proc, Symbol)

    Invoke this on the event



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

.subscriptionsObject



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_methodsArray

Returns List of sandbox methods.

Returns:

  • (Array)

    List of sandbox methods



121
122
123
# File 'lib/kindergarten/perimeter.rb', line 121

def sandbox_methods
  self.class.exposed_methods
end