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.
Constant Summary collapse
- DEFAULT_MAPPING =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default MIME type to format mapping
{ "application/octet-stream" => :all, "*/*" => :all }.freeze
Instance Attribute Summary collapse
- #mapping ⇒ Object private
-
#values ⇒ Object
The array of enabled formats.
Instance Method Summary collapse
- #add(format, mime_types = []) ⇒ self
- #any? ⇒ Boolean private
-
#clear ⇒ self
Clears any previously added mappings and format values.
-
#default ⇒ Symbol?
Returns the default format name.
- #empty? ⇒ Boolean private
-
#format_for(mime_type) ⇒ Symbol, NilClass
Retrieve the format name associated with the given MIME Type.
-
#initialize(values: [], mapping: DEFAULT_MAPPING.dup) ⇒ Formats
constructor
private
A new instance of Formats.
- #keys ⇒ Object private
- #map(&blk) ⇒ Object private
-
#mime_type_for(format) ⇒ String?
Returns the primary MIME type associated with the given format.
-
#mime_types_for(format) ⇒ Array<String>
Returns an array of all MIME types associated with the given format.
Constructor Details
#initialize(values: [], mapping: DEFAULT_MAPPING.dup) ⇒ 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.
41 42 43 44 |
# File 'lib/hanami/action/config/formats.rb', line 41 def initialize(values: [], mapping: DEFAULT_MAPPING.dup) @values = values @mapping = mapping end |
Instance Attribute Details
#mapping ⇒ 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.
27 28 29 |
# File 'lib/hanami/action/config/formats.rb', line 27 def mapping @mapping end |
#values ⇒ Object
The array of enabled formats.
37 38 39 |
# File 'lib/hanami/action/config/formats.rb', line 37 def values @values end |
Instance Method Details
#add(format) ⇒ self #add(format, mime_type) ⇒ self #add(format, mime_types) ⇒ self
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/hanami/action/config/formats.rb', line 92 def add(format, mime_types = []) format = Utils::Kernel.Symbol(format) Array(mime_types).each do |mime_type| @mapping[Utils::Kernel.String(mime_type)] = format end @values << format unless @values.include?(format) self end |
#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.
112 113 114 |
# File 'lib/hanami/action/config/formats.rb', line 112 def any? @values.any? end |
#clear ⇒ self
Clears any previously added mappings and format values.
140 141 142 143 144 145 |
# File 'lib/hanami/action/config/formats.rb', line 140 def clear @mapping = DEFAULT_MAPPING.dup @values = [] self end |
#default ⇒ Symbol?
Returns the default format name
204 205 206 |
# File 'lib/hanami/action/config/formats.rb', line 204 def default @values.first 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.
106 107 108 |
# File 'lib/hanami/action/config/formats.rb', line 106 def empty? @values.empty? end |
#format_for(mime_type) ⇒ Symbol, NilClass
Retrieve the format name associated with the given MIME Type
160 161 162 |
# File 'lib/hanami/action/config/formats.rb', line 160 def format_for(mime_type) @mapping[mime_type] end |
#keys ⇒ 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.
210 211 212 |
# File 'lib/hanami/action/config/formats.rb', line 210 def keys @mapping.keys 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.
118 119 120 |
# File 'lib/hanami/action/config/formats.rb', line 118 def map(&blk) @values.map(&blk) end |
#mime_type_for(format) ⇒ String?
Returns the primary MIME type associated with the given format.
177 178 179 |
# File 'lib/hanami/action/config/formats.rb', line 177 def mime_type_for(format) @mapping.key(format) end |
#mime_types_for(format) ⇒ Array<String>
Returns an array of all MIME types associated with the given format.
Returns an empty array if no such format is configured.
191 192 193 |
# File 'lib/hanami/action/config/formats.rb', line 191 def mime_types_for(format) @mapping.each_with_object([]) { |(mime_type, f), arr| arr << mime_type if format == f } end |