Class: Cybertrain::FormatCollector
- Inherits:
-
Object
- Object
- Cybertrain::FormatCollector
- Defined in:
- cybertrain/format_collector.rb
Overview
What Controller#respond_to yields: one method per format, each a declaration that this action answers it.
respond_to do |format|
format.html
format.json { render json: @article.as_json }
format.turbo_stream
end
The request’s format is known before the action runs, so the first
declaration that matches it runs its block at once, as it is declared;
the declarations after it do nothing. No block is ever stored (spikes/
NOTES.md rule 88). A declaration without a block leaves the render to
Controller#respond_to: the action’s template for that format. When
nothing matched, respond_to answers 406 Not Acceptable.
any matches whatever format the request has, if no declaration before
it matched; not a format whose extension is unregistered (:unknown,
see Request#format), which only 406 answers, as in Rails.
Instance Method Summary collapse
-
#any { ... } ⇒ nil
-
#csv { ... } ⇒ nil
-
#html { ... } ⇒ nil
-
#json { ... } ⇒ nil
-
#matched_format ⇒ Symbol
The format whose declaration matched,
:noneuntil one did. -
#matched_without_block? ⇒ Boolean
Whether the matching declaration came without a block (the action’s template for that format is then rendered).
-
#text { ... } ⇒ nil
-
#turbo_stream { ... } ⇒ nil
-
#xml { ... } ⇒ nil
Instance Method Details
#any { ... } ⇒ nil
75 76 77 78 79 |
# File 'cybertrain/format_collector.rb', line 75 def any(&block) return nil if @format == :unknown arm(@format, block) end |
#csv { ... } ⇒ nil
54 55 56 |
# File 'cybertrain/format_collector.rb', line 54 def csv(&block) arm(:csv, block) end |
#html { ... } ⇒ nil
33 34 35 |
# File 'cybertrain/format_collector.rb', line 33 def html(&block) arm(:html, block) end |
#json { ... } ⇒ nil
40 41 42 |
# File 'cybertrain/format_collector.rb', line 40 def json(&block) arm(:json, block) end |
#matched_format ⇒ Symbol
The format whose declaration matched, :none until one did.
84 85 86 |
# File 'cybertrain/format_collector.rb', line 84 def matched_format @matched_format end |
#matched_without_block? ⇒ Boolean
Whether the matching declaration came without a block (the action’s template for that format is then rendered).
92 93 94 |
# File 'cybertrain/format_collector.rb', line 92 def matched_without_block? @matched_without_block end |
#text { ... } ⇒ nil
68 69 70 |
# File 'cybertrain/format_collector.rb', line 68 def text(&block) arm(:text, block) end |
#turbo_stream { ... } ⇒ nil
47 48 49 |
# File 'cybertrain/format_collector.rb', line 47 def turbo_stream(&block) arm(:turbo_stream, block) end |
#xml { ... } ⇒ nil
61 62 63 |
# File 'cybertrain/format_collector.rb', line 61 def xml(&block) arm(:xml, block) end |