class Pakyow::Plugin

Base plugin class.

@api private

Attributes

mount_path[R]
plugin_name[R]
plugin_path[R]
parent[R]

Public Class Methods

_load(state) click to toggle source

@api private

# File lib/pakyow/plugin.rb, line 193
def self._load(state)
  state = Marshal.load(state)
  Pakyow.app(state[:parent][:name]).plugs.find { |plug|
    plug.class.plugin_name == state[:plugin_name] &&
      plug.class.plugin_path == state[:plugin_path] &&
      plug.class.mount_path == state[:mount_path]
  }
end
new(parent, &block) click to toggle source
# File lib/pakyow/plugin.rb, line 77
def initialize(parent, &block)
  super()

  @parent = parent
  @state = []
  @endpoints = Endpoints.new
  @features = self.class.features
  @key = build_key

  performing :configure do
    configure!(@parent.environment)
  end

  performing :initialize do
    if block_given?
      instance_exec(&block)
    end

    # Load state prior to calling the load hooks so that helpers are available.
    #
    load_state

    # We still want to call the load hooks so that behavior works properly.
    #
    performing :load do; end

    defined!
  end

  create_helper_contexts

  if respond_to?(:boot)
    boot
  end
end

Private Class Methods

Plugin(name, path) click to toggle source

rubocop:disable Naming/MethodName

# File lib/pakyow/plugin.rb, line 319
def Plugin(name, path)
  Class.new(self) do
    @plugin_name = name
    @plugin_path = path
  end
end
disable(*features) click to toggle source
# File lib/pakyow/plugin.rb, line 344
def disable(*features)
  @__disabled_features.concat(features)
end
enable(*features) click to toggle source
# File lib/pakyow/plugin.rb, line 340
def enable(*features)
  @__enabled_features.concat(features)
end
features() click to toggle source
# File lib/pakyow/plugin.rb, line 348
def features
  Dir.glob(File.join(plugin_path, "features", "*")).map { |feature_path|
    {
      name: File.basename(feature_path).to_sym,
      path: feature_path
    }
  }.tap do |features|
    features.delete_if do |feature|
      @__disabled_features.include?(feature[:name])
    end

    if @__enabled_features.any?
      features.keep_if do |feature|
        @__enabled_features.include?(feature[:name])
      end
    end
  end
end
inherited(plugin_class) click to toggle source

rubocop:enabled Naming/MethodName

Calls superclass method
# File lib/pakyow/plugin.rb, line 327
def inherited(plugin_class)
  super

  if instance_variable_defined?(:@plugin_name)
    plugin_class.instance_variable_set(:@plugin_name, instance_variable_get(:@plugin_name))
    plugin_class.instance_variable_set(:@plugin_path, instance_variable_get(:@plugin_path))

    unless Pakyow.plugins.include?(@plugin_name)
      Pakyow.register_plugin(@plugin_name, plugin_class)
    end
  end
end

Public Instance Methods

__object_name() click to toggle source

@api private

# File lib/pakyow/plugin.rb, line 145
def __object_name
  self.class.__object_name
end
_dump(_) click to toggle source

@api private

# File lib/pakyow/plugin.rb, line 178
def _dump(_)
  Marshal.dump(
    {
      parent: {
        name: @parent.config.name
      },

      plugin_name: self.class.plugin_name,
      plugin_path: self.class.plugin_path,
      mount_path: self.class.mount_path
    }
  )
end
booted() click to toggle source
# File lib/pakyow/plugin.rb, line 113
def booted
  call_hooks :after, :boot
end
call(connection) click to toggle source
Calls superclass method
# File lib/pakyow/plugin.rb, line 124
def call(connection)
  super(isolated(:Connection).from_connection(connection, :@app => self))
end
exposed_value_name(name) click to toggle source
# File lib/pakyow/plugin.rb, line 167
def exposed_value_name(name)
  prefix = if self.class.__object_name.name == :default
    self.class.plugin_name
  else
    "#{self.class.plugin_name}(#{self.class.__object_name.name})"
  end

  :"__#{prefix}.#{name}"
end
feature?(name) click to toggle source
# File lib/pakyow/plugin.rb, line 117
def feature?(name)
  name = name.to_sym
  @features.any? { |feature|
    feature[:name] == name
  }
end
frontend_key(name = nil) click to toggle source
# File lib/pakyow/plugin.rb, line 202
def frontend_key(name = nil)
  if name
    :"@#{@key}.#{name}"
  else
    @key
  end
