Class: Hanami::Action::Config::Formats Private
- Inherits:
-
Object
- Object
- Hanami::Action::Config::Formats
- Defined in:
- lib/hanami/action/config/formats.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Action format configuration.
Instance Attribute Summary collapse
-
#accepted ⇒ Object
The array of formats to accept requests by.
-
#default ⇒ Symbol?
Returns the default format name.
- #mapping ⇒ Object readonly private
Instance Method Summary collapse
- #accept(*formats) ⇒ Object private
-
#accept_types ⇒ Object
Returns an array of all accepted media types.
- #accept_types_for(format) ⇒ Object (also: #mime_types_for) private
- #accepted_formats(standard_formats = {}) ⇒ Object private
- #add(format, mime_types) ⇒ self
- #any? ⇒ Boolean private
-
#clear ⇒ self
Clears any previously added mappings and format values.
- #content_types_for(format) ⇒ Object private
- #empty? ⇒ Boolean private
-
#format_for(media_type) ⇒ Symbol, NilClass
Retrieve the format name associated with the given media type.
-
#initialize(accepted: [], default: nil, mapping: {}) ⇒ Formats
constructor
private
A new instance of Formats.
- #map(&blk) ⇒ Object private
-
#media_type_for(format) ⇒ String?
(also: #mime_type_for)
Returns the media type associated with the given format.
-
#register(format, media_type, accept_types: [media_type], content_types: [media_type]) ⇒ self
Registers a format and its associated media types.
- #values ⇒ Object
Constructor Details
#initialize(accepted: [], default: nil, mapping: {}) ⇒ Formats
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Formats.
62 63 64 65 66 |
# File 'lib/hanami/action/config/formats.rb', line 62 def initialize(accepted: [], default: nil, mapping: {}) @accepted = accepted @default = default @mapping = mapping end |
Instance Attribute Details
#accepted ⇒ Object
The array of formats to accept requests by.
28 29 30 |
# File 'lib/hanami/action/config/formats.rb', line 28 def accepted @accepted end |
#default ⇒ Symbol?
Returns the default format name.
When a request is received that cannot
58 59 60 |
# File 'lib/hanami/action/config/formats.rb', line 58 def default @default end |
#mapping ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
18 19 20 |
# File 'lib/hanami/action/config/formats.rb', line 18 def mapping @mapping end |
Instance Method Details
#accept(*formats) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
90 91 92 93 |
# File 'lib/hanami/action/config/formats.rb', line 90 def accept(*formats) self.default = formats.first if default.nil? self.accepted = accepted | formats end |
#accept_types ⇒ Object
Returns an array of all accepted media types.
231 232 233 |
# File 'lib/hanami/action/config/formats.rb', line 231 def accept_types accepted.map { |format| mapping[format]&.accept_types }.flatten(1).compact end |
#accept_types_for(format) ⇒ Object Also known as: mime_types_for
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
270 271 272 |
# File 'lib/hanami/action/config/formats.rb', line 270 def accept_types_for(format) mapping[format]&.accept_types || [] end |
#accepted_formats(standard_formats = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
96 97 98 99 100 101 102 103 |
# File 'lib/hanami/action/config/formats.rb', line 96 def accepted_formats(standard_formats = {}) accepted.to_h { |format| [ format, mapping.fetch(format) { standard_formats[format] } ] } end |
#add(format) ⇒ self #add(format, mime_type) ⇒ self #add(format, mime_types) ⇒ self
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/hanami/action/config/formats.rb', line 173 def add(format, mime_types) msg = " Hanami::Action `config.formats.add` is deprecated and will be removed in Hanami 2.4.\n\n Please use `config.formats.register` instead.\n\n See https://guides.hanamirb.org/v2.3/actions/formats-and-mime-types/ for details.\n TEXT\n warn(msg, category: :deprecated)\n\n mime_type = Array(mime_types).first\n\n # The old behaviour would have subsequent mime types _replacing_ previous ones\n mapping.reject! { |_, format| format.media_type == mime_type }\n\n register(format, Array(mime_types).first, accept_types: mime_types)\n\n accept(format) unless @accepted.include?(format)\n\n self\nend\n" |
#any? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
203 204 205 |
# File 'lib/hanami/action/config/formats.rb', line 203 def any? @accepted.any? end |
#clear ⇒ self
Clears any previously added mappings and format values.
219 220 221 222 223 224 225 |
# File 'lib/hanami/action/config/formats.rb', line 219 def clear @accepted = [] @default = nil @mapping = {} self end |
#content_types_for(format) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
275 276 277 |
# File 'lib/hanami/action/config/formats.rb', line 275 def content_types_for(format) mapping[format]&.content_types || [] end |
#empty? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
197 198 199 |
# File 'lib/hanami/action/config/formats.rb', line 197 def empty? accepted.empty? end |
#format_for(media_type) ⇒ Symbol, NilClass
Retrieve the format name associated with the given media type
248 249 250 |
# File 'lib/hanami/action/config/formats.rb', line 248 def format_for(media_type) mapping.values.reverse.find { |format| format.media_type == media_type }&.name end |
#map(&blk) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
209 210 211 |
# File 'lib/hanami/action/config/formats.rb', line 209 def map(&blk) @accepted.map(&blk) end |
#media_type_for(format) ⇒ String? Also known as: mime_type_for
Returns the media type associated with the given format.
265 266 267 |
# File 'lib/hanami/action/config/formats.rb', line 265 def media_type_for(format) mapping[format]&.media_type end |
#register(format, media_type, accept_types: [media_type], content_types: [media_type]) ⇒ self
Registers a format and its associated media types.
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/hanami/action/config/formats.rb', line 131 def register(format, media_type, accept_types: [media_type], content_types: [media_type]) mapping[format] = Mime::Format.new( name: format.to_sym, media_type: media_type, accept_types: accept_types, content_types: content_types ) self end |
#values ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/hanami/action/config/formats.rb', line 34 def values msg = " Hanami::Action `config.formats.values` is deprecated and will be removed in Hanami 2.4.\n\n Please use `config.formats.accepted` instead.\n\n See https://guides.hanamirb.org/v2.3/actions/formats-and-mime-types/ for details.\n TEXT\n warn(msg, category: :deprecated)\n\n accepted\nend\n" |