Class: Dry::Validation::Messages::Template Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/validation/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.



47
48
49
50
51
# File 'lib/dry/validation/template.rb', line 47

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)


27
28
29
# File 'lib/dry/validation/template.rb', line 27

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)


19
20
21
# File 'lib/dry/validation/template.rb', line 19

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)


23
24
25
# File 'lib/dry/validation/template.rb', line 23

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.



30
31
32
# File 'lib/dry/validation/template.rb', line 30

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.



35
36
37
38
39
40
41
42
43
44
# File 'lib/dry/validation/template.rb', line 35

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.



59
60
61
# File 'lib/dry/validation/template.rb', line 59

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.



54
55
56
# File 'lib/dry/validation/template.rb', line 54

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