Sha256: 569bce47194215e585758460cb30631de9f3d69e891a719083541dc8836e4bd4

Contents?: true

Size: 1.9 KB

Versions: 17

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

17 entries across 17 versions & 1 rubygems

Version Path
lolita-4.0.11 lib/lolita/extensions/extensions.rb
lolita-4.2.0 lib/lolita/extensions/extensions.rb
lolita-4.0.10 lib/lolita/extensions/extensions.rb
lolita-4.0.9 lib/lolita/extensions/extensions.rb
lolita-4.0.8 lib/lolita/extensions/extensions.rb
lolita-4.0.7 lib/lolita/extensions/extensions.rb
lolita-4.0.6 lib/lolita/extensions/extensions.rb
lolita-4.0.5 lib/lolita/extensions/extensions.rb
lolita-4.0.4 lib/lolita/extensions/extensions.rb
lolita-4.0.3 lib/lolita/extensions/extensions.rb
lolita-4.0.2 lib/lolita/extensions/extensions.rb
lolita-4.0.1 lib/lolita/extensions/extensions.rb
lolita-4.0.0 lib/lolita/extensions/extensions.rb
lolita-3.4.3 lib/lolita/extensions/extensions.rb
lolita-3.4.2 lib/lolita/extensions/extensions.rb
lolita-3.4.1 lib/lolita/extensions/extensions.rb
lolita-3.4.0 lib/lolita/extensions/extensions.rb