lib/dry/schema/messages.rb in dry-schema-0.3.0 vs lib/dry/schema/messages.rb in dry-schema-0.4.0
- old
+ new
@@ -4,38 +4,29 @@
module Schema
# An API for configuring message backends
#
# @api private
module Messages
- def self.setup(config)
- messages = build(config)
+ BACKENDS = {
+ i18n: 'I18n',
+ yaml: 'YAML'
+ }.freeze
- if config.messages_file && config.namespace
- messages.merge(config.messages_file).namespaced(config.namespace)
- elsif config.messages_file
- messages.merge(config.messages_file)
- elsif config.namespace
- messages.namespaced(config.namespace)
- else
- messages
- end
- end
+ module_function
- # @api private
- def self.build(config)
- klass = case config.messages
- when :yaml then default
- when :i18n then Messages::I18n
- else
- raise "+#{config.messages}+ is not a valid messages identifier"
+ public def setup(config)
+ backend_class = BACKENDS.fetch(config.backend) do
+ raise "+#{config.backend}+ is not a valid messages identifier"
end
- klass.build
- end
+ namespace = config.namespace
+ options = config.to_h.select { |k, _| Abstract.settings.include?(k) }
- # @api private
- def self.default
- Messages::YAML
+ messages = Messages.const_get(backend_class).build(options)
+
+ return messages.namespaced(namespace) if namespace
+
+ messages
end
end
end
end