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

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

Instance Method Summary collapse

Instance Method Details

#phlex(obj, content_type: nil, stream: false) ⇒ Object

Renders a Phlex object.

Parameters:

  • obj (Phlex::SGML)

    The Phlex object to be rendered.

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/roda/plugins/phlex.rb', line 144

def phlex(obj, 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

  phlex_opts = opts[:phlex]
  renderer = if (layout = phlex_opts[:layout])
    phlex_layout_handler.call(layout, phlex_opts[:layout_opts], obj)
  else
    obj
  end

  if stream
    self.stream do |out|
      renderer.call(out, view_context: self)
    end
  else
    renderer.call(view_context: self)
  end
end

#phlex_layout(layout = Undefined) ⇒ Phlex::SGML?

Retrieves or sets the layout.

Parameters:

  • layout (Phlex::SGML, Undefined, nil) (defaults to: Undefined)

    The layout to be set.

Returns:

  • (Phlex::SGML, nil)

    The current layout or nil if not set.



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/roda/plugins/phlex.rb', line 98

def phlex_layout(layout = Undefined)
  case layout
  when Undefined
    opts.dig(:phlex, :layout)
  when nil
    opts[:phlex].delete(:layout)
    opts[:phlex].delete(:layout_opts)
  when ::Phlex::SGML
    opts[:phlex][:layout] = layout
  else
    raise TypeError.new(layout)
  end
end

#phlex_layout_handler(handler = Undefined) ⇒ #call?

Retrieves or sets the layout handler.

Parameters:

  • handler (#call, Undefined, nil) (defaults to: Undefined)

    The layout handler to be set.

Returns:

  • (#call, nil)

    The current layout handler or nil if not set.



129
130
131
132
133
134
135
136
137
138
# File 'lib/roda/plugins/phlex.rb', line 129

def phlex_layout_handler(handler = Undefined)
  case handler
  when Undefined
    opts.dig(:phlex, :layout_handler)
  when nil
    opts[:phlex].delete(:layout_handler)
  else
    opts[:phlex][:layout_handler] = handler
  end
end

#phlex_layout_opts(layout_opts = Undefined) ⇒ Object?

Retrieves or sets the layout options.

Parameters:

  • layout_opts (Undefined, nil) (defaults to: Undefined)

    The layout options to be set.

Returns:

  • (Object, nil)

    The current layout options or nil if not set.



115
116
117
118
119
120
121
122
123
124
# File 'lib/roda/plugins/phlex.rb', line 115

def phlex_layout_opts(layout_opts = Undefined)
  case layout_opts
  when Undefined
    opts.dig(:phlex, :layout_opts)
  when nil
    opts[:phlex].delete(:layout_opts)
  else
    opts[:phlex][:layout_opts] = layout_opts
  end
end