Class: Dry::Schema::Messages::Abstract

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/dry/schema/messages/abstract.rb

Overview

Abstract class for message backends

Direct Known Subclasses

I18n, Namespaced, YAML

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(options = EMPTY_HASH) ⇒ 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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dry/schema/messages/abstract.rb', line 50

def self.build(options = EMPTY_HASH)
  messages = new

  messages.configure do |config|
    options.each do |key, value|
      config.public_send(:"#{key}=", value)
    end

    config.root = "#{config.top_namespace}.#{config.root}"

    config.rule_lookup_paths = config.rule_lookup_paths.map { |path|
      "#{config.top_namespace}.#{path}"
    }

    yield(config) if block_given?
  end

  messages.prepare
end

Instance Method Details

#call(predicate, options) ⇒ Template Also known as: []

Retrieve a message template

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dry/schema/messages/abstract.rb', line 89

def call(predicate, options)
  options = {locale: default_locale, **options}
  opts = options.reject { |k,| config.lookup_options.include?(k) }
  path = lookup_paths(predicate, options).detect { |key| key?(key, opts) }

  return unless path

  result = get(path, opts)

  [
    Template.new(
      messages: self,
      key: path,
      options: opts
    ),
    result[:meta]
  ]
end

#default_localeObject

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.



167
168
169
# File 'lib/dry/schema/messages/abstract.rb', line 167

def default_locale
  config.default_locale
end

#filled_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.



139
140
141
# File 'lib/dry/schema/messages/abstract.rb', line 139

def filled_lookup_paths(tokens)
  config.lookup_paths.map { |path| path % tokens }
end

#interpolatable_data(_key, _options, **_data) ⇒ 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.

Raises:

  • (NotImplementedError)


172
173
174
# File 'lib/dry/schema/messages/abstract.rb', line 172

def interpolatable_data(_key, _options, **_data)
  raise NotImplementedError
end

#interpolate(_key, _options, **_data) ⇒ 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.

Raises:

  • (NotImplementedError)


177
178
179
# File 'lib/dry/schema/messages/abstract.rb', line 177

def interpolate(_key, _options, **_data)
  raise NotImplementedError
end

#key?(_key, _options = EMPTY_HASH) ⇒ Boolean

Check if given key is defined

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


115
116
117
# File 'lib/dry/schema/messages/abstract.rb', line 115

def key?(_key, _options = EMPTY_HASH)
  raise NotImplementedError
end

#looked_up_paths(predicate, options) ⇒ String

Retrieve an array of looked up paths

Parameters:

  • predicate (Symbol)
  • options (Hash)

Returns:

  • (String)


127
128
129
130
# File 'lib/dry/schema/messages/abstract.rb', line 127

def looked_up_paths(predicate, options)
  tokens = lookup_tokens(predicate, options)
  filled_lookup_paths(tokens)
end

#lookup_paths(predicate, options) ⇒ 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.



133
134
135
136
# File 'lib/dry/schema/messages/abstract.rb', line 133

def lookup_paths(predicate, options)
  tokens = lookup_tokens(predicate, options)
  filled_lookup_paths(tokens)
end

#namespaced(namespace) ⇒ Object

Return a new message backend that will look for messages under provided namespace

Parameters:

  • namespace (Symbol, String)


153
154
155
# File 'lib/dry/schema/messages/abstract.rb', line 153

def namespaced(namespace)
  Dry::Schema::Messages::Namespaced.new(namespace, self)
end

#rootPathname

Return root path to messages file

Returns:

  • (Pathname)


162
163
164
# File 'lib/dry/schema/messages/abstract.rb', line 162

def root
  config.root
end

#rule(name, options = {}) ⇒ 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.



76
77
78
79
80
81
82
# File 'lib/dry/schema/messages/abstract.rb', line 76

def rule(name, options = {})
  tokens = {name: name, locale: options.fetch(:locale, default_locale)}
  path = rule_lookup_paths(tokens).detect { |key| key?(key, options) }

  rule = get(path, options) if path
  rule.is_a?(Hash) ? rule[:text] : rule
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.



144
145
146
# File 'lib/dry/schema/messages/abstract.rb', line 144

def rule_lookup_paths(tokens)
  config.rule_lookup_paths.map { |key| key % tokens }
end

#translate(key, locale: default_locale) ⇒ 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.



71
72
73
# File 'lib/dry/schema/messages/abstract.rb', line 71

def translate(key, locale: default_locale)
  t["#{config.top_namespace}.#{key}", locale: locale]
end