Module: ReSorcery::Configuration
- Extended by:
- Decoder::BuiltinDecoders
- Included in:
- ReSorcery
- Defined in:
- lib/re_sorcery/configuration.rb
Overview
Configure ‘ReSorcery`: All configuration kept in one place
‘ReSorcery` has some values that can be configured by users. To keep such configuration clear, and to prevent confusing behavior, `#configure` can only be called once, and must be called before `include`ing `ReSorcery`.
Example:
ReSorcery.configure do
link_rels ['self', 'create', 'update']
link_methods ['get', 'post', 'put']
end
configuration methods. Each entry contains a ‘:decoder` key, which is used to check the value passed to the configuration method, and a `:default` key, which is the default value for that entry.
Constant Summary collapse
- UNIQUE_STRING_OR_SYMBOL =
non_empty_array(is(String, Symbol).map(&:to_s)).map(&:uniq)
- DEFAULT_LINK_METHOD_DECODER =
is(Proc) .and { |p| p.arity == 1 || "default_link_method Proc must accept exactly one argument" }
- CONFIGURABLES =
{ link_rels: { decoder: UNIQUE_STRING_OR_SYMBOL, default: %w[self create update destroy].freeze, }.freeze, link_methods: { decoder: UNIQUE_STRING_OR_SYMBOL, default: %w[get post patch put delete].freeze, }.freeze, default_link_method: { decoder: DEFAULT_LINK_METHOD_DECODER, default: ->(link_methods) { link_methods.first }.freeze, }.freeze, default_link_type: { decoder: is(String), default: "application/json", }.freeze, }.freeze
Instance Method Summary collapse
Instance Method Details
#configuration ⇒ Object
48 49 50 |
# File 'lib/re_sorcery/configuration.rb', line 48 def configuration @configuration ||= CONFIGURABLES.transform_values { |v| v.fetch(:default) } end |
#configure ⇒ Object
52 53 54 55 56 57 |
# File 'lib/re_sorcery/configuration.rb', line 52 def configure(&) raise Error::InvalidConfigurationError, @configured if configured? @configured = "configured at #{caller_locations.first}" instance_exec(&) end |