Class: Hanami::Action::Config::Formats Private

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

Since:

  • 2.0.0

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

Since:

  • 2.0.0

{
  "application/octet-stream" => :all,
  "*/*" => :all
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Since:

  • 2.0.0



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

#mappingObject

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.

Since:

  • 2.0.0



27
28
29
# File 'lib/hanami/action/config/formats.rb', line 27

def mapping
  @mapping
end

#valuesObject

The array of enabled formats.

Examples:

config.formats.values = [:html, :json]
config.formats.values # => [:html, :json]

Since:

  • 2.0.0



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

Overloads:

  • #add(format) ⇒ self

    Adds and enables a format.

    Examples:

    config.formats.add(:json)

    Parameters:

    • format (Symbol)
  • #add(format, mime_type) ⇒ self

    Adds a custom format to MIME type mapping and enables the format. Adds a format mapping to a single MIME type.

    Examples:

    config.formats.add(:json, "application/json")

    Parameters:

    • format (Symbol)
    • mime_type (String)
  • #add(format, mime_types) ⇒ self

    Adds a format mapping to multiple MIME types.

    Examples:

    config.formats.add(:json, ["application/json+scim", "application/json"])

    Parameters:

    • format (Symbol)
    • mime_types (Array<String>)

Returns:

  • (self)

Since:

  • 2.0.0



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.

Returns:

  • (Boolean)

Since:

  • 2.0.0



112
113
114
# File 'lib/hanami/action/config/formats.rb', line 112

def any?
  @values.any?
end

#clearself

Clears any previously added mappings and format values.

Returns:

  • (self)

Since:

  • 2.0.0



140
141
142
143
144
145
# File 'lib/hanami/action/config/formats.rb', line 140

def clear
  @mapping = DEFAULT_MAPPING.dup
  @values = []

  self
end

#defaultSymbol?

Returns the default format name

Examples:

@config.formats.default # => :json

Returns:

  • (Symbol, nil)

    the default format name, if any

Since:

  • 2.0.0



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.

Returns:

  • (Boolean)

Since:

  • 2.0.0



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

Examples:

@config.formats.format_for("application/json") # => :json

Parameters:

  • mime_type (String)

    the MIME Type

Returns:

  • (Symbol, NilClass)

    the associated format name, if any

See Also:

Since:

  • 2.0.0



160
161
162
# File 'lib/hanami/action/config/formats.rb', line 160

def format_for(mime_type)
  @mapping[mime_type]
end

#keysObject

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.

Since:

  • 2.0.0



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.

Since:

  • 2.0.0



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.

Examples:

@config.formats.mime_type_for(:json) # => "application/json"

Parameters:

  • format (Symbol)

    the format name

Returns:

  • (String, nil)

    the associated MIME type, if any

See Also:

Since:

  • 2.0.0



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.

Parameters:

  • format (Symbol)

    the format name

Returns:

  • (Array<String>)

    the associated MIME types

Since:

  • 2.0.0



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