Module: Roda::RodaPlugins::Phlex::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#phlex(obj, layout: phlex_layout, context: phlex_context, content_type: nil, stream: false) ⇒ Object

Renders a Phlex object.

Parameters:

  • obj (Phlex::SGML)

    The Phlex object to be rendered.

  • layout (Class, nil) (defaults to: phlex_layout)

    The layout to be used for rendering. Defaults to the layout set by #phlex_layout.

  • context (Hash, nil) (defaults to: phlex_context)

    The Phlex context to be used for rendering. Defaults to the context set by #phlex_context.

  • content_type (String, nil) (defaults to: nil)

    The content type of the response.

  • stream (Boolean) (defaults to: false)

    Whether to stream the response or not.

Raises:



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/roda/plugins/phlex.rb', line 186

def phlex(obj, layout: phlex_layout, context: phlex_context, content_type: nil, stream: false)
  raise TypeError.new(obj) unless obj.is_a?(::Phlex::SGML)

  content_type ||= "image/svg+xml" if obj.is_a?(::Phlex::SVG)
  response["Content-Type"] = content_type if content_type

  renderer = if layout
    phlex_layout_handler.call(layout, phlex_layout_opts, obj)
  else
    obj
  end

  opts = {view_context: self}
  if context
    opts[:context] = if context.is_a?(::Phlex::Context)
      context
    else
      ::Phlex::Context.new(context)
    end
  end

  if stream
    self.stream do |out|
      renderer.call(out, **opts)
    end
  else
    renderer.call(**opts)
  end
end

#phlex_contextHash?

Retrieves the Phlex context.

Returns:

  • (Hash, nil)

    The current Phlex context.



166
167
168
169
# File 'lib/roda/plugins/phlex.rb', line 166

def phlex_context
  return @_phlex_context if defined?(@_phlex_context)
  @phlex_context = opts.dig(:phlex, :context).dup
end

#phlex_layoutClass?

Retrieves the layout class.

Returns:

  • (Class, nil)

    The current layout (a +Phlex::SGML+ class) or nil if not set.



104
105
106
107
# File 'lib/roda/plugins/phlex.rb', line 104

def phlex_layout
  return @_phlex_layout if defined?(@_phlex_layout)
  @_phlex_layout = opts.dig(:phlex, :layout)
end

#phlex_layout_handler#call

Retrieves the layout handler.

Returns:

  • (#call)

    The current layout handler.



148
149
150
151
# File 'lib/roda/plugins/phlex.rb', line 148

def phlex_layout_handler
  return @_phlex_layout_handler if defined?(@_phlex_layout_handler)
  @_phlex_layout_handler = opts.dig(:phlex, :layout_handler)
end

#phlex_layout_optsObject?

Note:

Layout options set via the plugin configuration will get deped for the current request. Be aware that this is a shallow copy and changes to nested objects will affect the original object, that is all subsequent requests. This is unsafe and should be avoided:

plugin :phlex, layout_opts: {key: {nested: "value"}}
# ...
# UNSAFE: Changes to phlex_layout_opts[:key] will affect the plugin config.
phlex_layout_opts[:key][:nested] = "other value"

Retrieves the layout options hash.

Returns:

  • (Object, nil)

    The current layout options or nil if not set.



133
134
135
136
# File 'lib/roda/plugins/phlex.rb', line 133

def phlex_layout_opts
  return @_phlex_layout_opts if defined?(@_phlex_layout_opts)
  @_phlex_layout_opts = opts.dig(:phlex, :layout_opts).dup
end

#set_phlex_context(context) ⇒ Hash

Sets the Phlex context.

Parameters:

  • context (Hash)

    The Phlex context to be set.

Returns:

  • (Hash)

    The Phlex context that was set.



174
175
176
# File 'lib/roda/plugins/phlex.rb', line 174

def set_phlex_context(context)
  @_phlex_context = context
end

#set_phlex_layout(layout) ⇒ Class?

Sets the layout class.

Parameters:

  • layout (Class, nil)

    The layout class to be set.

Returns:

  • (Class, nil)

    The layout class that was set.



113
114
115
116
117
118
119
# File 'lib/roda/plugins/phlex.rb', line 113

def set_phlex_layout(layout)
  if !layout || layout <= ::Phlex::SGML
    @_phlex_layout = layout
  else
    raise TypeError.new(layout)
  end
end

#set_phlex_layout_handler(handler) ⇒ Object

Sets the layout handler. Use +nil+ or +:default: to reset the layout handler to the DEFAULT_LAYOUT_HANDLER.



155
156
157
158
159
160
161
162
# File 'lib/roda/plugins/phlex.rb', line 155

def set_phlex_layout_handler(handler)
  @_phlex_layout_handler = case handler
  when nil, :default
    DEFAULT_LAYOUT_HANDLER
  else
    handler
  end
end

#set_phlex_layout_opts(layout_opts) ⇒ Object?

Note:

The DEFAULT_LAYOUT_HANDLER expects +layout_opts+ to be a +Hash+.

Sets the layout options.

Parameters:

  • layout_opts (Object, nil)

    The layout options to be set.

Returns:

  • (Object, nil)

    The layout options that were set.



142
143
144
# File 'lib/roda/plugins/phlex.rb', line 142

def set_phlex_layout_opts(layout_opts)
  @_phlex_layout_opts = layout_opts
end