Class: Dry::Schema::Messages::Namespaced

Inherits:
Abstract
  • Object
show all
Defined in:
lib/dry/schema/messages/namespaced.rb

Overview

Namespaced messages backend

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

build, cache, #cache, #default_locale, #lookup, #namespaced, #root, #rule, #translate

Constructor Details

#initialize(namespace, messages) ⇒ Namespaced

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 Namespaced.



23
24
25
26
27
28
# File 'lib/dry/schema/messages/namespaced.rb', line 23

def initialize(namespace, messages)
  @config = messages.config
  @namespace = namespace
  @messages = messages
  @call_opts = { namespace: namespace }.freeze
end

Instance Attribute Details

#call_optsObject (readonly)

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.



20
21
22
# File 'lib/dry/schema/messages/namespaced.rb', line 20

def call_opts
  @call_opts
end

#configObject (readonly)

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.



17
18
19
# File 'lib/dry/schema/messages/namespaced.rb', line 17

def config
  @config
end

#messagesObject (readonly)

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.



14
15
16
# File 'lib/dry/schema/messages/namespaced.rb', line 14

def messages
  @messages
end

#namespaceObject (readonly)

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.



11
12
13
# File 'lib/dry/schema/messages/namespaced.rb', line 11

def namespace
  @namespace
end

Instance Method Details

#call(key, options = {}) ⇒ Object Also known as: []



43
44
45
# File 'lib/dry/schema/messages/namespaced.rb', line 43

def call(key, options = {})
  super(key, options.empty? ? call_opts : options.merge(call_opts))
end

#get(key, options = {}) ⇒ String

Get a message for the given key and its options

Parameters:

  • key (Symbol)
  • options (Hash) (defaults to: {})

Returns:

  • (String)


38
39
40
# File 'lib/dry/schema/messages/namespaced.rb', line 38

def get(key, options = {})
  messages.get(key, options)
end

#key?(key, *args) ⇒ Boolean

Check if given key is defined

Returns:

  • (Boolean)


53
54
55
# File 'lib/dry/schema/messages/namespaced.rb', line 53

def key?(key, *args)
  messages.key?(key, *args)
end

#lookup_paths(tokens) ⇒ 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.



58
59
60
# File 'lib/dry/schema/messages/namespaced.rb', line 58

def lookup_paths(tokens)
  super(tokens.merge(root: "#{tokens[:root]}.#{namespace}")) + super
end

#rule_lookup_paths(tokens) ⇒ 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.



63
64
65
66
# File 'lib/dry/schema/messages/namespaced.rb', line 63

def rule_lookup_paths(tokens)
  base_paths = messages.rule_lookup_paths(tokens)
  base_paths.map { |key| key.gsub('dry_schema', "dry_schema.#{namespace}") } + base_paths
end