end
helper_caller(helper_context, connection, call_context) click to toggle source
# File lib/pakyow/plugin.rb, line 153
def helper_caller(helper_context, connection, call_context)
  connection = connection.class.from_connection(connection, :@app => self)

  HelperCaller.new(
    plugin: self,
    connection: connection,
    helpers: @helper_contexts[helper_context.to_sym].new(connection, call_context)
  )
end
helpers(connection) click to toggle source
# File lib/pakyow/plugin.rb, line 140
def helpers(connection)
  @helper_class.new(self, connection)
end
load_frontend() click to toggle source
# File lib/pakyow/plugin.rb, line 163
def load_frontend
  @state.each(&:load_frontend)
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/pakyow/plugin.rb, line 128
def method_missing(method_name, *args, &block)
  if @parent.respond_to?(method_name)
    @parent.public_send(method_name, *args, &block)
  else
    super
  end
end
plugin_path() click to toggle source
# File lib/pakyow/plugin.rb, line 149
def plugin_path
  self.class.plugin_path
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/pakyow/plugin.rb, line 136
def respond_to_missing?(method_name, include_private = false)
  @parent.respond_to?(method_name) || super
end
top() click to toggle source
# File lib/pakyow/plugin.rb, line 210
def top
  parent.top
end

Private Instance Methods

build_key() click to toggle source
# File lib/pakyow/plugin.rb, line 216
def build_key
  namespace = self.class.__object_name.namespace.parts.last
  @key = case namespace
  when :default
    :"#{self.class.plugin_name}"
  else
    :"#{self.class.plugin_name}.#{namespace}"
  end
end
create_helper_contexts() click to toggle source
# File lib/pakyow/plugin.rb, line 290
def create_helper_contexts
  @helper_contexts = %i(global passive active).each_with_object({}) { |context, helper_contexts|
    helper_class = Class.new do
      def initialize(connection, context)
        @connection, @context = connection, context
      end

      def method_missing(method_name, *args, &block)
        if @context.respond_to?(method_name)
          @context.public_send(method_name, *args, &block)
        else
          super
        end
      end

      def respond_to_missing?(method_name, include_private = false)
        @context.respond_to?(method_name, include_private) || super
      end
    end

    self.class.include_helpers(context, helper_class)
    helper_contexts[context] = helper_class
  }
end
define_app_endpoints() click to toggle source
# File lib/pakyow/plugin.rb, line 262
def define_app_endpoints
  @endpoints.each do |endpoint|
    # Register endpoints accessible for backend path building.
    #
    @parent.endpoints << Endpoint.new(
      name: [config.name.to_s, endpoint.name].join("_"),
      method: endpoint.method,
      builder: endpoint.builder
    )

    # Register endpoints accessible for frontend path building.
    #
    namespace = self.class.__object_name.namespace.parts.last

    endpoint_name = if namespace == :default
      :"@#{self.class.plugin_name}.#{endpoint.name}"
    else
      :"@#{self.class.plugin_name}(#{namespace}).#{endpoint.name}"
    end

    @parent.endpoints << Endpoint.new(
      name: endpoint_name,
      method: endpoint.method,
      builder: endpoint.builder
    )
  end
end
load_aspect(aspect, **) click to toggle source
# File lib/pakyow/plugin.rb, line 226
def load_aspect(aspect, **)
  @state.each do |state|
    super(aspect, path: state.backend_path(aspect), target: self)
  end
end
load_endpoints() click to toggle source
# File lib/pakyow/plugin.rb, line 252
def load_endpoints
  state.each_with_object(@endpoints) do |(_, state_object), endpoints|
    state_object.instances.each do |state_instance|
      endpoints.load(state_instance)
    end
  end

  define_app_endpoints
end
load_feature_state() click to toggle source
# File lib/pakyow/plugin.rb, line 241
def load_feature_state
  @features.each do |feature|
    @state << State.new(self, path: feature[:path])

    initializer = File.join(feature[:path], "initializer.rb")
    if File.exist?(initializer)
      instance_eval(File.read(initializer), initializer)
    end
  end
end
load_global_state() click to toggle source
# File lib/pakyow/plugin.rb, line 237
def load_global_state
  @state << State.new(self)
end
load_state() click to toggle source
# File lib/pakyow/plugin.rb, line 232
def load_state
  load_global_state
  load_feature_state
end