Module: Innate::Helper::Aspect

### Home Classes Methods

### In Files

- lib/innate/helper/aspect.rb

### Namespace

- MODULE Innate::Helper::Aspect::SingletonMethods

### Methods

- ::ancestral_aop
- ::included
- #aspect_call
- #aspect_wrap
- #wrap_action_call

### Class/Module Index

Quicksearch

- Innate
- Innate::Action
- Innate::Adapter
- Innate::Cache
- Innate::Cache::API
- Innate::Cache::DRb
- Innate::Cache::FileBased
- Innate::Cache::Marshal
- Innate::Cache::Memory
- Innate::Cache::YAML
- Innate::Current
- Innate::Etanni
- Innate::Helper
- Innate::Helper::Aspect
- Innate::Helper::Aspect::SingletonMethods
- Innate::Helper::CGI
- Innate::Helper::Flash
- Innate::Helper::Link
- Innate::Helper::Redirect
- Innate::Helper::Render
- Innate::HelperAccess
- Innate::HelpersHelper
- Innate::LRUHash
- Innate::LogHub
- Innate::Mock
- Innate::Mock::Session
- Innate::Node
- Innate::Optioned
- Innate::Optioned::SingletonMethods
- Innate::Options
- Innate::Request
- Innate::Response
- Innate::Rewrite
- Innate::Route
- Innate::Session
- Innate::Session::Flash
- Innate::SingletonMethods
- Innate::StateAccessor
- Innate::Traited
- Innate::Trinity
- Innate::URLMap
- Innate::View
- Innate::View::ERB
- Innate::View::Etanni
- Innate::View::None
- Logger
- Logger::ColorFormatter
- Object

No matching classes.

# Innate::Helper::Aspect

The Aspect helper allows you to execute hooks before or after a number of actions.

See {Innate::Helper::Aspect::SingletonMethods} for more details on the various hooks available.

@example Querying a database before a number of actions.

    class Posts
      include Innate::Node

      map    '/'
      helper :aspect

      before(:index, :other) do
        @posts = Post.all
      end

      def index
        return @posts
      end

      def other
        return @posts[0]
      end
    end

This helper is essential for proper working of {Action#render}.

### Constants

AOP

  Hash containing the various hooks to call for certain actions.

### Public Class Methods

ancestral_aop(from) click to toggle source

Consider objects that have Aspect included

    # File lib/innate/helper/aspect.rb, line 49
    def self.ancestral_aop(from)
      aop = {}

      from.ancestors.reverse.each do |anc|
        aop.merge!(AOP[anc]) if anc < Aspect
      end

      aop
    end

included(into) click to toggle source

Called whenever this helper is included into a class.

@param [Class] into The class the module was included into.

    # File lib/innate/helper/aspect.rb, line 43
    def self.included(into)
      into.extend(SingletonMethods)
      into.add_action_wrapper(5.0, :aspect_wrap)
    end

### Public Instance Methods

aspect_call(position, name) click to toggle source

Calls the aspect for a given position and name.

@param [to_sym] position The position of the hook, e.g. :before_all. @param [to_sym] name The name of the method for which to call the hook.

    # File lib/innate/helper/aspect.rb, line 65
    def aspect_call(position, name)
      return unless aop = Aspect.ancestral_aop(self.class)
      return unless block = at_position = aop[position]

      block = at_position[name.to_sym] unless at_position.is_a?(Proc)

      instance_eval(&block) if block
    end

aspect_wrap(action) click to toggle source

Wraps the specified action between various hooks.

@param [Innate::Action] action The action to wrap.

    # File lib/innate/helper/aspect.rb, line 79
    def aspect_wrap(action)
      return yield unless method = action.name

      aspect_call(:before_all, method)
      aspect_call(:before, method)
      result = yield
      aspect_call(:after, method)
      aspect_call(:after_all, method)

      result
    end

wrap_action_call(action, &block) click to toggle source

This awesome piece of hackery implements action AOP.

The so-called aspects are simply methods that may yield the next aspect in the chain, this is similar to racks concept of middleware, but instead of initializing with an app we simply pass a block that may be yielded with the action being processed.

This gives us things like logging, caching, aspects, authentication, etc.

Add the name of your method to the trait to add your own method to the wrap_action_call chain.

@example adding your method

    class MyNode
      Innate.node '/'

      private

      def wrap_logging(action)
        Innate::Log.info("Executing #{action.name}")
        yield
      end

      trait[:wrap]
    end

methods may register themself in the trait and will be called in left-to-right order, each being passed the action instance and a block that they have to yield to continue the chain.

@param [Action] action instance that is being passed to every registered

    method

@param [Proc] block contains the instructions to call the action method

    if any

@see Action#render @author manveru

    # File lib/innate/helper/aspect.rb, line 131
    def wrap_action_call(action, &block)
      return yield if action.options[:is_layout]
      wrap = SortedSet.new
      action.node.ancestral_trait_values(:wrap).each{|sset| wrap.merge(sset) }
      head, *tail = wrap.map{|k,v| v }
      tail.reverse!
      combined = tail.inject(block){|s,v| lambda{ __send__(v, action, &s) } }
      __send__(head, action, &combined)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.