Class: Hanami::Action::Config

Inherits:
Dry::Configurable::Config
  • Object
show all
Defined in:
lib/hanami/action/config.rb,
lib/hanami/action/config/formats.rb

Overview

Config for ‘Hanami::Action` classes.

See Also:

Since:

  • 2.0.0

Defined Under Namespace

Classes: Formats

Constant Summary collapse

DEFAULT_PUBLIC_DIRECTORY =

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 public directory

This serves as the root directory for file downloads

Since:

  • 1.0.0

"public"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cookiesHash{Symbol=>String}

Sets default cookie options for all responses.

By default this, is an empty hash.

Examples:

config.cookies = {
  domain: "hanamirb.org",
  path: "/controller",
  secure: true,
  httponly: true
}

Returns:

  • (Hash{Symbol=>String})

    the cookie options

Since:

  • 0.4.0



# File 'lib/hanami/action/config.rb', line 120

#default_charsetString

Sets a charset (character set) as default fallback for all the requests without a strict requirement for the charset.

By default, this value is nil.

Returns:

  • (String)

See Also:

Since:

  • 0.3.0



# File 'lib/hanami/action/config.rb', line 92

#default_headersHash{String=>String}

Sets default headers for all responses.

By default, this is an empty hash.

Examples:

config.default_headers = {"X-Frame-Options" => "DENY"}

Returns:

  • (Hash{String=>String})

    the headers

See Also:

Since:

  • 0.4.0



# File 'lib/hanami/action/config.rb', line 105

#formatsConfig::Formats (readonly)

Returns the format config for the action.

Returns:

Since:

  • 2.0.0



# File 'lib/hanami/action/config.rb', line 61

#handled_exceptionsHash{Exception=>Integer}

Specifies how to handle exceptions with an HTTP status.

Raised exceptions will return the corresponding HTTP status.

Examples:

config.handled_exceptions = {ArgumentError => 400}

Returns:

  • (Hash{Exception=>Integer})

    exception classes as keys and HTTP statuses as values

Since:

  • 0.2.0



# File 'lib/hanami/action/config.rb', line 23

#public_directoryString

Sets the path to public directory. This directory is used for file downloads.

This given directory will be appended onto the root directory.

By default, the public directory is ‘“public”`.

Examples:

config.public_directory = "public"
config.public_directory # => "/path/to/root/public"

Returns:

  • (String)

    the public directory path

See Also:

Since:

  • 2.0.0



167
168
169
170
# File 'lib/hanami/action/config.rb', line 167

def public_directory
  # This must be a string, for Rack compatibility
  root_directory.join(super).to_s
end

#root_directoryString

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.

Sets the the for the public directory, which is used for file downloads. This must be an existent directory.

Defaults to the current working directory.

Returns:

  • (String)

    the directory path

Since:

  • 1.0.0



# File 'lib/hanami/action/config.rb', line 138

Instance Method Details

#format(*formats) ⇒ Array<Symbol>

Sets the format (or formats) for the action.

To configure custom formats and MIME type mappings, call formats.add first.

Examples:

config.format :html, :json

Parameters:

  • formats (Array<Symbol>)

    the format names

Returns:

  • (Array<Symbol>)

    the given format names

See Also:

Since:

  • 2.0.0



84
85
86
87
88
89
90
# File 'lib/hanami/action/config.rb', line 84

def format(*formats)
  if formats.empty?
    self.formats.values
  else
    self.formats.values = formats
  end
end

#handle_exception(exceptions) ⇒ void

This method returns an undefined value.

Specifies how to handle exceptions with an HTTP status

Raised exceptions will return the corresponding HTTP status

The specified exceptions will be merged with any previously configured exceptions

Examples:

config.handle_exceptions(ArgumentError => 400}

Parameters:

  • exceptions (Hash{Exception=>Integer})

    exception classes as keys and HTTP statuses as values

See Also:

Since:

  • 0.2.0



54
55
56
57
58
59
# File 'lib/hanami/action/config.rb', line 54

def handle_exception(exceptions)
  self.handled_exceptions = handled_exceptions
    .merge(exceptions)
    .sort { |(ex1, _), (ex2, _)| ex1.ancestors.include?(ex2) ? -1 : 1 }
    .to_h
end