Sha256: c2eb3f9a327de27bc2599f31ef9169b027b13d9ada6a0d6f2bf02235c168e7a1

Contents?: true

Size: 1.9 KB

Versions: 19

Compression:

Stored size: 1.9 KB

Contents

module Lolita
  # This module provides functionality for extension machinery in Lolita. It loads proxies on demand.
  module Extensions
    EXTENSIONS = []
    
    def method_missing method_name, *args, &block
      if method_name[/_proxy$/]
        type = method_name.to_s.sub(/_proxy$/,"")
        self.class_eval <<-EXTENSION,__FILE__,__LINE__+1
          def #{method_name}(*args,&block)
            unless __extensions_proxies[:#{type}]
              load_extension_proxy(:#{type},self,__proxy_options_from_context)
            end
            __extensions_proxies[:#{type}]
          end
        EXTENSION
        send(method_name)
      else
        super
      end
    end

    def load_extensions_proxies(context=self, options={})
      EXTENSIONS.each do |type|
        load_extension_proxy(type,context,options)
      end
    end
    
    def load_extension_proxy type,context=self,options={}
      proxy_class = "Lolita::Extensions::#{type.to_s.camelize}::Proxy".constantize
      initialize_arity = proxy_class.instance_method(:initialize).arity
      __extensions_proxies[type] = if initialize_arity < 0 || initialize_arity > 1
        proxy_class.new(context,options)
      elsif initialize_arity == 0
        proxy_class.new
      else
        proxy_class.new(context)
      end
    end

    def self.add type
      EXTENSIONS << type unless EXTENSIONS.include?(type)
    end

    private

    def __proxy_options_from_context
      if defined?(::ActionController::Base) && self.is_a?(::ActionController::Base)
        {:controller => self, :request => request}
      elsif defined?(::ActionView::Base) && self.is_a?(::ActionView::Base)
        {:request => request}
      end
    end

    def __extensions_proxies 
      @proxies ||= {}
    end

  end
end

Lolita::Extensions.add :authentication
Lolita::Extensions.add :authorization

require 'lolita/extensions/authorization/proxy'
require 'lolita/extensions/authentication/proxy'

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
lolita-3.3.9 lib/lolita/extensions/extensions.rb
lolita-3.3.8 lib/lolita/extensions/extensions.rb
lolita-3.3.7 lib/lolita/extensions/extensions.rb
lolita-3.3.6 lib/lolita/extensions/extensions.rb
lolita-3.3.5 lib/lolita/extensions/extensions.rb
lolita-3.3.4 lib/lolita/extensions/extensions.rb
lolita-3.3.3 lib/lolita/extensions/extensions.rb
lolita-3.3.2 lib/lolita/extensions/extensions.rb
lolita-3.3.1 lib/lolita/extensions/extensions.rb
lolita-3.3.0 lib/lolita/extensions/extensions.rb
lolita-3.2.1 lib/lolita/extensions/extensions.rb
lolita-3.2.0.rc.17 lib/lolita/extensions/extensions.rb
lolita-3.2.0.rc.16 lib/lolita/extensions/extensions.rb
lolita-3.2.0.rc.14 lib/lolita/extensions/extensions.rb
lolita-3.2.0.rc.13 lib/lolita/extensions/extensions.rb
lolita-3.2.0.rc.12 lib/lolita/extensions/extensions.rb
lolita-3.2.0.rc.11 lib/lolita/extensions/extensions.rb
lolita-3.2.0.rc.10 lib/lolita/extensions/extensions.rb
lolita-3.2.0.rc.9 lib/lolita/extensions/extensions.rb