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.

  • ‘: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.

    • ‘:all`: Delegate all methods the Roda app responds to, to it. Be careful with this option.

      It can lead to unexpected behavior if the Roda app has methods that conflict with Phlex methods.
      
    • ‘Symbol`, `String`, `Array`: Delegate only the specified methods to the Roda app.

Defined Under Namespace

Modules: InstanceMethods Classes: TypeError

Constant Summary collapse

VERSION =
"0.1.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.

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.



54
55
56
57
58
59
60
61
62
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
# File 'lib/roda/plugins/phlex.rb', line 54

def self.configure(app, opts = OPTS)
  delegate = opts.key?(:delegate) ? opts.delete(:delegate) : true
  app.opts[:phlex] = opts
  app.opts[:phlex][:layout_handler] ||= DEFAULT_LAYOUT_HANDLER

  if delegate
    overrides = Module.new do
      def app
        @_view_context
      end

      case delegate
      when :all
        def method_missing(name, ...)
          if app.respond_to?(name)
            app.send(name, ...)
          else
            super
          end
        end

        def respond_to_missing?(name, include_private = false)
          app.respond_to?(name) || super
        end

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

    ::Phlex::SGML.include(overrides)
  end
end