Class: Cybertrain::FormatCollector

Inherits:
Object
  • Object
show all
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

Instance Method Details

#any { ... } ⇒ nil

Yields:

  • runs for any registered format no declaration before it matched

Returns:

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

Yields:

  • runs when the request asks for CSV

Returns:

  • (nil)


54
55
56
# File 'cybertrain/format_collector.rb', line 54

def csv(&block)
  arm(:csv, block)
end

#html { ... } ⇒ nil

Yields:

  • runs when the request asks for HTML

Returns:

  • (nil)


33
34
35
# File 'cybertrain/format_collector.rb', line 33

def html(&block)
  arm(:html, block)
end

#json { ... } ⇒ nil

Yields:

  • runs when the request asks for JSON

Returns:

  • (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.

Returns:

  • (Symbol)


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).

Returns:

  • (Boolean)


92
93
94
# File 'cybertrain/format_collector.rb', line 92

def matched_without_block?
  @matched_without_block
end

#text { ... } ⇒ nil

Yields:

  • runs when the request asks for plain text (.txt, .text)

Returns:

  • (nil)


68
69
70
# File 'cybertrain/format_collector.rb', line 68

def text(&block)
  arm(:text, block)
end

#turbo_stream { ... } ⇒ nil

Yields:

  • runs when the request asks for a Turbo Stream

Returns:

  • (nil)


47
48
49
# File 'cybertrain/format_collector.rb', line 47

def turbo_stream(&block)
  arm(:turbo_stream, block)
end

#xml { ... } ⇒ nil

Yields:

  • runs when the request asks for XML

Returns:

  • (nil)


61
62
63
# File 'cybertrain/format_collector.rb', line 61

def xml(&block)
  arm(:xml, block)
end