Module: Roda::RodaPlugins::Phlex::InstanceMethods
- Defined in:
- lib/roda/plugins/phlex.rb
Instance Method Summary collapse
-
#phlex(obj, layout: phlex_layout, context: phlex_context, content_type: nil, stream: false) ⇒ Object
Renders a Phlex object.
-
#phlex_context ⇒ Hash?
Retrieves the Phlex context.
-
#phlex_layout ⇒ Class?
Retrieves the layout class.
-
#phlex_layout_handler ⇒ #call
Retrieves the layout handler.
-
#phlex_layout_opts ⇒ Object?
Retrieves the layout options hash.
-
#set_phlex_context(context) ⇒ Hash
Sets the Phlex context.
-
#set_phlex_layout(layout) ⇒ Class?
Sets the layout class.
-
#set_phlex_layout_handler(handler) ⇒ Object
Sets the layout handler.
-
#set_phlex_layout_opts(layout_opts) ⇒ Object?
Sets the layout options.
Instance Method Details
#phlex(obj, layout: phlex_layout, context: phlex_context, content_type: nil, stream: false) ⇒ Object
Renders a Phlex object.
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_context ⇒ Hash?
Retrieves the 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_layout ⇒ Class?
Retrieves the layout class.
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.
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_opts ⇒ Object?
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.
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.
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.
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?
The DEFAULT_LAYOUT_HANDLER expects +layout_opts+ to be a +Hash+.
Sets the layout options.
142 143 144 |
# File 'lib/roda/plugins/phlex.rb', line 142 def set_phlex_layout_opts(layout_opts) @_phlex_layout_opts = layout_opts end |