Class: Stasis::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/stasis/scope.rb

Direct Known Subclasses

Action, Controller

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_stasisObject

‘Stasis`.



5
6
7
# File 'lib/stasis/scope.rb', line 5

def _stasis
  @_stasis
end

Instance Method Details

#_bind_plugin(plugin, type) ⇒ Object

Plugins use the ‘controller_method` and `action_method` class methods to bind plugin methods to a scope instance. This method does the binding.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stasis/scope.rb', line 9

def _bind_plugin(plugin, type)
  _each_plugin_method(plugin, type) do |plugin, method, real_method|
    self.instance_eval <<-EVAL
      # Define a method on `self` (the `Scope` instance).
      def #{method}(*args, &block)
        # Find the plugin.
        plugin = self._stasis.plugins.detect do |plugin|
          plugin.to_s == "#{plugin.to_s}"
        end
        # Pass parameters to the method on the plugin.
        plugin.send(:#{real_method}, *args, &block)
      end
    EVAL
  end
end

#_each_plugin_method(plugin, type, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/stasis/scope.rb', line 25

def _each_plugin_method(plugin, type, &block)
  # Retrieve plugin `methods`: a `Hash` whose keys are the method name to bind to
  # `self`, and whose values are the method name on the `Plugin` class we are
  # binding from.
  methods = plugin.class._methods ? plugin.class._methods[type] : nil
  methods ||= {}
  methods.each do |method, real_method|
    yield(plugin, method, real_method)
  end
end

#_each_plugins_method(type, &block) ⇒ Object



36
37
38
39
40
41
# File 'lib/stasis/scope.rb', line 36

def _each_plugins_method(type, &block)
  # For each plugin...
  _stasis.plugins.each do |plugin|
    _each_plugin_method(plugin, type, &block)
  end
end

#_send_to_plugin(priority, type) ⇒ Object

Using all ‘Plugin` instances of a certain priority, call methods of a certain type.



44
45
46
47
48
49
50
51
52
# File 'lib/stasis/scope.rb', line 44

def _send_to_plugin(priority, type)
  _each_plugins_method(type) do |plugin, method, real_method|
    # If priority matches and plugin responds to method...
    if plugin.class._priority == priority && plugin.respond_to?(real_method)
      # Call plugin method.
      plugin.send(real_method)
    end
  end
end