Module: Roda::RodaPlugins::Phlex

Defined in:
lib/roda/plugins/phlex.rb,
lib/roda/phlex.rb

Overview

The Phlex Plugin provides functionality for integrating Phlex with Roda applications.

Phlex Plugin Options

  • :layout (+::Phlex::SGML+): Specifies the layout class to be used for rendering views. This class should be a Phlex layout class that defines how the views are structured and rendered.
  • :layout_opts (+Object+): Options that are passed to the layout class when it is instantiated. These options can be used to customize the behavior of the layout. Usually, this is a +Hash+.
  • :layout_handler (+#call+): A custom handler for creating layout instances. This proc receives three arguments: the layout class, the layout options, and the object to be rendered. By default, it uses the DEFAULT_LAYOUT_HANDLER, which instantiates the layout class with the provided object and options as keyword arguments.
  • :context (+Hash+): The context that is passed to the rendering call. (default: {})
  • :delegate: Define if or which methods should be delegated to the Roda app:
    • true (default): Create a single app method that delegates to the Roda app.
    • false: Do not create any delegate methods.
    • Array<Symbol,String>: Delegate the named methods to the Roda app.
  • :delegate_on: Class or module to define delegation methods on. Defaults to +::Phlex::SGML+.
    • Use this option to limit delegation methods to a application specific class or module (like "ApplicationView") to avoid polluting the global namespace.
  • :delegate_name: The name of the method that delegates to the Roda app. Defaults to "app".

Defined Under Namespace

Modules: InstanceMethods Classes: TypeError

Constant Summary collapse

VERSION =
"0.3.0"
Error =
Class.new(StandardError)
DEFAULT_LAYOUT_HANDLER =

The default layout handler for creating layout instances. Expects layout options to be a +Hash+ when provided. Layout options are passed as keyword arguments to the layout class.

proc do |layout, layout_opts, obj|
  layout_opts ? layout.new(obj, **layout_opts) : layout.new(obj)
end

Class Method Summary collapse

Class Method Details

.configure(app, opts = OPTS) ⇒ Object

Configures the Phlex plugin for the Roda application.

Parameters:

  • app (Roda)

    The Roda application.

  • opts (Hash) (defaults to: OPTS)

    The options for configuring the Phlex plugin.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/roda/plugins/phlex.rb', line 63

def self.configure(app, opts = OPTS)
  delegate = opts.fetch(:delegate, true)
  if delegate
    delegate_on = opts.fetch(:delegate_on) { ::Phlex::SGML }
    delegate_name = opts.fetch(:delegate_name, "app")

    warn sprintf(DELEGATE_ERROR_MESSAGE, "delegate_on", delegate_on.inspect) unless delegate_on
    warn sprintf(DELEGATE_ERROR_MESSAGE, "delegate_name", delegate_name.inspect) unless delegate_name
  end

  app.opts[:phlex] = opts.dup
  app.opts[:phlex][:layout_handler] ||= DEFAULT_LAYOUT_HANDLER
  app.opts[:phlex][:context] ||= {}

  if delegate && delegate_on && delegate_name
    delegate_mod = Module.new do
      class_eval <<~RUBY, __FILE__, __LINE__ + 1
        def #{delegate_name}
          @_view_context
        end
      RUBY

      case delegate
      when Array
        delegate.each do |delegate|
          class_eval <<~RUBY, __FILE__, __LINE__ + 1
            def #{delegate}(...)
              #{delegate_name}.#{delegate}(...)
            end
          RUBY
        end
      end
    end

    delegate_on.include(delegate_mod)
  end
end