Class: Dry::Schema::Messages::Template Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Template wraps a string with interpolation tokens and defines evaluator function dynamically

Constant Summary collapse

TOKEN_REGEXP =

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.

/%{(\w*)}/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, tokens, evaluator) ⇒ Template

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



49
50
51
52
53
# File 'lib/dry/schema/messages/template.rb', line 49

def initialize(text, tokens, evaluator)
  @text = text
  @tokens = tokens
  @evaluator = evaluator
end

Instance Attribute Details

#evaluatorProc (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.

!@attribute [r] evaluator

Returns:

  • (Proc)


29
30
31
# File 'lib/dry/schema/messages/template.rb', line 29

def evaluator
  @evaluator
end

#textString (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.

!@attribute [r] text

Returns:

  • (String)


21
22
23
# File 'lib/dry/schema/messages/template.rb', line 21

def text
  @text
end

#tokensHash (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.

!@attribute [r] tokens

Returns:

  • (Hash)


25
26
27
# File 'lib/dry/schema/messages/template.rb', line 25

def tokens
  @tokens
end

Class Method Details

.[](input) ⇒ 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.



32
33
34
# File 'lib/dry/schema/messages/template.rb', line 32

def self.[](input)
  new(*parse(input))
end

.parse(input) ⇒ 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.



37
38
39
40
41
42
43
44
45
46
# File 'lib/dry/schema/messages/template.rb', line 37

def self.parse(input)
  tokens = input.scan(TOKEN_REGEXP).flatten(1).map(&:to_sym)
  text = input.gsub('%', '#')

  evaluator = <<-RUBY.strip
    -> (#{tokens.map { |token| "#{token}:" }.join(", ")}) { "#{text}" }
  RUBY

  [text, tokens, eval(evaluator, binding, __FILE__, __LINE__ - 3)]
end

Instance Method Details

#call(data = EMPTY_HASH) ⇒ Object Also known as: []

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.



61
62
63
# File 'lib/dry/schema/messages/template.rb', line 61

def call(data = EMPTY_HASH)
  data.empty? ? evaluator.() : evaluator.(data)
end

#data(input) ⇒ 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.



56
57
58
# File 'lib/dry/schema/messages/template.rb', line 56

def data(input)
  tokens.each_with_object({}) { |k, h| h[k] = input[k] }
end