Class: Kindergarten::Purpose

Inherits:
Object
  • Object
show all
Defined in:
lib/kindergarten/purpose.rb

Overview

Keep track of a single purpose

Constant Summary collapse

RESTRICTED_METHOD_NAMES =
[ :_subscribe, :fire, :add_perimeter,
  :initialize,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sandbox) ⇒ Purpose

Returns a new instance of Purpose.



10
11
12
13
14
15
# File 'lib/kindergarten/purpose.rb', line 10

def initialize(name, sandbox)
  @name          = name
  @sandbox       = sandbox
  @methods       = {}
  @subscriptions = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kindergarten/purpose.rb', line 58

def method_missing(name, *args, &block)
  super

rescue NoMethodError => ex
  unless methods.has_key?(name)
    raise ex
  end

  perimeter = methods[name]
  perimeter.governed(name, sandbox.unguarded?) do
    perimeter.send(name, *args, &block)
  end
end

Instance Attribute Details

#methodsObject (readonly)

Returns the value of attribute methods.



4
5
6
# File 'lib/kindergarten/purpose.rb', line 4

def methods
  @methods
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/kindergarten/purpose.rb', line 4

def name
  @name
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



4
5
6
# File 'lib/kindergarten/purpose.rb', line 4

def sandbox
  @sandbox
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



4
5
6
# File 'lib/kindergarten/purpose.rb', line 4

def subscriptions
  @subscriptions
end

Instance Method Details

#_subscribe(event, &block) ⇒ Object



37
38
39
40
# File 'lib/kindergarten/purpose.rb', line 37

def (event, &block)
  @subscriptions[event] ||= []
  @subscriptions[event] << block
end

#_unsubscribe(event) ⇒ Object



42
43
44
# File 'lib/kindergarten/purpose.rb', line 42

def _unsubscribe(event)
  @subscriptions.delete(event)
end

#add_perimeter(perimeter, instance) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kindergarten/purpose.rb', line 17

def add_perimeter(perimeter, instance)
  if perimeter.exposed_methods.blank?
    raise Kindergarten::Perimeter::NoExposedMethods.new(perimeter)
  end

  perimeter.exposed_methods.each do |name|
    if RESTRICTED_METHOD_NAMES.include?(name)
      raise(
        Kindergarten::Perimeter::RestrictedMethodError.new(perimeter, name)
      )

    elsif @methods.has_key?(name)

      Kindergarten.warning "overriding already sandboxed method #{@name}.#{name}"
    end

    @methods[name] = instance
  end
end

#fire(event_name, payload = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kindergarten/purpose.rb', line 46

def fire(event_name, payload=nil)
  event = Kindergarten::Event.new(event_name, self.name, payload)

  if @subscriptions.has_key?(event_name)
    @subscriptions[event_name].each do |proc|
      proc.call(event, self)
    end
  end

  self.sandbox.broadcast!(event)
end